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?

James P. Wright
  • 8,991
  • 23
  • 79
  • 142
  • 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. –  Feb 14 '12 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. – James P. Wright Feb 14 '12 at 23:52
  • What makes you think you can't use a constructor? – s_hewitt Feb 14 '12 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. – James P. Wright Feb 14 '12 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. – s_hewitt Feb 14 '12 at 23:57
  • It's not clear why can't you create a constructor or set value in PreInit event handler in your child page. It's also not clear what you mean by "outside of a method". – Evgeny Lukashevich Feb 15 '12 at 00:01
  • See this question for more information: http://stackoverflow.com/questions/2824967/overriding-page-class-constructor-in-asp-net-code-behind-file-when-is-it-call – s_hewitt Feb 15 '12 at 00:06
  • For everyone asking why I can't use a Constructor...it's because I was having a brain fart...I can use a Constructor...and I probably will....however it DOES seem like I should just be able to assign the property/variable outside of a Method... – James P. Wright Feb 15 '12 at 00:09

3 Answers3

9

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
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.

Community
  • 1
  • 1
-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>
xxbbcc
  • 16,930
  • 5
  • 50
  • 83