Marshal
Marshal

Reputation: 1237

Passing strings in c# to a static statement

I am trying to create a SQL database in c# by having my program connect to the server. I am having a problem with creating a login screen. The problem is in order to create a login screen. I am trying to pass 2 strings to the connection class and am unable to do so since the statement is static?

public class sqlconnect
{
    public String us;
    public String pas;

    public SqlConnection myConnection = new SqlConnection("user id=" + (us) + ";" +
                                           "password=" + (pas) + ";server=PANDORA;" +
                                           "Trusted_Connection=yes;" +
                                           "database=NHS; " +
                                           "connection timeout=30");
}

ERROR - A field initializer cannot reference the non-static field, method, or property 

If I try and make "us" or "pas" static I am unable to pass information from my main class to this one. I would be grateful if someone could point out what I'm doing wrong as I am not an expert at C# I must admit. Thank you.

Upvotes: 3

Views: 169

Answers (2)

Jagd
Jagd

Reputation: 7306

// property
public SqlConnection myConnection { get; set; }

// method
public SqlConnection GetSqlConnection()
{
     if (myConnection == null) 
       myConnection = new SqlConnection("user id=" + us + ";" +
                                       "password=" + pas + ";server=PANDORA;" +
                                       "Trusted_Connection=yes;" +
                                       "database=NHS; " +
                                       "connection timeout=30");

     return myConnection;
}

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564423

You can't use variables within a field initializer. A good option here would be to make myConnection a property or (better yet, in this case) a method:

public SqlConnection CreateConnection()
{
     return new SqlConnection("user id=" + us + ";" +
                                       "password=" + pas + ";server=PANDORA;" +
                                       "Trusted_Connection=yes;" +
                                       "database=NHS; " +
                                       "connection timeout=30");
}

This allows you to use your other variables (state) in order to create the value you want.

I would also recommend changing your string values to properties, having a constructor to initialize them property, and using better names. Another alternative would be to have the username and password be parameters to the method, ie:

public SqlConnection CreateConnection(string username, string password)
{
     return new SqlConnection("user id=" + username + ";" +
                                       "password=" + password + ";server=PANDORA;" +
                                       "Trusted_Connection=yes;" +
                                       "database=NHS; " +
                                       "connection timeout=30");
}

This would elimniate the need to store these within your class as state (unless they're used by other routines, as well).

Upvotes: 8

Related Questions