Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am playing around with Lists and I was wondering how to show a random item/element from that List.

I have a class called Person

public class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }


In my form I have created two textboxes (txtName & txtSurname) what I want to achieve is whenever user clicks a button (btnShow) it will automatically display either the name or the surname (never both).

I have created a List that hold the Person Object and initialized Person object.
C#
private List<Person> people = new List();
private Person person;


This is my btnCreate event. Which adds new people to the list:

C#
private void btnShow_Click(object sender, EventArgs e){
person= new Person();
person.Name= txtName.Text;
country.Surname= txtSurname.Text;
countries.Add(country);
}


What I have tried:

This is my btnShow event which will show the List<> items in the text boxes. Keep in my mind that I want to also display only the name or surname each time not both. And I am not sure how to do it. I have tried to just simply shuffle through the whole list to get something displayed but it displayed for example, System.Random instead of actual name.

C#
private void btnShow_Click(object sender, EventArgs e){
Random r = new Random();
string random = people[r.Next(people.Count)].ToString();

txtName.Text = random.ToString();
}


Any help would be appreciated

Thanks.
Posted
Updated 8-Nov-17 6:40am
v5
Comments
OriginalGriff 30-Oct-17 15:14pm    
Help with what, exactly?
What does the code do that you didn't expect, or not do that you did?
Where are you stuck?
What help do you need?

1 solution

"Doesn't work" doesn't give anyone enough information to help you, what is happening that you didn't expect to happen, or what isn't happening that you want to happen?

I'm going to assume your issue is partly in using ToString on the Person object. If you don't override ToString you'll just get the name of the type. So either be explicit in how you want the result to look;

string random = people[r.Next(people.Count)];
string result = random.Surname + ", " + random.Name;


or override ToString on the Person class to return the result you want. If the result has to be random Name or Surname you can do something like

public string ToString(Random random)
{
    if (random.Next(2) == 0)
    {
        return this.Name;
    }
    else
    {
        return this.Surname;
    }
}

public override string ToString()
{
    return ToString(new Random());
}


I've done a standard ToString() and also one where you can pass in your own instance of Random. This is in case you want to run the code in a loop. Random uses the time as a seed for its random number generator so if you create new instances of Random in a loop you can end up with the same random number each time because enough time hasn't passed to generate a new seed\sequence.
 
Share this answer
 
Comments
fellanmorgh 30-Oct-17 16:14pm    
Updated the question, sorry about that. Yes, it displayed System.Random instead of actual name or surname. I have tried the first solution you have suggested but whenever I try to type in random.Surname + ", " + random.Name but it says that .Name or .Surname dosent exist.
F-ES Sitecore 30-Oct-17 16:28pm    
The code I posted was wrong :o

Person random = people[r.Next(people.Count)];
string result = random.Surname + ", " + random.Name;

Note I removed the "ToString()" from the "random =" line.
fellanmorgh 30-Oct-17 16:49pm    
Alright cool that worked just fine :). Just one more question about the methods from solution two. I am not sure where should I call it whenever I want to display only the name or surname. Should I call it to be as: string result = randon.ToString(r)?
F-ES Sitecore 30-Oct-17 17:13pm    
Yep, you'd call it like

Person random = people[r.Next(people.Count)];
string result = random.ToString();

or if you have the code in a loop do;

Person random = people[r.Next(people.Count)];
string result = random.ToString(r);
fellanmorgh 30-Oct-17 18:50pm    
Awesome. Played around with it, works fine :). Thanks a lot for help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900