-4

I have a function in my form that generates a random string. I want to show the result of this function in both a message box and a text box at the same time. How to? Should my function be public or private?

Here is my function:

    public string GenerateRandomCode(int myLength)
    {
        string charPool = "ABCDEFGOPQRSTUVWXY1234567890ZabcdefghijklmHIJKLMNnopqrstuvwxyz";

        StringBuilder rs = new StringBuilder();

        Random random = new Random();
        for (int i = 0; i < myLength; i++)
        {
            rs.Append(charPool[(int)(random.NextDouble() * charPool.Length)]);
        }

        return rs.ToString();
    }
Cullub
  • 2,901
  • 3
  • 30
  • 47
Jem
  • 21
  • 1
  • 2
  • 3
  • I just figured out that, by "public string", they're referring to the first part of the method signature. – Douglas Jul 13 '13 at 01:58
  • 2
    @Jem: You cannot return a string from a "private void", since "void" means that you don't return anything. You could, however, use a "private void" method to assign a string to a field or property, and then read it from there. – Douglas Jul 13 '13 at 02:00
  • @Douglas Okay, thank you. I apologize that the question wasn't clear, just wasn't sure how to ask it. I'm to the the C#, but I enjoy it. – Jem Jul 13 '13 at 02:05
  • I edited your question to make it more clear. If it doesn't still ask your question, tell me, and I'll fix it. – Cullub Aug 19 '14 at 21:09

2 Answers2

2

From what I understood, you want the text of a textbox to show the result of GenerateRandomCode and show that value in a MessageBox, too. You can do that like so:

int length = 10;
string msg;



private void button1_Click(object sender, EventArgs e)
{
    msg = GenerateRandomCode(length);
    textBox1.Text = msg;
    MessageBox.Show(msg);

}

Random random = new Random();

public string GenerateRandomCode(int length)
{
        string charPool = "ABCDEFGOPQRSTUVWXY1234567890ZabcdefghijklmHIJKLMNnopqrstuvwxyz";
        StringBuilder rs = new StringBuilder();
        /*Random random = new Random();*/

        for (int i = 0; i < length; i++)
        {
            rs.Append(charPool[(int)(random.NextDouble() * charPool.Length)]);
        }
        return rs.ToString();
 }
pcnThird
  • 2,362
  • 2
  • 19
  • 33
  • Yes, thank you. I was also trying to ask if you create other stuff in a private void can this also be applied like this? Sorry if it still doesn't make sense. – Jem Jul 13 '13 at 01:57
  • Void does *not* return a value. But it is possible to show output like `MessageBox.Show(x)` within a void method. – pcnThird Jul 13 '13 at 02:02
  • you can pass a value by ref to a void still – Robert Hoffmann Jul 13 '13 at 02:03
  • Why are you passing length by ref ? I don't see it being modified anywhere. You usually use ref when you don't have a return value, or if you want multiple return values like with TryParse() – Robert Hoffmann Jul 13 '13 at 02:06
  • @Robert, I passed it by ref to keep a consistency with the OP's code. But you're right; it's not needed for this example. I'll edit that. – pcnThird Jul 13 '13 at 02:09
  • +0: while answering the question itself this code shows bad practice of using `Random` - http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number – Alexei Levenkov Jul 13 '13 at 02:10
  • @AlexeiLevenkov the new Random() is not in the loop ..wheres the problem ? Sure he could put it in a static var to go further, but as long as it's not in the loop, there no problem here. – Robert Hoffmann Jul 13 '13 at 02:12
  • 1
    @Robert it the code that will be written tomorrow - which will use the function to generate values of 5 controls at the same time (or something similar). Code samples are expected to be unusable, but doing things that are known to behave completely wrong is simply not nice (that why it is +0 and not -1 :)). – Alexei Levenkov Jul 13 '13 at 02:15
  • @Alexei Levenkov: After reading http://csharpindepth.com/Articles/Chapter12/Random.aspx, I used only one instance of Random to reduce the likelihood of generating the same numbers. Any thing else that can be improved? Thread-safety, maybe? – pcnThird Jul 13 '13 at 03:05
  • +1. thanks for the edit. There are plenty ways to write this code, but you version is straightforward to be used by beginner. I.e. something basic like using `Random.Next(int)`, and fancy like LINQ `String.Join("", Enumerable.Repeat(1, length).Select(_=> charPool[random.Next(charPool.Length)]))` – Alexei Levenkov Jul 13 '13 at 04:23
0

As I understood it, you want to show it in a message box and text box. To show in MessageBox, you will invoke the method and pass the returned value to MessageBox, like this:

string Result = GenerateRandomCode(6);
MessageBox(Result);

To display it in a TextBox you can simply create it on your form(I don't know what type of form you are using) and assign Result to it:

TextBox textResult = new TextBox();
textResult.Text = Result;

Private methods can only be called by the class which owns the method. They are used without creating the object of the class. And Yes (!) you can do it all with a Private method as well.

Cullub
  • 2,901
  • 3
  • 30
  • 47
Shaharyar
  • 12,254
  • 4
  • 46
  • 66