Overridable variable members?

  • Thread starter Thread starter Guest
  • Start date Start date
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?
 
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
 
Back
Top