Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a base class constructor in derived class after some code block in derived constructor [duplicate]

public class bar
{
   public bar(list<int> id, String x, int size, byte[] bytes)
   {
     ...
   }
}

public class Foo: Bar
{
    public Foo(list<int> id, String x, someEnumType y):
     base(id, x, sizeof(someEnumType), y)
    {
        //some functionality
    }
}

As you see in the above code I need to convert someEnumType to byte array type before calling base class constructor. Is there a way to do it? Something like:

public class Foo: Bar
{
    public Foo(list<int> id, String x, someEnumType y)
    {
        //someEnumType to byte array
        base(...)

    }
} 
like image 568
user2528718 Avatar asked Mar 25 '15 10:03

user2528718


1 Answers

Just create a method in your derived class and call it....

public class bar
{
   public bar(list<int> id, String x, int size, byte[] bytes)
   {
     ...
   }
}

public class Foo: Bar
{
    public Foo(list<int> id, String x, someEnumType y):
     base(id, x, sizeof(someEnumType), Convert(y))
    {
        //some functionality
    }

    public static byte[] Convert(SomeEnumType type)
    {
        // do convert
    }
}
like image 64
puko Avatar answered Sep 28 '22 18:09

puko