none
Variable Array

    Question

  • I am wanting to create an Array or Collection of mixed data - like a list of names, addresses and phone numbers for multiple groups and people.  I am new to C# and have only been able to locate information for what I would describe as a 1 dimensional array or list.  For example if I want to have the following excel-like table in C# - how do I configure the code?

    Name   Address   Phone Number

    Joe 123 anywhere 502/123-4567

    Jill 235 someplace 418/229-4750

    etc.

    Thanks in advance

    Ken in Kentucky

    Friday, February 17, 2017 1:56 PM

All replies

  • You need to think in a more structured way rather than in an "Excel-like bunch of cells holding anything" way!

    What you are describing is an array/collection of person information, where each block contains the person's name, address, phone number and so on.

    So first you need to describe your person, for which you can define a struct or class.

    E.g.

    public class Person
    {
       public string Name {get;set;}
       public string Address {get;set;}
       public string PhoneNumber {get;set;}
    }


    Now you could have an array of Person objects. E.g.

    Person[] persons = new Person()
    {
       new Person {Name="Bob"},
       new Person {Name="Carol", Address="somewhere"}
    }

    Note that this is not necessarily the best way - that all depends on where you get your data from, what you want to do with it and so on. For example, you may want to define an Address class first holding a FirstAddressLine, SecondAddressLine, Town, County etc; and then define your Person class to hold an Address object rather than a simple string for the address.


    • Edited by RJP1973 Friday, February 17, 2017 2:18 PM
    • Proposed as answer by Cor LigthertMVP Sunday, February 19, 2017 10:53 AM
    Friday, February 17, 2017 2:16 PM
  • I am wanting to create an Array or Collection of mixed data - like a list of names, addresses and phone numbers for multiple groups and people.  I am new to C# and have only been able to locate information for what I would describe as a 1 dimensional array or list.  For example if I want to have the following excel-like table in C# - how do I configure the code?

    Name   Address   Phone Number

    Joe 123 anywhere 502/123-4567

    Jill 235 someplace 418/229-4750

    etc.

    Thanks in advance

    Ken in Kentucky

    All you need is write a CSV file to be read by your Excel Application.

    Friday, February 17, 2017 6:29 PM
  • You need to create an array containing 'objects', for each 'object' you want to include the Name e.g. "Joe", Address e.g. "123 anywhere" and phone number e.g. "502/123-4567"

    To do this we create a struct or class first, this is like a blueprint of what each 'object' is going to contain

    public class Person
    {
     public string Name {get;set;}
     public string Address {get;set}
     public string PhoneNumber {get;set}
    
     public Person(string Name, string Address, string PhoneNumber)
     {
     this.Name = Name;
     this.Address = Address;
     this.PhoneNumber = PhoneNumber;
     }
    
    }

    This allows us to create an instance of the object 'Person', set a Name, Address and PhoneNumber then add it into our array

    We can create a new instance of the object 'Person' a couple of ways -

    Person person = new Person("Joe", "123 anywhere", "502/123-4567");

    or

    Person person = new Person;
    person.Name = "Joe";
    person.Address = "123 anywhere";
    person.PhoneNumber = "502/123-4567";

    This will create an instance of 'Person' with the following details

    Name: 'Joe', Address: '123 anywhere', PhoneNumber: "502/123-4567"

    Now you can create a collection of 'Person objects'

    Person[] people = {
    new Person("Joe", "123 anywhere", "502/123-4567"),
    new Person("Jill", "235 someplace", "418/229-4750")
    };

    This will create an array of instanced objects of type 'Person' each holding a name, address and phone number, to use the data in this array we can index it then use the set methods to return values, e.g. (using the array we created above)

    Console.WriteLine(people[0].Name);
    Console.WriteLine(people[1].PhoneNumber);
    
    //Console output would look like
    //Joe
    //418/229-4750
    I'm sure its not the best if youre copying from an excel file, but its simple, hope it helps someone




    Saturday, February 18, 2017 1:01 AM
  • The Person class is the way to go, but the heck with using an Array. Use a generic List:

    List<Person> Persons = new List<Person>();
    Person person = new Person("Fred", "1234 Main Street", "(509) 123-4567");
    Persons.Add(person;)
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, February 18, 2017 5:59 PM
    Moderator