value from public Variable or public Component Visual in C#

How to get value from public variable if the variable in different fro

Page 1 of 1

3 Replies - 3488 Views - Last Post: 07 November 2008 - 12:25 AM Rate Topic: -----

#1 michaeltjia  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 04-November 08

value from public Variable or public Component Visual in C#

Post icon  Posted 06 November 2008 - 07:18 PM

Hi guys,

I want to ask you about public variable,

the case is, if i have 2 form in my application, and in form 1, i declared public variable or public component visual C#, and i doing something with the variable, for an example, i give a variable value :

class TestVariable
{
public int a;

private void Test()
{
	if (checkbox1.checked == true)
   {
		a = 10;
	}

}
}



i give a value in variable a in form 1, and after that i go to form 2, and if i want to use the value in variable a, which is the value is 10, how can i get the value without change to default value? because i can't get value 10 in variable a, can you help me?

this is my code to access variable value in form 1 :


int b;

TestVariable form = new TestVariable();
b = form.a;



This post has been edited by michaeltjia: 06 November 2008 - 07:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: value from public Variable or public Component Visual in C#

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: value from public Variable or public Component Visual in C#

Posted 06 November 2008 - 07:46 PM

If I understand your problem, you need to add a property to form1 that returns the current
value of "a".

public partial class Form1: Form
{
    private int a = 0;

    public Form1()
    {
          InitializeComponent();
    }
    // Property to get/set a's value
    public int ValueOfA
   {
        get { return a; }
        set { a = value; }
    }
...
    private void Test()
   {
        if (checkbox1.checked == true)
        {
            a = 10;
        }
   }
...
}      


Then in form2
int b;

TestVariable form = new TestVariable();
b = form.ValueOfA();



Was This Post Helpful? 0
  • +
  • -

#3 michaeltjia  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 04-November 08

Re: value from public Variable or public Component Visual in C#

Posted 06 November 2008 - 10:27 PM

Hi,

Sorry but it still can't work, ok i will tell you more detail,

I have 2 form, i want to pass value in checkbox in form1 into form2, but still i can't passed the value, can you explain to me and put some code in here please?

Thanks and Best Regards,

Michael
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: value from public Variable or public Component Visual in C#

Posted 07 November 2008 - 12:25 AM

Ok. Create a new project with just the default blank form. On that form drop a checkbox. Select the
checkbox and look at the properties of the checkbox. On the properties pane select the events list
and double-click the CheckedChanged event. Insert this code in the event method.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
      Form2 frm2 = new Form2();
      frm2.Message = checkBox1.Text;
      frm2.ShowDialog();            
}



Open the Solutions Explorer pane and add a second form, form2. Drop a text box onto the form.
Change the code in the Form2.cs file to the following.
    public partial class Form2 : Form
    {
        public string Message
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        public Form2()
        {
            InitializeComponent();
        }
    }


When built and run, click the checkbox. The second form will load and it's textbox will display
the checkbox text of form1.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1