0

ここで乱数を作成することに完全に行き詰まっています。理想的には、顧客番号と同じ番号を 2 回使用しないでください。

次に、それを tb_id (テキスト ボックス) に表示し、その行を顧客ファイルに書き込む必要があります。

ここで何か助けていただければ幸いです。

private void button1_Click(object sender, EventArgs e)
{    
    **var rng = new Random();**
    // Here I need to write the random number (it's a customer number) to the tb_id text box. so I'm able to write it to their customer file afterwards.

    try
    {
        string fileName = string.Format(tb_surname.Text);

        if (tb_firstname.Text == "" || fileName == "" || tb_postcode.Text == "")
        {
            MessageBox.Show("Missing values from textboxes!");
        }
        else if (File.Exists(fileName + "Text"))
        {
            if (MessageBox.Show("Warning: There is already a file with the surname you enter already on the system. Contents will be replaced - do you want to continue?", "File Demo", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                 MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
            {
                //write lines of text to file
                StreamWriter outputStream = File.CreateText(fileName + ".Txt");
                outputStream.WriteLine(tb_firstname.Text);
                outputStream.WriteLine(tb_surname.Text);
                outputStream.WriteLine(tb_surname.Text);
                **outputStream.WriteLine(tb_id.Text);**
                outputStream.Close();
                MessageBox.Show("Text saved to file successfully!");
                this.Close();
            }
        }
    }
    // ...
}

パート2

       // creating a random number here for the customer id
        Guid g = Guid.NewGuid();
        tb_id.Text = g.ToString();

Guid 番号をテキスト ファイルに書き込みます。

        outputStream.WriteLine(tb_id.Text);
4

3 に答える 3

7

ユーザーに固有のものを取得したい場合、乱数を使用することはお勧めしません。同じ番号を持つ別のユーザーを取得する可能性が高くなります。

GUIDはおそらくより良い考えです

  Guid g = Guid.NewGuid();
于 2013-01-16T18:29:37.097 に答える
1

まず、乱数を使用してユーザーを識別している場合は、COLD TOLD のように GUID を使用します。

ユニークランダムは全くの別物


しかし、乱数の使い方を理解するために...

rng型として宣言するRandom

実際に乱数を作成するには、次のように入力します

int randI =  ng.Next(0, 1000);

これにより、0 から 1000 までの乱数が得られます。

Intellience を使用して、乱数に関して他にどのようなオプションがあるかを確認できます。

その番号をテキストボックスに入力するよりも

textBox1.Text = randI.ToString();
于 2013-01-16T18:29:35.213 に答える
1

Guid構造を使用できます。GUID はグローバルに一意の識別子であり、.NET フレームワークで提供されます。

 this.iD = Guid.NewGuid().ToString();
于 2013-01-16T18:29:57.207 に答える