blanket
blanket

Reputation: 79

get/set Accessors the correct scope

i created a class called ProfileHelper, and I can't seem to get my get/set accessors correct; instead, I'm getting red lines on both get and set. Here is the code I am trying to use:

public static String UserName(string columnName)
{ 
    get
    {
        using (SqlConnection cn = new SqlConnection(SiteConfig.ConnectionString))
        {
            string sSql = ("SELECT UserName , LoweredUserName FROM  aspnet_Users  WHERE UserId = @UserId");

            using (SqlCommand cm = new SqlCommand(sSql, cn))
            {
                cm.Parameters.AddWithValue("@UserId", Membership.GetUser().ProviderUserKey.ToString());
                cn.Open();
                using (SqlDataReader rd = cm.ExecuteReader())
                {
                    while (rd.Read())
                    {
                        return columnName;
                    }
                    rd.Close();
                }
                cn.Close();
            }         
        }            
        return columnName;
    }
    set
    {
        using (SqlConnection cn = new SqlConnection(SiteConfig.ConnectionString))
        {
            string sSql = ("UPDATE [aspnet_ Users] SET UserName = @UserName, LoweredUserName = @LoweredUserName WHERE UserId = @UserId");

            using (SqlCommand cm = new SqlCommand(sSql, cn))
            {
                cm.Parameters.AddWithValue("@UserId", Membership.GetUser ().ProviderUserKey.ToString());
                cn.Open();
                cm.ExecuteNonQuery();
                cn.Close();
            }    
        } 
    }
}

Upvotes: 1

Views: 3573

Answers (3)

Matthias Meid
Matthias Meid

Reputation: 12513

Syntax is basically:

private string userName;
public string UserName // no parameter here!
{
  get { return this.userName; }
  set { this.userName = value; } // value is a keyword
}

Alternatively, auto-property do the backing field for you:

public string UserName { get; set; } // equivalent
public string MyExternallyReadOnly { get; private set; } // setter is private

You should not fiddle around with SQL connections in properties. Access to properties should be fast and reliable. Consider replacing it by methods to make it clear that this is actually a longer-enduring action with external dependencies (which is more likely to fail):

public string GetUserName() { }
public void UpdateUserName() {}

Upvotes: 7

Tim S.
Tim S.

Reputation: 56536

You should make a pair of methods, GetUserName(string columnName) and SetUserName(string columnName, string value). This is the preferred approach when it will probably take a while, e.g. because you're making a DB connection, and lets you easily have a parameter.

public static String UserName(string columnName) is the signature for a method. public static String UserName would be a property. Properties have get/set accessors, methods do not. You need to change this. Properties do not allow arguments, except for indexed properties, which can't be static (e.g. used like myObj[someColumnName], declared like public string this[string columnName]). If you're expecting to access the property like var something = ProfileHelper.UserName;, just use a property.

Upvotes: 2

Thinking Sites
Thinking Sites

Reputation: 3542

That's a method, not a property. Only properties have get and set accessors. Pick one.

public static String UserName(string columnName) // this implies a method
     { 
      get // this implies a property
      {

Upvotes: 8

Related Questions