Should I assign a Field to something? Pros/Cons please?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EARNEST
    New Member
    • Feb 2010
    • 128

    Should I assign a Field to something? Pros/Cons please?

    Hello there.

    Should I assign? Can you please explain why or why not, the reasons, please?

    Warning:
    Code:
    is never assigned to, and will always have its default value null
    Code:
    Code:
        class f_Definition
        {
            string _definitionAcronym;
            string _definitionName;
            double[] _definitionValue = new double[4];
            DefinitionValue _DefValue;
    
            //properties
            public string DefinitionAcronym
            {
                set { _definitionAcronym = value; }
                get { return _definitionAcronym;  }
            }
    
            public string DefinitionName
            {
                get { return _definitionName; }
            }
    in first case, with set, does it assign it automatically? for that field, no warning generated. for the second property, there is a warning.

    Thanks, and sorry if this question is silly
  • peochei
    New Member
    • Oct 2010
    • 12

    #2
    This happens when you declare a variable without assigning any value to it. It is not an error but compiler warns you that you might have forgot doing so.

    You can leave the field _definitionName undeclared, however omitting setter in the property which wraps this variable you have disabled the possibility of assigning a value to this variable. You should consider whether this variable is needed or you can just delete it.

    When designing classes that are just prototypes pieces of application you should consider using auto-implemented properties.

    Comment

    • EARNEST
      New Member
      • Feb 2010
      • 128

      #3
      very helpful. thanks

      Comment

      Working...