Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7767955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:48:01+00:00 2026-06-01T15:48:01+00:00

Possible Duplicate: C# – Is there a better alternative than this to ‘switch on

  • 0

Possible Duplicate:
C# – Is there a better alternative than this to ‘switch on type’?

Consider the classic:

class Widget { }
class RedWidget : Widget { }
class BlueWidget : Widget { }

For the most part, in my UI, I can treat all Widgets the same. However, there are minor differences, which I need to if or switch through.

Possible approaches:

Enum Indicator – set by constructor

enum WidgetVariety { Red, Blue }

class Widget {
    public WidgetVariety Variety { get; protected set; }
}

class RedWidget : Widget {
    public RedWidget() {
        Variety = Red;
    }
}

// Likewise for BlueWidget...

switch (mywidget.Variety) {
case WidgetVariety.Red:
    // Red specific GUI stuff

case WidgetVariety.Blue:
    // Blue specific GUI stuff
}

Use is

Widget w = ...;
if (w is RedWidget) {
    (RedWidget)w ...
}
else if (w is BlueWidget) {
    (BlueWidget)w ...
}

The reason I’ve resorted to this is 1) Most of the code is already somewhat written this way, but much uglier. 2) 90% of the code is identical – basically just one column in a GridView needs to be handled differently depending on the type.

Which would you recommend? (Or anyone have a better solution?)


Edit I know I’ll probably be recommended to the Visitor Pattern, but that simply seems to complicated for sparse, minor differences in this case.

Edit 2
So one particular difference I was having a hard time sorting out is this column that is different between the two types. In one case, it retrieves a bool value, and assigns that to the grid cell. In the other case, it gets a string value.

I suppose in this case, it should be obvious that I could define:

public object virtual GetColumn4Data();

public override GetColumn4Data() { return m_boolval; }

public override GetColumn4Data() { return m_mystring; }

This felt wrong to me initially, due to the use of object. However, that is the type of the property that I am assigning to in the cell, so of course this makes sense!

Too long at the office today it seems…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:48:03+00:00Added an answer on June 1, 2026 at 3:48 pm

    There’s another possibility. Use virtual dispatch:

    class Widget
    {
        public virtual void GuiStuff() { }
    } 
    class RedWidget : Widget
    {
        public override void GuiStuff()
        {
            //... red-specific GUI stuff
            base.GuiStuff();
        }
    } 
    class BlueWidget : Widget
    {
        public override void GuiStuff()
        {
            //... blue-specific GUI stuff
            base.GuiStuff();
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there
Possible Duplicate: Model.find(1) gives ActiveRecord error when id 1 does not exist If there
Possible Duplicate: When do you use the “this” keyword? Hello, I understand that the
Possible Duplicate: how to use foursquare API in android application? This is a question
Possible Duplicate: Are there any O(1/n) algorithms? Any algorithm whose complexity decreases with increase
Possible Duplicate: Create Generic method constraining T to an Enum Is there any reason
Possible Duplicate: What is the “double tilde” (~~) operator in JavaScript? I found this
Possible Duplicate: C# okay with comparing value types to null Consider the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.