【发布时间】:2013-01-16 18:24:23
【问题描述】:
我完全停留在创建随机数上。理想情况下,不是客户编号的两倍。
然后我需要在我的 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);
【问题讨论】:
-
你为什么不用数据库?
-
由于任务要求设置。在理想的世界里,我猜数据库会更好。
-
如果随机数的使用不是那么错误的话,如何不使用
new Random将会有很多问题。随机数永远不能保证是唯一的,因此您可能正在寻找其他东西...... -
为什么数字必须是随机的而不是顺序的?
-
嗯,它没有,我猜只是独一无二的。