Reputation: 3
I am able to pass one variable to a second form from a first main form using the code below: Form 1:
public void PJRating_Click(object sender, RoutedEventArgs e)
{
//This block reads in user text box submissions
mys = Convert.ToDouble(MYS.Text);
tubeOD = Convert.ToDouble(TubeOD.Text);
tubeID = Convert.ToDouble(TubeID.Text);
tjOD = Convert.ToDouble(TJOD.Text);
adjTens = ((Math.Pow(((((tubeOD - tubeID) * 0.95) + tubeID)), 2) - Math.Pow(tubeID, 2)) * (Math.PI / 4)) * (mys * Math.Pow(10, 3));
//Diplay the new window
PupJointRating newTensileRating = new PupJointRating(adjTens);
newTensileRating.Show();
}
Form 2:
public PupJointRating(double adjTens)
{
InitializeComponent();
Convert.ToString(adjTens);
string tensCap = String.Format("{0:N0}", adjTens);
DisplayTensCap.Text = tensCap;
}
This code works for displaying the "adjTens" variable from the first form on the second form. However, there are other variables from this first form that I want to display on the second form and I cannot figure out how to do this. Any help would be much appreciated.
Upvotes: 0
Views: 69
Reputation: 1446
Better way is to declare some data transfer class, for instance:
public class SecondFormParams
{
public string ArgumentOne {get;set;}
public string ArgumentTwo {get;set;}
}
Add this as argument for second forms:
public PupJointRating(SecondFormParams secondFormParams)
{
InitializeComponent();
string tensCap = String.Format("{0:N0}", secondFormParams.ArgumentOne);
DisplayTensCap.Text = tensCap;
//Do something with other params, like: secondFormParams.ArgumentTwo
}
And populate this object with params and then show the second form:
public void PJRating_Click(object sender, RoutedEventArgs e)
{
// Init your transfer object
var sfp = new SecondFormParams();
//This block reads in user text box submissions
mys = Convert.ToDouble(MYS.Text);
tubeOD = Convert.ToDouble(TubeOD.Text);
tubeID = Convert.ToDouble(TubeID.Text);
tjOD = Convert.ToDouble(TJOD.Text);
sfp.ArgumentOne = ((Math.Pow(((((tubeOD - tubeID) * 0.95) + tubeID)), 2) - Math.Pow(tubeID, 2)) * (Math.PI / 4)) * (mys * Math.Pow(10, 3));
sfp.ArgumentTwo = "Yo man, do something with me on the next form";
//Diplay the new window
PupJointRating newTensileRating = new PupJointRating(sfp);
newTensileRating.Show();
}
Using this approach you can easily define some additional properties for transfer object and pass this without changing arguments for second form.
Upvotes: 0
Reputation: 4104
You have two options. You could expand your current Form2
constructor like so:
public PupJointRating(double adjTens, double secondParam)
{
InitializeComponent();
Convert.ToString(adjTens);
string tensCap = String.Format("{0:N0}", adjTens);
DisplayTensCap.Text = tensCap;
//do something with secondParam
}
And then in your first form you'd do something like this:
PupJointRating pjr = new PupJointRating(myDoubleVar, myOtherDoubleVar);
Or you can simply just add a second constructor with additional arguments as described above. Doing this gives you the option of creating a PupJointRating
object with either just one single argument passed to it, or two:
var pjr1 = new PupJointRating(myDoubleVar);
var pjr2 = new PupJointRating(myDoubleVar, myOtherDoubleVar);
Both of those would then be valid lines of code and give you an object.
This is called member overloading and is both very common, and very useful.
Upvotes: 1
Reputation: 66
You can add more arguments to the constructor of Form2. For example:
public PupJointRating(double adjTens, int arg1, string arg2)
{
InitializeComponent();
Convert.ToString(adjTens);
string tensCap = String.Format("{0:N0}", adjTens);
DisplayTensCap.Text = tensCap;
//Do something with arg1, arg2
}
Then call it by passing additional arguments:
int myInt= 0;
string myString = string.Empty;
//...
//Diplay the new window
PupJointRating newTensileRating = new PupJointRating(adjTens, myInt, myString);
newTensileRating.Show();
Upvotes: 3