C# structures/classes/lists index -


I am currently learning C # and for monopoly on C and playing like any other version on C # was said. But there are some things that I do not understand primarily on C # because it is an object-oriented language. So the question is, to manage players, I created a straight on the C version.

  typedef struct regist {int number; Four names [50]; Int money; Int position; Int inGame; } Player; Player player [8];  

And to get just about any data:

  player [5] .name = "joe";  

Now, my problem with C # is that I'm successful in creating "struct" First of all, I made a struct, then I tried to make it with the list of strokes. Then I decided to make new examples of each class for each player. I was told that it was the best way, so I did it. Therefore my players are in the class:

  Public class players {int number; String name; Int money; Int position; Bull InGame; }  

and on my Form 1:

Private Zero newGameButton_Click (Object Sender, EventArgs e) {setNames (); GetNames (); }

  public zeros setname () {player player 1 = new player (); Player1.name = textbox1.Text; Player player 2 = new player (); Player2.name = textbox2.Text; Player player 3 = new player (); Player3.name = textbox3.Text; Player player 4 = new player (); Player4.name = textbox4.Text; }  

Now I do not know what to do:

  public string getNames () {return player [2] .name; }  

How can I get this? I also tried with lists, but it did not allow me to do this as I wanted, and I also want to be able to use such examples in other classes, I hope that it is possible. Thank you.

Once you have a list The list will add to your activities in Then you can expect index:

  private list & lt; Player & gt; AllPlayers = new list & lt; Player & gt; (); Public Zero Setnam () {player player 1 = new player (); Player1.name = textbox1.Text; Player player 2 = new player (); Player2.name = textbox2.Text; Player player 3 = new player (); Player3.name = textbox3.Text; Player player 4 = new player (); Player4.name = textbox4.Text; AllPlayers.Add (PLAYER1); AllPlayers.Add (player2); AllPlayers.Add (player3); AllPlayers.Add (player4); }  

Now you can use it in your code in allPlayers [2] or whatever other index.

A style note, your class name must be player not player . player is an object, the player player indicates the collection property of the objects.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -