Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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:

    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,

Edited by 3xodus

Using: Visual Studio 2005/08

Languages: C#, Win32 C++, Java, PHP

  • Administrators
Posted

Given the following three class definitions

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

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

           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();

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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:

 

   [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]

Using: Visual Studio 2005/08

Languages: C#, Win32 C++, Java, PHP

Posted

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 ;)

Using: Visual Studio 2005/08

Languages: C#, Win32 C++, Java, PHP

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...