Passing a number of integers between forms

  • Thread starter Thread starter nickcleary1
  • Start date Start date
N

nickcleary1

I need to pass 4 integers between two forms but am having trouble
passing more than one.I am hoping to use the contructor approach as i
need the variables straight away

Any help would be great

thanks

Nick
 
Tell me the problem, the given explaination is not detail enough but yes you
can pass any number of parameters via a user defined constructor to any
amount of forms :-)

Nirosh.
 
What does your consturctro look like?

I think a good idea would be to pass array of integres to other form, so the
number of integers shouldn't be a problem.
 
I need to get int1 int2 and int3 from

"private void button1_Click(object sender, System.EventArgs e)" in
form1 to

public form2();

In form2 i will need to use each variable in public form2 rather than a
specific object further down the code

i was previously using the contructor tutorial i found here

http://www.codeproject.com/useritems/pass_data_between_forms.asp

but could not work out how to send multiple integers

Im sorry if this is still pretty vague! im still getting the hang of
this!
 
I need to get int1 int2 and int3 from

"private void button1_Click(object sender, System.EventArgs e)" in
form1 to

public form2();

In form2 i will need to use each variable in public form2 rather than a
specific object further down the code

i was previously using the contructor tutorial i found here

http://www.codeproject.com/useritems/pass_data_between_forms.asp

but could not work out how to send multiple integers

Im sorry if this is still pretty vague! im still getting the hang of
this!

So create a constructor for form2 that either takes in four integers, an int
array, or possibly a struct that has the integers as properties. The last
approach gives you the most flexibility in that you can add additional items
to the struct without changing your constructor signature. But use
whichever best fits the needs of your particular problem. Exs:

public Form2()
{
InitializeComponent();
}

// constructor that takes in four ints
public Form2(int val1, int val2, int val3, int val4) : this()
{
// set values based on ints passed in
control1.Value = int1;
control2.Values = int2;
//etc...
}

// constructor that takes an int[]
public Form2(int [] vals) : this()
{
control1.Value = vals[0];
control2.Value = vals[1];
//etc...
}

// create a struct to be used for passing data into the form
// refine to have private fields with public accessors
public struct Form2Data
{
public int val1;
public int val2;
public int val3;
public int val4;
}

// constructor that takes in the data in a struct
public Form2(Form2Data data) : this()
{
control1.Value = data.val1;
control2.Value = data.val2;
//etc...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top