3

I have two classes declared like this:

class Object1
{
    protected ulong guid;
    protected uint type;

    public Object1(ulong Guid, uint Type)
    {
        this.guid = Guid;
        this.type = Type
    }
    // other details omitted
}

class Object2 : Object1
{
    // details omitted
}

In the client code, I want to be able to create an instance of each object like this:

Object1 MyObject1 = new Object1(123456789, 555);
Object2 MyObject2 = new Object2(987654321, 111);

How can I get Object2 to use Object1's constructor like that? Thankyou.

3 Answers 3

9
class Object2 : Object1
{
   Object2(ulong l, uint i ) : base (l, i)
   {
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Could someone please explain why this must be done? I appreciate the answer!
1

You have to provide a constructor with the same signature for Object2 and then call the base class's constructor:

class Object2 : Object1
{
    public Object2(ulong Guid, uint Type) : base(Guid, Type) {}
}

Comments

1

Give Object2 a constructor like this:-

public Object2(ulong Guid, uint Type): base(Guid, Type)

3 Comments

blast... you have type faster than light speed to get an answer in first on this site! ;-)
No, you just have to stop the time while you're typing your answer. Easy.
There was only a two minute difference - that's 120 seconds.