Reputation: 49
I would like to complete a command in C# using a class called GenerateKeys
inside the class there is a function called GenKeyA(classValue);
this is called from the default button1_click
event handler in Visual Studio 13, using the form string variable formValue
like so. GenKeyA(formValue)
, and by doing this i would like to send that returned string to a textBox1.Text
variable. Please correct me if im wrong in any way!.
public class GenerateKeys
/// The Class
{
/// string Variable.
string classValue = "";
/// Called from button1_click
private void GenKeyA (string classValue)
{
Random rand = new Random();
classValue = ""+rand.Next(0,9)+""+rand.Next(0,9)+""+rand.Next(0,9)+"";
/// from here, i wish to return the string classValue to
/// the form1.frmValue string then send that to textBox1.Text = formValue;
}
}
Upvotes: 0
Views: 1496
Reputation: 37070
To return a value to another class from a method, you only need to do three things:
void
, which means it doesn't return)return
to pass an object back to the caller.So, you can change your method like this:
public string GenKeyA (string classValue)
{
Random rand = new Random();
return rand.Next(0,9).ToString() + rand.Next(0,9).ToString() +
rand.Next(0,9).ToString();
}
Then, on the calling side, you would do something like:
textBox1.Text = GenKeyA(someStringValue);
Upvotes: 1
Reputation: 56459
Just make the function return a string
and be public
(or internal
depending on your setup):
public string GenKeyA (string classValue)
Then call it in your form by doing:
form1.frmValue = GenKeyA(classValue);
textBox1.Text = formValue;
Upvotes: 1