Hi, I have I am trying to create a program for my lecturer, who has asked me to create an application containing a number of different class that all inherit from an abstract class.

This has been completed and encapsulated, however in writting the application that utilises these classes I have run into an issue.

The application will carry out the same routines regardless of the users choice of class. However as each class has different methods I can not see how to cast between a public object to the chosen cast.

ie the user selects one of the classes to use

class A
class B
class C

the user may select class B however the app would carry out the same procedures.

thinking i could just cast between the public object and an instance of the required class I tryed
public object userChoice;
B chosenClass = new B();
userChoice<B> = chosenClass;

this will not compile, please can anyone point me in the correct direction.

Dani AI

Generated

Short answer: hold the chosen instance in a variable typed as the shared abstract base (or an interface) and instantiate the appropriate derived class at runtime. That gives you a compile-time contract (methods/properties you can call) while the CLR dispatches the correct override for the concrete type. Using a raw object hides members and forces casts or type checks.

A minimal pattern:

abstract class BaseThing
{
    public abstract void DoWork();
}

class ThingA : BaseThing
{
    public override void DoWork() { /* A-specific behavior */ }
}

class ThingB : BaseThing
{
    public override void DoWork() { /* B-specific behavior */ }
}

// create based on user selection
BaseThing chosen = CreateFromUserChoice(selection);
chosen.DoWork(); // polymorphic call

If some derived types expose operations not shared by the base, prefer an interface or capability interface instead of sprinkling casts around:

interface ICanExport { void Export(); }

class ThingB : BaseThing, ICanExport
{
    public void Export() { /* only B does this */ }
}

if (chosen is ICanExport exporter) exporter.Export();

Notes tied to the thread: was pointing toward a common ancestor; and were right to flag the original declaration/syntax choices that hide members or are invalid. For creation, use a simple factory (or DI) to map the user choice to a concrete instance. When debugging, inspect chosen.GetType() in the debugger and prefer compile-time contracts (base class / interfaces) over runtime casting whenever possible.

Further reading: C# polymorphism and inheritance on Microsoft Docs (C# polymorphism).

Recommended Answers

All 6 Replies

The classes would need to share a parent to do that and all be descended from the parent

Hi thanks for your reply.

I am unsure what you mean by parent. All of the classes avalible to be selected by the user all use the same base abstract class. However the constructor of each changes the values of the fields within the base in a different manner and aslo have different methods avalible for use.

The global object I wish to base the application code on reports that it is of the correct type, however I am unable to access any of the required members.

All your 3 options would have to descend from the same class. Much as you descend from your parents, classes descend from things, currently if you say nothing else TObject. So while you could then cast your tobject and assign an instance of your item, it would be hard to work with.

--> public object userChoice; declaration of an object
--> B chosenClass = new B();
--> userChoice<B> = chosenClass; use of object without instantiation

userChoice<B> = chosenClass; use of object without instantiation

More like, syntax error.

FallenPaladin why don't you paste some representative code to show exactly the kind of thing you're trying to do?

Hi

Thank you all for your time. and, with your help I have managed to solve the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.