Reputation: 3
Hey i'm doing a project in which people can play a little war.
Now i have a class to create a player and i everytime i assign one of the properties to my class it just stays null. Here's my code and i've commented which one stays null and don't know why. first code you'll see is the method for creating a new player and third piece of code is my array with strings
public List<Speler> BeginSpel()
{
List<Speler> Spelers = new List<Speler>();
Random r = new Random();
for (int i = 1; i <= 20; i++)
{
Guid g;
g = Guid.NewGuid();
int rangindex = r.Next(0, 22);
int wapenindex = r.Next(0, 4);
DateTime vandaag = DateTime.Now;
//Wapen wapen = new Wapen();
string wapen = "";
switch (wapenindex)
{
case 0:
wapen = Wapen.mes.ToString();
break;
case 1:
wapen = Wapen.handgeweer.ToString();
break;
case 2:
wapen = Wapen.sniper.ToString();
break;
case 3:
wapen = Wapen.bazooka.ToString();
break;
}
//so the string below here always gets filled up but when i check my list the property where i use rangAfkorting just stays null
string rangAfkorting = Rang.rangAfkorting[rangindex];
Spelers.Add(new Speler(g.ToString(), "Player_" + i.ToString(), Rang.rang[rangindex], rangAfkorting, 100, wapen, vandaag.ToString(), "nog niet"));
}
return Spelers;
}
public class Speler
{
public string Naam { get; set; }
public string Id { get; set; }
public string Rang { get; set; }
public string Wapen { get; set; }
public string Aangemaakt { get; set; }
public string Rangafkorting { get; set; }
public string KIA { get; set; }
public int Health { get; set; }
public Speler(int health)
{
Health = health;
}
public Speler()
{
}
//rang afkorting stays null
public Speler(string id, string naam, string rang, string rangafkorting, int health, string wapen , string aangemaakt, string kia)
{
Naam = naam;
Id = id;
Rang = rang;
Health = health;
Wapen = wapen;
Aangemaakt = aangemaakt;
KIA = kia;
rangafkorting = Rangafkorting;
}
}
public static string[] rang = new string[] {"generaal",
"luitenant-generaal",
"generaal-majoor",
"brigade-generaal",
"kolonel",
"luitenant-kolonel",
"majoor",
"kapitein-commandant",
"kapitein",
"luitenant",
"onderluitenant",
"adjudant-majoor",
"adjudant-chef",
"adjudant",
"eerste sergeant-majoor",
"eerste sergeant-chef",
"eerste sergeant",
"sergeant",
"eerste korporaal-chef",
"korporaal-chef",
"korporaal",
"eerste soldaat",
"soldaat"};
public static string[] rangAfkorting = new string[]
{"Gen",
"LtGen",
"GenMaj",
"BdeGen",
"Kol",
"LtKol",
"Maj", "Cdt", "Kapt", "Lt", "OLt", "AdjtMaj", "AdjtChef", "Adjt", "1SgtMaj", "1SgtChef", "1Sgt", "Sgt", "1KplChef", "KplChef", "Kpl", "1Sdt", "Sdt"};
Upvotes: 0
Views: 60
Reputation: 37020
The problem is in your Speler
constructor where you do the assignment for Rangafkorting
. You are attempting to assign a new value to the argument that was passed to the constructor rather than setting the property of the class based on the argument.
Try this instead (switch the order):
Rangafkorting = rangafkorting;
Upvotes: 1