3

This may be a very stupid question and it's making feel like my brain has gone...but I cannot fathom how to set a protected or public variable from a Base class.

I have an Enum that is used in the Base class, but must be set in the Inheriting class. The Inheriting class is very little to no code so I SPECIFICALLY want to do this outside of a Method.

An example of what I want:

public class MyBase { protected myEnum; } public class MyClass : MyBase { myEnum = SomeValue; } 

If I set myEnum as a property with get/set, I can override it...but then I end up with many more lines of code.
If I override Page_Load I can access this.myEnum but again, I want to access it outside of a method as it is the only code in the Inheriting class.

Am I missing something completely obvious here?

8
  • Try to assign the instance variable inside a constructor or [other] method. (Read the error message for why it doesn't like the above example syntax.) Alternatively, use a virtual "getter" defined in the inheriting classes or invert it to use events, etc. Using static would likely be ... unwise. Commented Feb 14, 2012 at 23:49
  • @pst : Explain more of this "virtual getter"? I don't want to have to override the entire Property (which would give me 5 lines of code just to assign this value. Commented Feb 14, 2012 at 23:52
  • What makes you think you can't use a constructor? Commented Feb 14, 2012 at 23:54
  • @s_hewitt : Because the class is never instantiated in my code. It's a webpage, so the user just navigates to it. Commented Feb 14, 2012 at 23:55
  • Correct. The constructor will still called, how else would ASP.NET build the page from your class? It has to create an instance of the class at some point. Commented Feb 14, 2012 at 23:57

3 Answers 3

9

You need to create a constructor.
You cannot execute code outside a method.

Sign up to request clarification or add additional context in comments.

5 Comments

This code applies to an aspx page, so the pages aren't instantiated in my code. Hence why I want it in the codebehind.
@JamesP.Wright: You can still create a constructor; that will still work.
Example? You are saying that inside my code-behind I should instantiate a new instance of the class that I am in, just to assign this variable?
Just create the constructor. ASP.NET will use it when appropriate.
Oh, I think I just understood. So I just need public MyClass() { myEnum = SomeValue; }...I was thinking I needed to do var myClass = new MyClass(SomeValue), etc.
4

It is not possible to assign an "instance variable" unless one has (or is in) an instance :-) [C# does allow some compiler magic for fields defined within the current class definition and values assigned in the declaration; this does not expand to arbitrary assignments.]

While I would personally use the constructor of the derived class (or a method guaranteed to run at an appropriate time), one way that can be used to invert this, is to use a virtual getter:

Please note this example access a virtual method in the constructor (see Calling virtual method in base class constructor) which needs to be done with care: (The derived class may be in an "invalid state" insofar as it's constructor body has not run yet.)

class Base { protected virtual MyEnum DefaultEnumValue { get { return 0; } } MyEnum enumValue; Base () { enumValue = DefaultEnumValue; } } class Derived : Base { protected override MyEnum DefaultEnumValue { get { return MyEnum.Derived; } } } 

Once again, note the call to a virtual method, which can be avoided with a little more work in Base (e.g. wrap enumValue in a lazy-load getter itself), but for "shortest code"....

Happy coding.

Comments

-1

Would this work for you?

BasePage.cs (BasePage, derives from Page):

class BasePage : Page { ... private MyEnum _myenum; ... protected MyEnum EnumProperty { get { return _myenum; } set { _myenum = value; } } } 

Page.aspx.cs (derives from BasePage):

class _Page : BasePage { // Nothing here for this ... } 

Page.aspx:

<%@Page ... Inherits="_Page" %> ... <script runat="server">base.EnumProperty = MyEnum.Value;</script> <html> ... </html> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.