0

I'm currently learning C# and I was asked to make a game like monopoly on C and another version on C#. But there's some stuff I don't really understand on C# mainly because it's an object oriented language. So the question is, to manage the players I created a struct on the C version.

typedef struct regist
{
int number;
char name[50];
int money;
int position;
int inGame;
} Player;

Player players[8];

and to get any data of the struct I just do:

players[5].name = "Joe";

Now, my problem with C# is I managed to create the "struct". First I created a struct. Then I tried to make it with a list of structs. Then I just decided to make new instances of a class for every player. I was told it was the best way so I did it. So I have on my player class:

public class Players
{
 int number;
 string name;
 int money;
 int position;
 bool inGame;
 }

and on my form1:

private void newGameButton_Click(object sender, EventArgs e) { setNames(); getNames(); }

    public void setNames()
    {
        Players player1 = new Players();
        player1.name= textBox1.Text;
        Players player2 = new Players();
        player2.name = textBox2.Text;
        Players player3 = new Players();
        player3.name = textBox3.Text;
        Players player4 = new Players();
        player4.name = textBox4.Text;
    }

now what I don't know how to do is:

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

How can I achieve this? I also tried with lists but it wouldn't let me do it the way I wanted and also, I want to be able to use this same instances in others classes, I hope that is possible... Thank you.

MPT
  • 13
  • 4
  • 2
    What was the issue when you tried to use lists? – kgh Jan 22 '15 at 18:54
  • 3
    Don't use `char` arrays. – SLaks Jan 22 '15 at 18:54
  • Please show what you tried with lists. Because that's one of the bests ways to go. And by the way your final snippet won't work because a char array is not a string and your field is private by default. – Pierre-Luc Pineault Jan 22 '15 at 18:54
  • what getNames should do? You are creating a class which is good, and creating multiple instances of it by calling new operator, which is also good. However there is a little typo in your example code: player2.name... <=> jogador2.name = textBox2.Text; Also change class name from Players to Player so its correctly named. – Panu Oksala Jan 22 '15 at 18:55
  • @MPT Did you look at your C# code snippet? You have a char array in your class. And all those fields are private. – Pierre-Luc Pineault Jan 22 '15 at 18:58
  • @Mino I'm sorry, it's fixed now, the typo was because I changed the code was in portuguese (jogadores) and i changed it to english (players) and forgot to change those :p – MPT Jan 22 '15 at 18:59
  • @Pierre-LucPineault Oh, I got it now...just a typo, because to post on the forum I copied from the C part, it's of course a string in my code, though :) – MPT Jan 22 '15 at 19:00
  • I posted the list code. – MPT Jan 22 '15 at 19:04

2 Answers2

2

Once you have a List<Players>, you need to actually add your intances to the list.Then you can index as you expect:

private List<Players> allPlayers = new List<Players>();

public void setNames()
{
    Players player1 = new Players();
    player1.name= textBox1.Text;
    Players player2 = new Players();
    player2.name = textBox2.Text;
    Players player3 = new Players();
    player3.name = textBox3.Text;
    Players player4 = new Players();
    player4.name = textBox4.Text;

    allPlayers.Add(player1);
    allPlayers.Add(player2);
    allPlayers.Add(player3);
    allPlayers.Add(player4);
}

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

A style note, your class should be named Player not Players. A Player is an object, Players indicates a collection property of Player objects.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • yes, I get that and thank you but my question is, how can I then do that allPlayers[2] from within other classes? do I have to instantiate a new instance of the class that code is in? – MPT Jan 22 '15 at 19:07
  • @MPT No, you would need to pass the `allPlayers` list to that object, either on the class's constructor or as a parameter to one of the methods. – BradleyDotNET Jan 22 '15 at 19:08
  • Presumably you know how parameters work, so your other class would have a method, say the constructor, that accepts a `List`, ie: `public MyOtherClass(List players)`. It would then probably store that reference off in a private variable, that the classes methods could then access. Objects are always passed **as** a reference (like a pointer) in C#, so both your variable and the other classes variable will point to the same object. – BradleyDotNET Jan 22 '15 at 19:21
  • @MPT Not a problem, glad to help! – BradleyDotNET Jan 22 '15 at 22:29
0

While Creating class use properties

public class Players
{
    public int number{get;set;}
    public string name{get;set;}
    public int money{get;set;}
    public int position { get; set; }
    public bool inGame { get; set; }

    public string getNames(){
    return name;
    }
}

and while creating its objects

public  Players[] p = new Players[5];

and assignment can be done

p[0].name = textBox1.Text;
p[1].name = textBox2.Text;
p[2].name = textBox3.Text;
p[3].name = textBox4.Text;
p[4].name = textBox5.Text;

now if from any place of object p is accessible the for sure you can

 p[2].getNames();

do this.

OR

use List

List<Players> pList = new List<Players>();
Players p1 = new Players();
p1.name="abc";
pList.add(p1);
Players p2 = new Players();
p2.name="xyz";
pList.add(p2);

and again if pList is accessible then

public string getNames(){
return pList[0].name;
}
vishu9219
  • 761
  • 6
  • 14
  • when I use the return it says "index out of range", but if I put an messageBox.Show it works ok, why? – MPT Jan 22 '15 at 19:22
  • do one thing create a function getName inside the class and call using p[2].getName(); Hope this will solve your problem. – vishu9219 Jan 22 '15 at 19:25
  • http://stackoverflow.com/questions/4142867/what-is-difference-between-property-and-variable-in-c-sharp – vishu9219 Jan 22 '15 at 19:36
  • Arrays start at index 0, you might want to fix that. – Chris Jan 22 '15 at 19:36