Home All Groups Group Topic Archive Search About

Overridable variable members?

Author
29 Nov 2004 4:21 AM
Gary K

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?

Author
29 Nov 2004 7:00 AM
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

--
Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Bookmark and Share