我有这个令人沮丧的问题,我似乎无法解决。
我尝试使用公共静态字符串中的文本填充TextBox
。
但是当我运行程序时,它只显示一个空白文本框,里面没有任何内容。
我没有任何错误,所以我很难理解我做错了什么。
这是我的代码:
public ShowMp3()
{
InitializeComponent();
OverzichttxtBox.Text = OverzichtMP3();
}
public static String OverzichtMP3()
{
String overzicht = "";
foreach (Mp3Player player in Mp3.GetPlayers())
overzicht = overzicht + "ID: " + Convert.ToString(player.id) + "\r\n" +
"Merk: " + player.make + "\r\n" + "Model: " + player.model +
"\r\n" + "MB-size: " + player.mBSize + "\r\n" + "Prijs: " +
player.price + "\r\n" + "\r\n";
return overzicht;
}
Mp3.GetPlayers()
就是这样:
private static ArrayList players = new ArrayList();
public static void Initialize()
{
Mp3Player player1 = new Mp3Player(1, "GET Technologies .inc", "HF 410", 4096, 129.95M, 500);
Mp3Player player2 = new Mp3Player(2, "Far & Loud", "XM 600", 8192, 224.95M, 500);
Mp3Player player3 = new Mp3Player(3, "Innotivative ", "Z3", 512, 79.95M, 500);
Mp3Player player4 = new Mp3Player(4, "Resistance S.A.", "3001", 4096, 124.95M, 500);
Mp3Player player5 = new Mp3Player(5, "CBA", "NXT Volume", 2048, 159.05M, 500);
players.Add(player1);
players.Add(player2);
players.Add(player3);
players.Add(player4);
players.Add(player5);
}
public static ArrayList GetPlayers()
{
return players;
}
答案 0 :(得分:3)
我怀疑问题是你永远不会打电话给Mp3.Initialize()
。您可以将其添加到Mp3
类中的静态构造函数:
private static List<Mp3Player> players = new List<Mp3Player>();
static Mp3()
{
Initialize();
}
// This can be private now...
private static void Initialize()
{
....
请注意,您也可以将ArrayList
更改为List<Mp3Player>
。
答案 1 :(得分:1)
另外,另外,您应该尝试StringBuilder
和String.Format
。而不是......
overzicht = overzicht + "ID: " + Convert.ToString(player.id) + "\r\n" +
"Merk: " + player.make + "\r\n" + "Model: " + player.model +
"\r\n" + "MB-size: " + player.mBSize + "\r\n" + "Prijs: " +
player.price + "\r\n" + "\r\n";
你可以这样做:
StringBuilder overzicht = new StringBuilder();
overzicht.AppendLine(String.Format("ID: {0}", player.id));
overzicht.AppendLine(String.Format("Merk: {0}", player.make));
overzicht.AppendLine(String.Format("Model: {0}", player.model));
overzicht.AppendLine(String.Format("MB-size: {0}", player.mBSize));
overzicht.AppendLine(String.Format("Prijs: {0}", player.price));
return overzicht.ToString();
更容易阅读;)
我的String.Format在这里并不那么引人注目......但如果这一切都在一条线上,那就更有用了:
return String.Format("ID:{0}, Merk:{1}, Model:{2}, MB-size:{3}, Prijs:{4}",
player.id, player.make, player.mode,
player.mBSize, player.price);