Reputation: 13
Im quite new to structs so i appreciate any help you guys give. The problem i have is that i dont know how i can make it so that the method ShowPokeBox actually gets the Array by Parameter. I tried Puting Pokemon.Array into the Parameter for ShowPokeBox but i couldnt get that to work as well.
Program runs as expected except for the ShowPokeBox. It doesnt inculte any data at all.
Thanks in advance.
namespace StructArrays
struct Pokemon{
public int health;
public string name;
public string author; }
class Program
{
public static void PokeBox(int PokeAnzahl)
{
Pokemon[] PokeBox = new Pokemon[PokeAnzahl];
Console.WriteLine("Enter {0} different Pokemons: ", PokeAnzahl);
for (int i = 0; i < PokeAnzahl; i++)
{
PokeBox[i] = new Pokemon();
Console.WriteLine("Enter Health Points: ");
PokeBox[i].health = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Pokemon Name: ");
PokeBox[i].name = Console.ReadLine();
Console.WriteLine("Enter Author of Pokemon: ");
PokeBox[i].author = Console.ReadLine();
}
}
public static void ShowPokeBox(int PokeAnzahl)
{
Pokemon[] PokeBox = new Pokemon[PokeAnzahl];
for (int i = 0; i < PokeAnzahl; i++)
{
Console.WriteLine("Pokemon Nr. {0} Name: {1} HP: {2} Author: {3}", i, PokeBox[i].name, PokeBox[i].health, PokeBox[i].author);
}
}
static void Main(string[] args)
{
int PokeAnzahl;
Console.WriteLine("How many Pokemons do you want to create?: ");
PokeAnzahl = Convert.ToInt32(Console.ReadLine());
PokeBox(PokeAnzahl);
ShowPokeBox(PokeAnzahl);
Console.ReadLine();
}
}
Upvotes: 1
Views: 27
Reputation: 1973
You are constructing two completely separate arrays of Pokemon, one when reading from input, and another when attempting to display the input data. ShowPokeBox() needs a reference to the same Pokemon[] that's created and populated in PokeBox().
i would suggest returning the Pokemon[] from PokeBox(), and then passing it into ShowPokeBox()...
public static Pokemon[] PokeBox(int PokeAnzahl)
{
Pokemon[] Pokemons = new Pokemon[PokeAnzahl];
Console.WriteLine("Enter {0} different Pokemons: ", PokeAnzahl);
for (int i = 0; i < PokeAnzahl; i++)
{
...
}
return Pokemons;
}
public static void ShowPokeBox(Pokemon[] Pokemons)
{
...
}
static void Main(string[] args)
{
...
var Pokemons = PokeBox(PokeAnzahl);
ShowPokeBox(Pokemons);
}
Upvotes: 2