I want to declare string Array
inside a class and then access it by a method. What I want is to run random inside method and catch the value, but I don't know if I'm doing it right.
namespace Animals
{
class Animals
{
static private string animal1 { set; get; }
static private string animal2 { set; get; }
private string randomOfAnimal { set; get; }
// To run random and get one randomize anmilan I'am runing this
public string GetAnimal()
{
string[] animalList = new string[] { animal1, animal2};
Random name = new Random();
randomOfAnimal = animalList[name.Next(0, animalList.Length - 1)];
return randomOfAnimal;
}
public string GetAnimal1()
{
return animal1;
}
public string Getanimal2()
{
return animal2;
}
public void PrintAnimals()
{
Console.WriteLine("Write the first animal name");
animal1 = Console.ReadLine();
Console.WriteLine("Write the second animal name");
animal2 = Console.ReadLine();
}
}
}
And then I create an object on:
static void Main(string[] args)
{
Animals newAnimal = new Animals();
newAnimal.PrintAnimals(); // to give a value
// And then to save randomized animal from class
string myAnimal = newAnimal.GetAnimal();
}
Is this right to do? For some reason it's working but I feel something is wrong.
Random.Next()
especially read about the second parameter and check your code regarding the found information. \$\endgroup\$