Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# put methods in different class.

  • 18-12-2009 10:35am
    #1
    Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭


    Hi,

    Ok, so in VB.NET i've been using a library (TwitterVB) that has put different methods in different classes. How do I do this in C# ?

    Sample Code:
    public class Parent
    {
    [INDENT]public void mParent()
    {
    }[/INDENT]
    }
    public class Child : Parent
    {
    [INDENT]public void mChild()
    {
    }[/INDENT]
    }
    

    Is this possible?:
    Parent obj = new Parent();
    obl.Child.mChild();
    

    I would think this is possible if obj was of type Child. I'm sure i'm missing out on the use of some OOP keyword or something.

    Ideas?


Comments

  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    It's possible yes. The way you would do it depends on exactly what you want to achieve.

    You also need to consider "is a" and "has a" relationships and maybe get a better understanding of inheritance. In your example you have defined Child as inheriting from Parent (i.e. Child "is a" Parent). In this case Child would inherit any methods in Parent, for e.g. the mParent() method.

    Then typically you would create an instance of the Child
    Child myChild = new Child();
    
    and you could call methods in both it and the parent
    myChild.mChild();
    myChild.mParent();
    

    What you have described in the line "obl.Child.mChild();" is more of a "has a" relationship. For this you would define your classes differently for e.g.
    public class Class1
    {
       public Class2 myObj2;
    
       public Class1()
       {
          this.myObj2 = new Class2();
       }
    }
    
    public class Class2
    {
       public void method2()
       {
          //do something
       }
    }
    

    Now Class1 has an instance of Class2 and you can call methods like you described in your example, myObj2 is a property of class 1 and can be referenced from it.
    Class1 myObj = new Class1();
    myObj.myObj2.method2();
    

    Hope that makes sense?


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    stevenmu wrote: »
    Hope that makes sense?

    Yes, it does. Thanks.

    I understand inheritane and all that but I still couldn't do what I wanted.

    I had thought of creating a Class2 object in Class1 and calling that to access class2 but I wasn't sure it was the right way of doing it.

    At least now I know somebody else approves of this technique so i'll happily use it.

    Thanks!


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Yep, it's a reasonably standard thing to do.

    One additional suggestion: in my example I just made myObj2 public in Class1 which allows it to be accessed from other classes. While this works fine, it's not considered best practice, the best practice way is to make it private and to use Properties to access it, the benefit of these is that it allows you to put extra logic in (like in the example on the MS site where instead of just returning the seconds it converts them to hours). But certainly just for experimenting/learning leaving it public and accessing it directly is fine.


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    Yes, I have been looking at that also. Thanks.

    I'm getting a StackOverflowException now when I try and create a new object of the dervied class in the parent.

    I'm thinking this is caused by an infinit loop but I can't see where.Annoying!

    EDIT: The loop is caused by specifying that the child class is inheriting from the parent.
    I am declaring a new instance of child in the default constructor for parent.
    How can I solve this while keeping the inheritance intact?

    EDIT Again: I ave viewed a class diagram for TwitterVB and it appears that all the method classes are tied together using a methodsBase class.

    I suppose it's about time I sitdown with a pen and paper..:o


Advertisement