CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #1
    Join Date
    Nov 2006
    Posts
    357

    Question about method with ref argument

    Hey,

    Hopefully a simple question, heres some code:

    Code:
    class A { public int Var1; }
    class B : A { public int Var2; }
    
    class C
    {
       public static A GetA() 
       {
          A Var = new A();
          generateA(Var);
          return Var;
       }
    
       public static A GetB()
       {
          A Var = new B();
          generateA(ref Var);
          generateB(ref Var); // Error
          return Var;
       }
    
       protected static generateA(ref A Var)
       { Var.Var1 = 1; }
    
       protected static generateB(ref B Var)
       { Var.Var2 = 2; }
    
    }
    Now thats just a simple example of what im trying to do, but it seems to kick up a fuss when i try to pass over a base type that is acctually a subclass. In my real code i have a few methods that build up classes internal members, so one method for each sub class, that way i can reuse the code to build up the subclasses by populating in order...

    Anyway hopefully that shows what im trying to do, any help or advice would be great!
    Last edited by Grofit; May 17th, 2009 at 07:02 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured