简体   繁体   中英

How can I make the randomString answer public so i can access it from another class

I need the random answer generated to be accessed from another class. My goal is to give hints in another class so I already have person class where I have variables and I need to get the random answer and get the variables and output them for the user.

static void PopulatePersons()
            {
                person p1 = new person("Bill", "Brown", false, "m");
                person p2 = new person("Eric", "Brown", true, "m");
                person p3 = new person("Robert", "Blue", false, "m");
                person p4 = new person("George", "Brown", true, "m");
                person p5 = new person("Herman", "Green", false, "m");
                person p6 = new person("Anita", "Blue", false, "f");
                person p7 = new person("Maria", "Green", true, "f");
                person p8 = new person("Susan", "Brown", false, "f");
                person p9 = new person("Claire", "Brown", true, "f");
                person p10 = new person("Anne", "Brown", false, "f");
                List<person> personList = new List<person>();
                personList.Add(p1);
                personList.Add(p2);
                personList.Add(p3);
                personList.Add(p4);
                personList.Add(p5);
                personList.Add(p6);
                personList.Add(p7);
                personList.Add(p8);
                personList.Add(p9);
                personList.Add(p10);

                foreach (person p in personList)
                    Console.WriteLine(p.GetData());
            }
        }

       public static void GetRandomPerson()
        {


            Random r = new Random();
            int index = r.Next(personList.Count);
           person randomString = personList[index];
         
        }

If you want to have random values you should use one random object instead creating it every time.

private static Random _random = new Random();

public static void GetRandomPerson()
{
    int index = _random.Next(personList.Count);
    person randomString = personList[index];
}

You need to expose the randomString variable. I would suggest simply returning it from the GetRandomPerson() method.

public static void GetRandomPerson()
{
    int index = _random.Next(personList.Count);
    return personList[index];
}

And you would call it from your other class as

person theRandomPerson = [StaticClassName].GetRandomPerson();

where [StaticClassName] is the name of your class, which you didn't include.

A couple of notes: person should be Person in C# (Class names start with an upper case).

Your PopulatePerson() method can be cleaned up, and you are creating and disposing of the list every time it's called.

Move the List into a static field or property:

private static List<Person> PersonList;

and instantiate it in a static constructor.

static StaticClassName() 
{
   PersonList = new List<Person>();
   // might as well fill it as well;
   PopulatePerson();
}

This will make it persistent, so you only have to create the list once.

Next, get rid of the variables p1-p10. You can populate the list as:

personList.Add(new person("Bill", "Brown", false, "m")); 
... 

Which will eliminate 10 lines. Any time you create variables x1, x2, ... , x10... You should stop and think if there's a better data structure to use. At the very least, use an array, but in this case, throwing the objects directly into the list is more than sufficient.

Finally, I'd move the print loop into a method of it's own. It appears to be debugging code, which is absolutely fine, but you want to make it easy to remove this code. In this particular situation, it's not too bad to comment out these lines. But, as your programming becomes more complex, you'll need to develop a personal style to handle debugging.

Set up another method

// Used for debugging 
static void DumpPersonList()
{
   foreach (person p in personList)
      Console.WriteLine(p.GetData());
}

and call it where appropriate. This way, you only have one line to comment out to remove the dump, and a future programmer might be grateful that you gave them this tool in the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM