views:

180

answers:

6

I want to assign some default value to a property or want to replace some character like given below. Is it a correct syntax or should i do this by creating a variable.

 public string Login_Name 
        { 
          get 
            { return this.Login_Name; } 
          set { this.Login_Name = value.Replace("'", "''"); } 
        }
+1  A: 

You'll have to set a variable, otherwise you'll end up with infinite recursion and a Stack Overflow. With what you have, your setter is calling itself with:

this.Login_Name = ... 

This is in the same way that your getter is calling itself with:

return this.Login_Name;
lc
+2  A: 

I do it like this.

private string _Login_Name = "Some Default";

public string Login_Name 
{ 
  get { return _Login_Name; } 
  set 
  {  
      _Login_Name = value.Replace("'", "''");  //might want to check for null first
  } 
}
Benny Jobigan
+4  A: 

That won't work; you'd effectively be creating an infinite loop.

Use a separate private field instead:

private string m_loginName;

public string Login_Name 
{ 
    get 
    { 
        return m_loginName; 
    } 
    set 
    {
       m_loginName = !string.IsNullOrEmpty(value) ? value.Replace("'", "''") : value;
    } 
}
Warren
@Warren - This will cause an error if value is null.
GenericTypeTea
indeed -- i was copying the OP's code. :-) I have updated it, thanks.
Warren
@Warren - No worries!
GenericTypeTea
+13  A: 

By accessing Login_Name the get with return Login_Name again leaving you with an infinite loop (StackOverflowException).

You should use properties to get and set private members:

public string Login_Name
{
   get
   {
      return _login_Name;
   }
   set
   {
      _login_Name = value;
      if (!string.IsNullOrEmpty(_login_Name))
      {
         _login_Name = _login_Name.Replace("'", "''");
      }
   }
}
private string _login_Name;

If you meant to use an auto-implemented property, it would look like this:

public virtual string Login_Name {get;set;} // virtual = good practice I think

But auto-implemented properties cannot have any additional logic applied to their gets or sets.

GenericTypeTea
@Mark: Thanks, I removed my comment ...
Hinek
+3  A: 

What you have written is not an auto-implemented property. An auto-implemented property would look like this:

 public string Login_Name { get; set; } 

Here is a quote from MSDN, emphasis mine:

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.

When you have extra logic like in your example you cannot use an auto-implemented property. You can use an ordinary property and declare the backing field yourself.

 private string loginName;

 public string LoginName 
 { 
      get
      {
          return loginName;
      } 

      set
      {
          loginName = (value == null) ? null : value.Replace("'", "''");
      } 
 }
Mark Byers
A: 

It's correct. The only problem in the snippet that you provided is the recursive call of setter method of the property.

set { this.Login_Name = value.Replace("'", "''"); } 

you should set value to some private field rather than recursively to the property itself, for example:

set { loginName = value.Replace("'", "''"); }
bsnote
It is not: 1st it is not an auto-implemented property and 2nd this will crash if value is null
Hinek