-2
\$\begingroup\$

I want to declare string Arrayinside 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.

\$\endgroup\$
1
  • \$\begingroup\$ I have voted to close this question because the code isn't doing what it should. Please read the documentation of the Random.Next() especially read about the second parameter and check your code regarding the found information. \$\endgroup\$ Commented Dec 1, 2015 at 6:26

1 Answer 1

1
\$\begingroup\$
  1. I think you do not fully understand the concept of static members. Until you do, you should avoid using them. Keep it simple.
  2. You should avoid creating GetSomeProperty() methods, thats what properties are for. In your case, you might want to use indexer instead of properties though.
  3. You should not re-create Random class if you want randomized results. Create it once and re-use it.

I think your class should probably look like this:

class Animals
{
    //check out  Heslacher's comment on Random.Next
    public string RandomAnimal  { get { return _animalList[_random.Next(0, animalList.Length)]; } }

    public string FirstAnimal { get { return _animalList[0]; } }
    public string SecondAnimal { get { return _animalList[1]; } }

    //your original method name is somewhat misleading
    public void ReadAnimals()
    {
        Console.WriteLine("Write the first animal name");
        _animalList[0] = Console.ReadLine();
        Console.WriteLine("Write the second animal name");
        _animalList[1] = Console.ReadLine();
    }

    private Random _random = new Random();
    private string[] _animalList = new string[2];
}
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.