第一篇文章,我对C#和一般编程都很陌生。
我正在制作一个足球运动员生成器应用程序,仅用于练习目的,其中我有一个带有构造函数的类,该构造函数生成一个具有两个字符串(名字和姓氏)和大量整数的对象。 (不同的技能属性,如射击力,传球等。
忍受我,我会在一秒钟内发布我的代码。
所以我希望所有这些对象都以某种方式存储所有变量。经过大量的谷歌搜索,我可以阅读不同种类,列表和词典的数组。
首先,这是我的班级:
class player
{
public string firstName;
public string lastName;
public string playPos;
public int playerId = 999;
public int isKeeper;
public int level;
public int age;
public int dKeeper;
public int dTackle;
public int dMarking;
public int mPlaymaking;
public int mCrossing;
public int fShooting;
public int fShotPower;
public int aDribbling;
public int aHeading;
public int aPassing;
public void autoGenPlayer()
{
//new killing random that i got off of Stack Overflow
Random rAG = new Random(Guid.NewGuid().GetHashCode());
//PlayerId, dont mind it does not work properly yet.
playerId = playerId + 1;
//a level integer just to base the other stats around, to keep the stats from spreading too much
level = rAG.Next(2, 11);
//Generating names, calling the methods a bit lower in this class.
firstName = firstNameGenerator();
lastName = lastNameGenerator();
//giving the player an age
age = rAG.Next(16, 35);
//a one out of seven chance to become a goal keeper.
isKeeper = rAG.Next(1,8);
//skills based on level. Max value is 20
dTackle = rAG.Next(level, level * 2);
dMarking = rAG.Next(level, level * 2);
mPlaymaking = rAG.Next(level, level * 2);
mCrossing = rAG.Next(level, level * 2);
fShooting = rAG.Next(level, level * 2);
fShotPower = rAG.Next(level, level * 2);
aDribbling = rAG.Next(level, level * 2);
aHeading = rAG.Next(level, level * 2);
aPassing = rAG.Next(level, level * 2);
//Checks if the player is a keeper. 5 is chosen at random. There are obv better ways to do this, but it doesnt matter right now.
if (isKeeper == 5)
{
//This basically just makes the keeper a keeper, and a shit outfield player.
dKeeper = rAG.Next(level, level * 2);
playPos = "Goal Keeper";
dTackle = rAG.Next(1, 3);
dMarking = rAG.Next(1, 3);
mPlaymaking = rAG.Next(1, 3);
mCrossing = rAG.Next(1, 3);
fShooting = rAG.Next(1, 3);
fShotPower = rAG.Next(1, 3);
aDribbling = rAG.Next(1, 3);
aHeading = rAG.Next(1, 3);
aPassing = rAG.Next(1, 3);
}
else
{
//if not a keeper, shit keeper attributes, and random outfielder atts.
dKeeper = 1;
}
//my clever way of assigning a player position to the players.
int def = dTackle + dMarking;
int mid = mCrossing + mPlaymaking;
int fwd = fShooting + fShotPower;
if (dKeeper > 1)
{
playPos = "Goal Keeper";
}
else if (def >= fwd && def >= mid)
{
playPos = "Defender";
}
else if (mid >= fwd && mid >= def)
{
playPos = "Midfielder";
}
else if (fwd >= mid && fwd >= def)
{
playPos = "Striker";
}
else
{
//in a rare case (if ever) the logic doesnt add ut, im spawning a star player. because. im not too got at this.
playPos = "Utility Legend";
dTackle = rAG.Next(16, 21);
dMarking = rAG.Next(16, 21);
mPlaymaking = rAG.Next(16, 21);
mCrossing = rAG.Next(16, 21);
fShooting = rAG.Next(16, 21);
fShotPower = rAG.Next(16, 21);
aDribbling = rAG.Next(16, 21);
aHeading = rAG.Next(16, 21);
aPassing = rAG.Next(16, 21);
}
}
//Generates a first name
public string firstNameGenerator()
{
string returnfirstName;
string[] firstnames;
firstnames = new string[60] { "60 different strings of first names... took them out for you, becasue it looked bad in the editor." };
Random rF = new Random(Guid.NewGuid().GetHashCode());
returnfirstName = firstnames[rF.Next(0, 40)];
return returnfirstName;
}
//generates a last name
public string lastNameGenerator()
{
string returnlastName;
string[] lastnames;
lastnames = new string[60] { "60 different strings of lastnames........." };
Random rL = new Random(Guid.NewGuid().GetHashCode());
returnlastName = lastnames[rL.Next(0, 40)];
returnlastName = lastnames[rL.Next(0, 40)];
return returnlastName;
}
}
现在,对于我的其他代码,您知道 - 将所有内容放在一起的部分。
namespace FormManager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
player p = new player();
p.autoGenPlayer();
textBox10.Text = p.firstName + " " + p.lastName;
textBox11.Text = p.age.ToString();
textBox1.Text = p.aPassing.ToString();
textBox2.Text = p.aDribbling.ToString();
textBox3.Text = p.aHeading.ToString();
textBox4.Text = p.dTackle.ToString();
textBox5.Text = p.dMarking.ToString();
textBox6.Text = p.fShooting.ToString();
textBox7.Text = p.fShotPower.ToString();
textBox8.Text = p.mPlaymaking.ToString();
textBox9.Text = p.mCrossing.ToString();
textBox12.Text = p.dKeeper.ToString();
textBox13.Text = p.playPos;
}
private void button2_Click(object sender, EventArgs e)
{
//number of objects to generate
int numberOfPlayersToGenerate = 10;
string[] savePlayers = new string[numberOfPlayersToGenerate];
//Generate many objects
for (int i = 0; i < numberOfPlayersToGenerate; i++)
{
player play = new player();
play.autoGenPlayer();
}
}
}
}
所以它基本上就是这里的for循环,我想用来存储所有生成的变量。 autoGenPlayer()方法生成了很多int和string的不同值,我想将它们全部存储起来,这样我就可以制作一个漂亮的表格来显示它。
我会喜欢这方面的任何想法,这让我疯狂。
答案 0 :(得分:1)
在form1中添加
List<Player> players = new List<Player>();
然后在你的button1_Click()中添加
players.add(p);
在button2_Click()
中players.add(play);
然后,您可以向表单添加gridview,并为玩家提供数据源 - FIN
不是这样 -
如果您不知道如何设置数据源,请How do I bind a GridView to a custom object?。
答案 1 :(得分:0)
欢迎来到社区。 p>
你实际上有比你命名的三个选项更多的选项。
要正确理解这一点,您需要了解Inheritance。
如果您希望将此“组”绑定到Datagrid.Datasource,那么它需要是一种类型。查看文档,这是system.object类型(没有很多帮助,因为所有继承自system.object)。但是,如果您进一步阅读,则会有一个特定对象列表(所有对象都以“数据”开头,所有古怪过时)以及任何实现IListSource或IList接口的组件。
使所有这些合适的事情是Array,List(T)和Dictionary(TKey, TValue)都实现了IList接口。
那你应该使用哪个?
This question为您提供了充分的理由,您应该使用IList接口本身。这样,您只会对使用数据的人员进行最小限制。具体来说,在你的情况下,它应该是一个IList(播放器)(甚至更好的IList(IPlayer)并创建一个Player实现的IPlayer接口。)
对于内部存储,请使用List(播放器),因为它是无界的(与数组不同)并且没有字典键的开销(你似乎不想在这个名字中查找播放器 - 如果这是你想要做的事情,那么考虑使用名称作为字典中的键)
另外,如果您将autoGenPlayer更改为constructor,则不需要play.autoGenPlayer();
行