Passing string & ldquo; classValue & rdquo; from a C # class to a C # form string & ldquo; formValue & rdquo;


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;


To return a value to another class from a method, you only need to do three things:

  1. Make it public, so the other class can 'see' it.
  2. Add a return type to your method (other than void, which means it doesn't return)
  3. Use the keyword 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() +

Then, on the calling side, you would do something like:

textBox1.Text = GenKeyA(someStringValue);