CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2011
    Posts
    2

    Self Initializing Class (Beginner needs Help)

    Ok i am trying to create a class i can use in each of my programs since its reusable code.

    For learning purposes i'm working on a networking managemeng type program

    So to start off with i have created a class that looks like this

    namespace MyCompany
    {

    public class Network
    {

    public static List<Segment> holder = new List<Segment>();

    public static void Main()
    {

    // XML Processing in here to populate holder List, it dynamically retrieves teh XML from a SOAP/Web server
    }

    public List<Segment> getSegments() {
    return(holder);
    }
    public class Segment
    {
    // Class structure to hold network segment name and an ArrayList of IPs for that segment
    }

    }


    }



    in my program i have or want to do something like this

    MyCompany.Network network = new MyCompany.Network();

    and when i do the above the hope is that the class intiailizes, runs through the Main() and popuplates the holder array so i can then do

    List<Segment> segments = network.getSegments(); and my segments list has all my network segment objects already prepopulated.


    The problem i am running into is the Main() for MyCompany.Network isn't running and the holder list isn't being populated.. In a simplier example i can't get Main() to run at all (even for something like just counting an int)..

    I apologize i am fairly new to C#, i've looked through tutorials and none of them go through anything like this for Classes.

  2. #2
    Join Date
    Dec 2011
    Posts
    2

    Re: Self Initializing Class (Beginner needs Help)

    OK i think i figured it out

    any self executing code seems to have to go into a same named method not Main()
    Code:
    namespace MyCompany {
       public class Network {
            public Network() {
                        //executes code on new MyCompany.Network();
            }
       }
    }

  3. #3
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: Self Initializing Class (Beginner needs Help)

    What you did is define a Constructor.
    Each class can have any number of constructors that receive different initialization variables. When you create a new instance of the class, the constructor gets called and initialices your instance. You could for example have:
    public classConstructor()
    public classConstructor(int ID)
    public classConstructor(int ID, string Name)
    ...etc

    Read this article on Using Constructors and bookmark MSDN, as it will be your best friend to sort out begginers doubts.

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