Reputation: 13
I have a Window Form with textboxes,checkboxes,comboboxes and buttons.I have another separate class which updates data in a database using the values of the Orignal WinForm elements.I want to access the properties(text,checked etc) of some or all of the feilds of the form elements in this class.The Problem is-
1.if i use the parameterised function call then the parameters list become too large. eg-function(int a,bool c,string d,int e,int f,bool e,bool h,string g) This is working but looks very untidy and i don't know whether this is a good programming methodology.
2.I can use static variables but then i have to create a separate function which updates the static variable's value. like index change in combobox.(but People say avoid static..)
3.Also i dont want to use instance objects of the main form class as then i would have to create object of the original form class repeatedly at many places.(Which is obviously very resource expensive).. So my question is which technique to use on the account of good programming methods???or there is a better way to do it...
Upvotes: 1
Views: 10262
Reputation: 9536
1) You can group all parameters into a class or struct and pass its instance to a function which updates GUI control.
2) Don't use static variables for this - you'd make this arguments global which is not what you want to do.
3) You are not creating a new instance of MyForm
class if you're passing and using its reference in your DB client class:
// class with all data you want to use to update GUI controls
class MyData
{
public string Text1 { get; set; }
public string Text2 { get; set; }
...
}
// class which obtains data from a DB
class DBClient
{
MyForm myForm;
DBClient(MyForm myForm)
{
this.myForm = myForm; // you're just passing a reference to MyForm here, you're not creating a new object
}
void UpdateFormControls()
{
MyData data = ...; // fill it with data obtained from from DB
myForm.UpdateControls(data);
}
}
// your custom form containing various controls
class MyForm : Form
{
DBClient dbClient;
MyForm()
{
dbClient = new DBClient(this);
}
public void UpdateControls(MyData data)
{
if (InvokeRequired)
{
this.BeginInvoke((MethodInvoker) delegate { UpdateControls(data); });
return;
}
control1.Text = data.Text1;
control2.Text = data.Text2;
...
}
}
Even better would be if you would decouple DBClient
and MyForm
by making MyForm
implement interface and then using interface reference in DBClient
class.
Upvotes: 0
Reputation: 7351
Better you can go with Public properties using get; set;
for each of the control & use these properties to access the form data in another class.
public partial class MainClass : Form
{
public MainClass()
{
InitializeComponent();
}
public string UserName
{
get { return textBox1.Text }
set { textBox1.Text = value; }
}
public bool IsChecked
{
get { return checkBox1.Checked; }
set { checkBox1.Checked = value; }
}
}
public class AnotherClass
{
public void MyFunc()
{
MainClass obj = new MainClass();
obj.UserName = "SomeUser1";
obj.IsChecked = true;
}
}
Upvotes: 2
Reputation: 56
Ideally you should create a class or a struct containing the fields that you need to update into the database. Now update the members of of the instance of this class with appropriate values and pass this instance as a paramter to the class responsible for updating the database. This is a more cleaner solution than having a long paramter list.
Upvotes: 0
Reputation: 17600
Maybe create class
with needed fields, update it values and pass it between form and data manager. Also you can bind controls to object
so values will be updated in controls automatically.
Upvotes: 0