Overridable variable members?

G

Guest

I am trying to convert some old code into .NET and this old code uses
internal structures to define the underlying data structure for a class. This
class is then used as a base class for other derived classes. I've worked out
how to get initialized structure variables in classes, but the problem is I
cannot work out how to override the variable in derived classes. I keep
getting 'virtual'/'override' not valid for item & "'new' must be used here"
errors. Is there anyway to do this?
I realise that I can create a virtual function that can be called to init
the variable, but the structures used are pretty large & complex and the
extra time used to construct the init data at runtime is horrific (extra few
seconds at startup), so I'd rather just nut this out.

Sample Code:
public abstract class Base {
protected internal struct BaseData {
public string SomeVariable;
public BaseData(string s) { SomeVariable = s; }
}

protected internal BaseData initData;
}
public class Derived {
protected internal BaseData initData = new BaseData("Derived Class Init
Data");
}

How can I do this?
 
M

Mattias Sjögren

Is there anyway to do this?

Can't you just pass it to a base class constructor?


public abstract class Base {
protected Base(BaseData init) { initData = init; }

protected internal BaseData initData;
}

public class Derived : Base {
public Derived() : base(new BaseData("Derived Class Init Data"))
{}
}




Mattias
 

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