2

I am coming over from the Java world and this construct is driving me nuts:

    private static string _muiUrl;

    private static string MUIUrl
    {
        get
        {
            if (String.IsNullOrEmpty(_muiUrl))
            {
                using (var db = new IntLMPDB())
                {
                    _muiUrl =
                        (from c in db.Control where c.ControlKey == "MUI_Url" select c.ControlValue).FirstOrDefault();
                }
            }
            return _muiUrl;
        }
    }

When I see that in a class and I want to use that property in the class itself, how should I call it?

Paul Duer
  • 1,100
  • 1
  • 13
  • 32

5 Answers5

3

Simply as follows:

var result = TypeWithMUIURLProperty.MUIUrl;

You can omit the class name as mentioned by others, however, for explicitness I will leave it in this example.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • 1
    -1 It's a static property. You can't access static properties using the `this` keyword. – Daniel T. Feb 17 '11 at 18:50
  • Thanks; though I should have contemplated the dangers of haste in answering. We live and learn. :) – Grant Thomas Feb 17 '11 at 18:56
  • as `MUIUrl` is *private*, this call will fail from outside the class! – Matten Feb 17 '11 at 18:58
  • There is only one copy to access regardless of how you qualify it - this works, definitely not worth the -1. – Grant Thomas Feb 17 '11 at 19:02
  • As `MUIUrl` is _private_ then ANY call to it from outside the class will fail - what's your point, sorry? – Grant Thomas Feb 17 '11 at 19:07
  • @Mr. Disappointment -- sorry for the typo in my comment. The property is private, but your answer reads like it is possible to access it from outside as you preceed it with `TypeWithMUIURLProperty`, but in the sense of the question your answer is 100 percent correct. – Matten Feb 17 '11 at 19:08
  • It's not the fact you preceeded it with the class name, but `TypeWithMUIURLProperty` makes me think you can type this in `TypeWithoutMUIURLProperty` :) I would like to take off the -1 but SO does not allow this unless you edit your question o_O – Matten Feb 17 '11 at 19:09
1

Inside the class, you do not need to qualify the property name at all, i.e. you write just

string url = MUIUrl;

Had the property been something “better” than just private, to access the property from a completely different class, you would need to qualify it using the class name (but you are allowed to do even inside the class, it is just not required), i.e.

string url = ThatClassYouDidNotName.MUIUrl;

(Side remark: this variant of lazy-initialization is not generally thread-safe.)

Mormegil
  • 7,955
  • 4
  • 42
  • 77
0

From within the class, just writing MUIUrl should do fine. That would pull an initialized value, and force the lazy instantiation.

If it were public and you were accessing it from somewhere else you would need to write YourClassName.MUIUrl. But since it is private, this is unnecessary.

Also, simply using MUIUrl will work from within the instance or other static members.

smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • +1 to neutralize the -1 somebody gave you as the answer is correct :) The last sentence is redundant. – Matten Feb 17 '11 at 19:00
  • I was just trying to make it clear that you can access private static properties from within an instance or a static member. By contrast, you can't access a private instance property from a static member. This is implicit in the definition of a static member, but I wanted to make sure you had all the information you need. – smartcaveman Feb 17 '11 at 19:07
0

When I implement a self loading property that has a private backing store I follow the convention of never interact with the backing store, always read from the property.

I find it stupid that you need the private field, I feel this type of code should be inherit inside of {get; set;} that you should have implicit access to a field of the type of the property whether you use it directly, or fall back to the default auto-property wiring.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
0

if the construct is driving you nuts, here's some sort of explanation as comment

private static string _muiUrl;
public static string MUIUrl
{
    get
    {
        //if the variable is null or empty
        if (String.IsNullOrEmpty(_muiUrl))
        {
            //using defines a scope, outside of which an object or objects will be disposed.
            //http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx
            using (var db = new IntLMPDB())
            {
                //this is linq
                //a simple example will be 
                //http://stackoverflow.com/questions/214500/which-linq-syntax-do-you-prefer-fluent-or-query-expression
                _muiUrl = (from c in db.Control 
                            where c.ControlKey == "MUI_Url" 
                            select c.ControlValue
                          ).FirstOrDefault();
                //or another linq syntax will be
                //_muiUrl= db.Control
                //        .Where(c => c.ControlKey == "MUI_Url")
                //        .FirstOrDefault()
                //        .ControlValue;
            }
        }
        return _muiUrl;
    }
}

Have deliberately given the property's access modifier as public since its how you call it outside class(feel free to make it private if its so).

Now call it as,

ClassName.MUIUrl
naveen
  • 53,448
  • 46
  • 161
  • 251