happysmile
happysmile

Reputation: 7777

binding list variable one after another

this is my class

public class Users
    {
       public string Name { get; set; }
       public int Age { get; set; }
       public string Gender { get; set; }
       public string Country { get; set; }
    }

i am defing an list variable

List<Users> myList = new List<Users> 

i have four functions each one returing a string array

{of  data content like, names, age, gender, country}
**functions names**
 FunNames();
 FunAge();
 Fungender();
 Funcountry();

now i need to bind these these return values of all these functions into list one by one like.

myList =FunNames();
myList =FunAge();
myList Fungender();
.....

hope my Question is clear. any help would be great thank you.

Upvotes: 1

Views: 929

Answers (2)

Ben McCormack
Ben McCormack

Reputation: 33118

If I understand your problem correctly, FunNames() et al each return a portion of the Users object. This can make your problem a little bit difficult because the data for each part of the Users object is stored in a separate array. Can we assume that each array is of the same length and that the corresponding positions in each array combine to make the various properties in the Users object? If so, we can use code like this:

public List<Users> GetUsersList()
{
    List<Users> myList = new List<Users>();
    string[] names = FunNames();
    string[] ages = FunAge();
    string[] genders = Fungender();
    string[] countries = Funcountry();

    for (int i = 0; i < names.Length; i++)
    {
        Users user = new Users();
        user.Name = names[i];
        user.Age = names[i];
        user.Gender = names[i];
        user.Country = names[i];
        myList.Add(user);
    }

    return myList();
}

The above code will iterate through each of arrays of strings returned by your functions and subsequently populate Users object and add them to the list. Again, this assumes that your arrays "line up" and have the same length.

NOTE: You may consider changing the name of Users to User. Think about it: a User object tells the programmer to expect a single User, not multiple Users. Your myList object represents multiple individual Users.

Upvotes: 0

tafa
tafa

Reputation: 7326

There is no easy way to do what you require as far as I know. You have to iterate all of those arrays, generate the Users instances and add them to your list.

string[] names = FunNames();
string[] ages = FunAge();
string[] genders = Fungender();
string[] countries = FunCountry();

/* have to make sure they are not of different lengths */

int minLength = names.Length;

if (ages.Length < minLength)
    minLength = ages.Length;

if (genders.Length < minLength)
    minLength = genders.Length;

if (countries.Length < minLength)
    minLength = countries.Length;

for(int i=0 ; i < minLength; i++)
{
    Users item = new Users();
    item.Name = names[i];
    item.Age = int.Parse(ages[i]);
    item.Gender = genders[i];
    item.Country = countries[i];

    myList.Add(item);
}

Upvotes: 1

Related Questions