Generate value for base() constructor

R

russ.haley

My question is mostly syntactical but I can't seem to find an answer
for it:

I would like to create a constructor on a derived class that creates
one of the parameters for the base class and then passes it to the base
class constructor as follows:

public abstract class BaseClass
{
private int Param1;
private byte[] Param2;

public BaseClass(int intParam1, byte[] abytParam2)
{
Param1 = intParam1;
Param2 = abytParam2;
}
}


public class MyDerivedClass : BaseClass
{
public MyDerivedClass(int intParam1, byte[] abytParam2):
base(intParam1, abytParam2)
{}

//***** This is where I'm stuck
public MyDerivedClass(int intParam1)

{
//CREATE abytParam2 in this contructor
// and pass it to the base constructor
// for BaseClass(int, byte[])
}

}

This should be easy. ANY suggestions would be great.

Cheers,
Russ
 
J

Jon Skeet [C# MVP]

My question is mostly syntactical but I can't seem to find an answer
for it:

I would like to create a constructor on a derived class that creates
one of the parameters for the base class and then passes it to the base
class constructor as follows:

This should be easy. ANY suggestions would be great.

Write a static method that generates the bytes. For instance:

public MyDerivedClass(int intParam1) : base (intParam1,
GenerateBytes (intParam1))
{
}

static byte[] GenerateBytes(int param)
{
return new byte[param];
}
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

| //***** This is where I'm stuck
| public MyDerivedClass(int intParam1)
|
| {
| //CREATE abytParam2 in this contructor
| // and pass it to the base constructor
| // for BaseClass(int, byte[])
| }
|
| }
|
| This should be easy. ANY suggestions would be great.

Try adding a static function to the derived class and call that in the base
call :

public class MyDerivedClass
{
private static byte[] CreateParam2()
{
return ... // created array
}

public MyDerivedClass(int intParam1) : base(intParam1, CreateParam2())
{
...
}
}

Joanna
 
N

Nick Hounsome

My question is mostly syntactical but I can't seem to find an answer
for it:

I would like to create a constructor on a derived class that creates
one of the parameters for the base class and then passes it to the base
class constructor as follows:

public abstract class BaseClass
{
private int Param1;
private byte[] Param2;

public BaseClass(int intParam1, byte[] abytParam2)
{
Param1 = intParam1;
Param2 = abytParam2;
}
}


public class MyDerivedClass : BaseClass
{
public MyDerivedClass(int intParam1, byte[] abytParam2):
base(intParam1, abytParam2)
{}

//***** This is where I'm stuck
public MyDerivedClass(int intParam1)

{
//CREATE abytParam2 in this contructor
// and pass it to the base constructor
// for BaseClass(int, byte[])
}

}

This should be easy. ANY suggestions would be great.

public MyDerivedClass(int intParam1) : base(intParam1, DefaultByteArray()){}

private static byte[] DefaultByteArray() {....}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top