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 7492215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:23:48+00:00 2026-05-29T16:23:48+00:00

I have the following three classes public class Base { string name; } public

  • 0

I have the following three classes

public class Base
{
    string name;
}

public class Foo : Base
{
    int value;
}

public class Bar : Base
{
    double value;
}

This is what I’m attempting

Base current = null;

if (somecondition)
    current = new Foo();
else
    current = new Bar();

for (int i=0; i<5; i++)
{
    current.value = i;           
}

The problem is VS 2010 shows an error in the loop body because Base doesn’t have a property value.

Now, I could workaround this issue by this way:

Base current = null;
bool isBar = true;

if (somecondition)
{
    current = new Foo();
    isBar = false;
}
else
    current = new Bar();

for (int i=0; i<5; i++)
{
    if (isBar)
        (current as Bar).value = i;          
    else
        (current as Foo).value = i;
}

But I was hoping for a better solution because once the loop starts iterating, the type of current isn’t going to change, yet I am going to test the type and accordingly cast it for each iteration.

What would be the right way to do this?

  • 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-05-29T16:23:48+00:00Added an answer on May 29, 2026 at 4:23 pm

    After making the fields accessible (or exposing them as accessible properties instead), your options are:

    • Make your base class abstract, with an abstract method or property to set the value (and possibly return it) – the problem here is that you can’t easily do that when the two types are different
    • Create an interface to do the same sort of thing, and cast to the interface
    • Just cast as you are now
    • Use dynamic if you’re using C# 4 – just declare current as dynamic, and you can assign to current.value and the compiler will insert code to work it out at execution time
    • Redesign your code / inheritance hierarchy

    Personally I would at least consider the last approach – is inheritance definitely appropriate here? Do the two types for value really need to be different? Do they have the same meaning, and if so would it make sense to push the value to the base class and potentially make it generic if you need different types?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three different base classes: class BaseA { public: virtual int foo() =
Assuming I have the following classes in an application: A base class: public abstract
Let's say I have the following two classes: public class Person { public string
Suppose I have the following class hierarchy: class A { int foo; virtual ~A()
Let's say I have two classes like the following: Class A { public: ..
I have an abstract entity base class defined like this: public abstract class SessionItem
I have a base class looking like this: public class BaseController : Controller {
Given the following inheritance tree: I have a public class BaseForm : Form This
I have the following problem. I have three classes, A, B and C. A
In my project I have the following three interfaces, which are implemented by classes

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.