Class Inheritance not default

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    Class Inheritance not default

    If I have the following classes:

    public class Person
    {
    ....
    public Person()
    {
    ...
    }

    public Person(int userID)
    {
    ...
    }
    ....
    }


    public class User : Person
    {
    ....
    public User()
    {
    ...
    }

    public User(int userID)
    {
    ...
    }
    ....
    }

    In my program I create a User object like so:

    Dim user = New User(1)

    My problem is that I need to pass the userID which is being passed to User
    to Person as well so it can get the Person record using userID as the key.
    I would then get the User record also using the userID key.

    The problem is that when I do the instantiation, it goes to User and then
    directly to Person and its default constructer and not the one with the
    userID being passed.

    How do I get it to go to the correct constructor using inheritance?

    Thanks,

    Tom


  • DrewCE

    #2
    Re: Class Inheritance not default

    public class User : Person
    {
    //...
    public User()
    : base()
    {
    // ...
    }

    public User(int userID)
    : base(userID)
    {
    // ...
    }
    // ....
    }

    -Drew

    "tshad" <tfs@dslextreme .comwrote in message
    news:ufYroD8GJH A.2156@TK2MSFTN GP05.phx.gbl...
    If I have the following classes:
    >
    public class Person
    {
    ...
    public Person()
    {
    ...
    }
    >
    public Person(int userID)
    {
    ...
    }
    ....
    }
    >
    >
    public class User : Person
    {
    ...
    public User()
    {
    ...
    }
    >
    public User(int userID)
    {
    ...
    }
    ....
    }
    >
    In my program I create a User object like so:
    >
    Dim user = New User(1)
    >
    My problem is that I need to pass the userID which is being passed to User
    to Person as well so it can get the Person record using userID as the key.
    I would then get the User record also using the userID key.
    >
    The problem is that when I do the instantiation, it goes to User and then
    directly to Person and its default constructer and not the one with the
    userID being passed.
    >
    How do I get it to go to the correct constructor using inheritance?
    >
    Thanks,
    >
    Tom
    >

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Class Inheritance not default

      tshad <tfs@dslextreme .comwrote:

      <snip>
      How do I get it to go to the correct constructor using inheritance?
      See http://pobox.com/~skeet/csharp/constructors.html

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      • tshad

        #4
        Re: Class Inheritance not default


        "DrewCE" <moc.sgodniahc@ werd - backwardswrote in message
        news:eZ5NhO8GJH A.4408@TK2MSFTN GP04.phx.gbl...
        public class User : Person
        {
        //...
        public User()
        : base()
        {
        // ...
        }
        >
        public User(int userID)
        : base(userID)
        {
        // ...
        }
        // ....
        }
        >
        That did it.

        Thanks,

        Tom
        -Drew
        >
        "tshad" <tfs@dslextreme .comwrote in message
        news:ufYroD8GJH A.2156@TK2MSFTN GP05.phx.gbl...
        >If I have the following classes:
        >>
        >public class Person
        >{
        >...
        > public Person()
        > {
        > ...
        > }
        >>
        > public Person(int userID)
        > {
        > ...
        > }
        > ....
        >}
        >>
        >>
        >public class User : Person
        >{
        >...
        > public User()
        > {
        > ...
        > }
        >>
        > public User(int userID)
        > {
        > ...
        > }
        > ....
        >}
        >>
        >In my program I create a User object like so:
        >>
        > Dim user = New User(1)
        >>
        >My problem is that I need to pass the userID which is being passed to
        >User to Person as well so it can get the Person record using userID as
        >the key. I would then get the User record also using the userID key.
        >>
        >The problem is that when I do the instantiation, it goes to User and then
        >directly to Person and its default constructer and not the one with the
        >userID being passed.
        >>
        >How do I get it to go to the correct constructor using inheritance?
        >>
        >Thanks,
        >>
        >Tom
        >>
        >

        Comment

        Working...