locked
convert from a bindinglist of specific object to a string array RRS feed

  • Question

  • Hi,

    I have a bindinglist of type PersonObject

    BindingList

    <PersonObject> selectedPeople, this object has a number of properties, one of them is Fullname.

     

    public class PersonObject

    {

     

    private string fullNameField;

     

    public string FullName

    {

     

    get

    {

     

    return this.fullNameField;

    }

     

    set

    {

     

    this.fullNameField = value;

    }

    

    No I need to pass to another class a string array of only the fullname, not the personobject, how can I convert from bindinglist to array while specifying its an array of strings?

    I tried this

    selectedPeople.ToArray<

    

    Thanks.

     

     
    string, p => p) but of course this doesn't compile.

     

     

    Friday, September 23, 2011 2:28 PM

Answers

  • Look at this:

    class C1
    {
          BindingList<PersonObject> list = new BindingList<Personobject>();
          //example to add one person:
          list.Add(new PersonObject { FullName = "John Someone" });
          list.Add(new PersonObject { FullName = "Sara OtherOne" });
    
           string[] namesArr = list.Select(s => s.FullName).ToArray();
           C2 c2 = new C2(namesArr);
            Console.ReadLine();
    }
    
    class C2
    {
           public C2(stirng[] names)
           {
                  foreach(string name in names)
                         Console.WriteLine(name);
           }
    }
    
    
    
    //class:
    public class PersonObject
    { 
            private string fullNameField;
            public string FullName
            {
                 get { return this.fullNameField; }
                 set { this.fullNameField = value; }
            }
    }
    



    Mitja
    • Marked as answer by kalamantina Friday, September 23, 2011 4:06 PM
    Friday, September 23, 2011 2:44 PM

All replies

  • Can you please repair the code, so it will look more like it?

    thx in advance.


    Mitja
    Friday, September 23, 2011 2:38 PM
  • Look at this:

    class C1
    {
          BindingList<PersonObject> list = new BindingList<Personobject>();
          //example to add one person:
          list.Add(new PersonObject { FullName = "John Someone" });
          list.Add(new PersonObject { FullName = "Sara OtherOne" });
    
           string[] namesArr = list.Select(s => s.FullName).ToArray();
           C2 c2 = new C2(namesArr);
            Console.ReadLine();
    }
    
    class C2
    {
           public C2(stirng[] names)
           {
                  foreach(string name in names)
                         Console.WriteLine(name);
           }
    }
    
    
    
    //class:
    public class PersonObject
    { 
            private string fullNameField;
            public string FullName
            {
                 get { return this.fullNameField; }
                 set { this.fullNameField = value; }
            }
    }
    



    Mitja
    • Marked as answer by kalamantina Friday, September 23, 2011 4:06 PM
    Friday, September 23, 2011 2:44 PM
  • I'm glad to hear that you got it working!

    Have a nice weekend!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, September 24, 2011 4:26 AM