[C#] How to use This Array

This is a discussion on [C#] How to use This Array within the C# Programming forums, part of the General Programming Boards category; at first is maybe practical to say that i am dutch, and so my english could be a little bit ...

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55

    [C#] How to use This Array

    at first is maybe practical to say that i am dutch, and so my english could be a little bit bad. :P

    Hello everybody,

    today i started to write a contact management application. and I use C# with the combination of XML(system.Xml) to make it work. at this moment i have a little problem with a array. the problem is that i dont know how to use the array in my script;

    this is the array:

    Code:
    string [] Contactinfo;
                Contactinfo = new string[5] {"Name", "Lastname", "City", "Postcode"};
    As you can see i declared the 4 arrays and give all of them a special name, the 1st Name, 2nd Lastname ect ect.

    but if i use this array like this(see code below). it turns out into a build error:

    Error 1 Cannot implicitly convert type 'string' to 'int'


    Code:
    xmltextwriter.WriteElementString("naam", Contactinfo["Name"]);
    this sounds strange to me because the array is a string and i didnt gave the array a value yet.(is it maybe possible, cause the array doesn't have a value yet so it returns in 0, so it's a int?)

    Thank you for your attention.

    Jelte.

    PS: does somebody knows a good beginner tutorial of C# and XML?
    Last edited by Jelte; 08-11-2008 at 10:53 AM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The strings you specified are the values of the array ("Name", "Lastname", etc...). To index each element you use zero-based integers (0, 1, 2, etc...).

    If you still want to index by a string you could use a dictionary, to map string -> string.
    Code:
    var ContactInfo = new System.Collections.Generic.Dictionary<string, string>();
    
    ContactInfo["Name"] = "John";
    ContactInfo["Lastname"] = "Smith";
    //etc...
    Depending on what you're trying to achieve it may be better to store the data in a struct or class:
    Code:
    class ContactInfo
    {
      public string Name;
      public string Lastname;
      //etc...
    }
    
    var MyInfo = new ContactInfo();
    
    MyInfo.Name = "John";
    MyInfo.LastName = "Smith";
    (in the above sample it's usually better to make a constructor that takes arguments, but that's another story)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    @Magos thanks for your reply, i knew that the original method of indexing is the zero-based, but i though i saw on some site that you can use: (see code below) to replace the numbers in this words.

    Code:
    Contactinfo = new string[5] {"Name", "Lastname", "City", "Postcode"};
    And Thanks so add the Code-Snippets in your message but i'am not yet that skilled that i can handle classes(i hope i will come in the future):P

    Greetings

    J.
    Last edited by Jelte; 08-11-2008 at 11:23 AM.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    you really should learn about classes early on since everything you do involves them. You shouldn't be using a string[] to represent an entity or object. It has no behavior, no convenient state, nor a convenient interface for users. The basics of classes are simple, you should learn them.
    Here to Deceive, Inveigle, Obfuscate Since 1945

  5. #5
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    indigo0086: thanks for your reply, do you have some good suggestion for a good tutorial about classes?

    Greetings Jelte.

  6. #6
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    @Jelte: Normally my initial reply to your last question would be: look it up on google. (this site has tutorials as well but not for C# since thats not the real focus here).

    But since you also speak dutch which is something rare on this board (except for Maes who hasnt been on since ages ) I'll do the google for you:

    First hit on google for C# class tutorial

    In elk geval welkom!

  7. #7
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Thanks you all, for your Help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] Sorting an array and preserving the original index
    By frodo_jedi in forum C Programming
    Replies: 10
    Last Post: 04-06-2009, 06:51 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21