1

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...

Rohit
  • 13
  • 1
  • 4

4 Answers4

1

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;
        }
    }
Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
0

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.

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
  • but then i have to data bind or something so that the values get updated whenever somthing changes..like index changed of combobox,textchanged of textbox...I posted this question because i thought there would be a more precise way to to do it... – Rohit May 15 '13 at 09:52
  • You can bind any public property of your control to any source of data by using Windows Forms Data Binding: http://msdn.microsoft.com/en-us/library/ef2xyb33.aspx – Bojan Komazec May 15 '13 at 10:08
  • For a simple example, please look at this question: http://stackoverflow.com/questions/115328/how-can-i-do-databinding-in-c – Bojan Komazec May 15 '13 at 10:15
0

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.

gzaxx
  • 17,312
  • 2
  • 36
  • 54
  • Data bind is what i think i would do i guess...But maybe someone may come up with some innovative technique as this scenario arrives to every person. – Rohit May 15 '13 at 09:56
0

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.

Amar
  • 56
  • 2
  • the questions is updated now..sorry for late update but cant use instances..i have many classes which take values from the orignal winform class.. – Rohit May 15 '13 at 09:48
  • Your suggestion seems effective to me now.. :) – Rohit May 15 '13 at 10:01