2

I am using sqlite in my app. I am declaring my sqliteConnection as constant. But the problem is that the user of this class can change the connectionString property of this sqliteConnection object. How I can prevent that? I want the connection string of the connection to be set just once.

4
  • 1
    if its declared with const keyword, it will be readonly by default. Commented Mar 17, 2013 at 7:02
  • 1
    By "user" you mean another developer with access to the code, or something else? If its a const it can't be changed in the code anymore. Commented Mar 17, 2013 at 7:03
  • I am referring to any developer who will use my class. Commented Mar 17, 2013 at 7:07
  • @VictorMukherjee - why not create a read-only property ? only with getter and no setter. Commented Mar 17, 2013 at 7:09

1 Answer 1

1

You can create a read-only property...

private string connectionString = "my_string";
public string ConnectionString
{
    get { return connectionString; }
}

Or a readonly field. The value can be set only in the constructor or as a constant:

public readonly ConnectionString = "my_string";

Or a const field. The value can be set as a constant (value is static, i.e. there will only be one value for all instances):

public const ConnectionString = "my_string";
Sign up to request clarification or add additional context in comments.

1 Comment

Pretty sure you can't do readonly and const on one field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.