asked on
C# property cannot be assigned
Why do I get this message on line
Properties.Settings.Default.TSServer = tsServer;
"Property or index 'TS Server' cannot be assigned to -- it is read only
namespace Time.Tracker
{
public partial class frmLogin : Form
{
string tsServer;
string tsDatabase;
string tsUserID;
string tsPassword;
public frmLogin()
{
InitializeComponent();
}
public string TSServer
{
get { return tsServer; }
}
private void btnLogin_Click(object sender, EventArgs e)
{
tsServer = txtServer.Text.Trim();
tsDatabase = txtDatabase.Text.Trim();
tsUserID = txtUserID.Text.Trim();
tsPassword = txtPassword.Text.Trim();
Properties.Settings.Default.TSServer = tsServer;
}
private void frmLogin_Load(object sender, EventArgs e)
{
txtServer.Text = Properties.Settings.Default.TSServer;
txtDatabase.Text = Properties.Settings.Default.TSDatabase;
}
}
}
ASKER
namespace Time.Tracker
{
public partial class frmLogin : Form
{
string tsServer;
string tsDatabase;
string tsUserID;
string tsPassword;
public frmLogin()
{
InitializeComponent();
}
public string TSServer
{
get { return tsServer; }
set { tsServer = value; }
}
private void btnLogin_Click(object sender, EventArgs e)
{
tsServer = txtServer.Text.Trim();
tsDatabase = txtDatabase.Text.Trim();
tsUserID = txtUserID.Text.Trim();
tsPassword = txtPassword.Text.Trim();
Properties.Settings.Default.TSServer = tsServer;
}
private void frmLogin_Load(object sender, EventArgs e)
{
txtServer.Text = Properties.Settings.Default.TSServer;
txtDatabase.Text = Properties.Settings.Default.TSDatabase;
}
}
}
Do you mean it fails here
Properties.Settings.Default.TSServer = tsServer;
If yes then check what Properties.Settings.Default.TSServer is
ASKER
Properties.Settings.Default.TSServer = tsServer;
...if TSServer is an Application-scope setting. The Application scope is read-only at runtime, while User scope can be updated.
However it looks like you are having a duplicate with the same named property of your login form as one of your settings in properties. If they really are the same information then one of them is redundant and can lead to difficult to find bugs (one gets modified and then the other is used)
ASKER
In the properties section there is a property called TSServer
I then have a class called Globals that contains a global variable called TSServer
I then also use a vraiable called tsServer
The user may want to change the database name after launching the program so the properties version is there simply as a default.
What would be the clearest way to name these variables?
That way, if a user changes their database name or any other settings, you can call Properties.Settings.Default.Save() to save the changes locally so they persist at the next load.
ASKER
What I was suggesting was that I presumed you would have a configuration screen somewhere in your application, where the user could put in new settings and test them. If they worked, they could click a save button in that screen.
From there, you would just update the Properties.Settings.Default.<variable name> value and then call Properties.Settings.Default.Save() after you've updated them all. The guts of that Save() call will take care of everything for you - it will save the new values to a separate user config file that's stored within your appdata folder. For the sake of being thorough, it will save it to the following path (assuming your app is called TimeTracker.exe and your app's version is 1.0.0.0):
C:\Users\Username\AppData\Local\TimeTracker\TimeTracker .exe_<some chars>\1.0.0.0\user.config
The next time the user loads up the app, it'll automatically pull in the values from user.config and use those.
My initial thoughts when planning something.
Think of 3 layers. Each layer has a general purpose. Data. Business. User.
Layer 1. Data. This is to obtain data from permanent storage and to save data back to that storage.
Layer 2. Business. This queries the data layer for the individual items, assembles data into the information requested, dissembles user input and passes the info back to the data layer. (It is not to make a simple copy of eg. a customer name. It is the postman delivering the letters).
Layer 3. User. This is the user input/output level where the user requests information or inputs information. The layer passes information to/from the business layer.
eg. The user wants to know all the orders for a customer this year. The user layer provides an interface to allow the user to enter the selections. It then requests the info from the business layer. That then requests from the data layer the info concerning the customer, the orders, the articles on the order, the monies paid/due.... The data layer then retrieves from one (or more) storages the necessary information.
Why. Change from a file storage to a database storage - business and user layers aren't interested in where and how the data is stored. Changes from one reporting tool to another - business and data layers don't come into that equation. You can totally rework the GUI from a winforms to a browser based solution - business and data layers are unchanged.
For some apps this might well be overkill (or too simple). It does however give you a start in the overall framework.
ASKER
{
get { return tsServer; }
set { tsServer = value; }
}
You are missing a 'setter' which is why it appears to be readonly