Pointers and Casting in C#/.NET

3xodus

Freshman
Joined
Dec 26, 2004
Messages
47
Location
Derbyshire, UK
I have some C++ code that I wrote while at Uni (I'm in a year work placement at the minute) that's along the lines of:
Code:
    ClassA* p_classA;
    ClassB* p_classB;
    ClassC* p_classC;

    switch(someInteger)
    {
        case 1:
            p_classB = new ClassB;
            p_classA = p_classB;
            break;
        case 2:
            p_classC = new ClassB;
            p_classA = p_classC;
            break;
        (.. and so on..)
    }
    p_classA->Something();
I came back to look at it as it does what I want to do at work in C# - that is to work with either ClassB or ClassC without knowing which, via a pointer of ClassA (which both ClassB and ClassC derive from), upcasted from a ClassB or C instance.

Is this even possible now in C#? I've read a little about how C#'s pointers differ from C++, and it seems not. All 3 classes are will be my own, but would contain managed properties such as strings and integers.

Is there another way I can approach this?
Now sure how well I've explained what I'm trying to do either.

Thanks,
 
Last edited:
Given the following three class definitions
C#:
public class ClassA
{
    public virtual void Something()
    {Debug.WriteLine("ClassA's version");}
}

public class ClassB : ClassA
{
    public override void Something()
    { Debug.WriteLine("ClassB's version"); }
}

public class ClassC : ClassA
{
    public override void Something()
    { Debug.WriteLine("ClassC's version"); }
}

then your code could be implemented as
C#:
ClassA p_classA;
    ClassB p_classB;
    ClassC p_classC;

            int someInteger = 1;

    switch(someInteger)
    {
        case 1:
            p_classB = new ClassB();
            p_classA = p_classB;
            break;
        case 2:
            p_classC = new ClassC();
            p_classA = p_classC;
            break;
    //    (.. and so on..)
    }
    p_classA.Something();

is that what you are after?

In fact you could simplify it to
C#:
            ClassA p_classA;

            int someInteger = 1;

            switch(someInteger)
            {
            case 1:
                p_classA = new ClassB();
                break;
            case 2:
                p_classA = new ClassC();
                break;
    //    (.. and so on..)
            }
            p_classA.Something();
 
It is almost, and I believe I've tried it this way at some point. The problem I have here (my fault - I didn't mention this) is that if I have a function in ClassB or ClassC (and ONLY ClassB OR ClassC) then the ClassA that I'm left with doesn't have access to it - I can only use this as an instance of ClassA with ClassB or ClassC overriden functions - hopefully of course I'm missing something.

In the C++ version as the ClassA pointer was pointing to an instance of, for example, a ClassB instance, I could access public functions within ClassB.

An example might be, if I were to change the existing switch statement:

Code:
    [COLOR=#0600ff]switch[/COLOR][COLOR=#000000]([/COLOR]someInteger[COLOR=#000000])[/COLOR]
    [COLOR=#000000]{[/COLOR]
        [COLOR=#0600ff]case[/COLOR] [COLOR=#ff0000]1[/COLOR]:
            p_classA = [URL="http://www.google.com/search?q=new+msdn.microsoft.com"][COLOR=#008000]new[/COLOR][/URL] ClassB[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
            p_classA->writeLetterB();
            [COLOR=#0600ff]break[/COLOR];
        [COLOR=#0600ff]case[/COLOR] [COLOR=#ff0000]2[/COLOR]:
            p_classA = [URL="http://www.google.com/search?q=new+msdn.microsoft.com"][COLOR=#008000]new[/COLOR][/URL] ClassC[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
            p_classA->writeLetterC();
            [COLOR=#0600ff]break[/COLOR];
    [COLOR=#000000]}
    
    if(p_classA is ClassB) p_classA->writeLetterB();
    else p_classA->writeLetterC();
[/COLOR]

 
Argh - sorry about this PlausiblyDamp - I *think* you're right on the money.

I was combining two different thoughts when I tried this out - it's been a hell of a day :), I'll try again with a clearer head in a few minutes. Thankyou very much ;)
 
Back
Top