I need to add ID numbers and descriptions to an array but i have no idea how to even make an array! I've tried researching it but i just find it confusing. I also need to be able to search an array. It should go something like this...

1. person enters ID numbers
2. enter description of person who's ID it is

then...

3. "enter id number"
4. person enters id number
5. if id number matches then description should pop up
6. if it doesn't match then returns to menu

can anyone help, im really stuck :(

Recommended Answers

All 11 Replies

What you need is an array of Person.
So first concentrate on making a class with all the info, methods etc. you want to put in a Person. Show your class code and then we will talk about arrays. OK?

What you need is an array of Person.
So first concentrate on making a class with all the info, methods etc. you want to put in a Person. Show your class code and then we will talk about arrays. OK?

urm...:S

I need to basically store ID numbers entered into an array, store descriptions entered into an array and then i need to be able to pull up the description by entering the ID number...

Well? I was thinking of something like this:

class Person
{
    string ID;

    string Name;
    int age;
    
    float CalculateSalary()
    {
    // make calculation
    }

    // whatever you want more
}

Now if we put every Person's info into a list(array), we can search the list and get what you want.

What ddanbe is suggesting is a more OO correct way to approach your problem. By creating a class to store the information relating to a Person you can then create an array of "People".

To just store ID and Description you will need a 2-dimensional array.
Since a standard array can only store one 'type' of value (Object arrays are messy and bloated) youy will need to store the ID as a string. The way to declare an array is to add a set of square bracers after the type declaration:

string[,] People = new string[10,2];
            People[0, 0] = "1";
            People[0, 1] = "First Person";

            People[1, 0] = "2";
            People[1, 1] = "Second Person";

The ',' in the bracers indicates that it is a multidimensional array. More then one comma would indicate more than 2 dimensions.
Once you have declared it you must initialise it. 'new string[10,2]' initialises the array as having an extent of 10 in the first dimension and 2 in the second. In other words 10 rows, 2 columns.
Be aware that in C# arrays are zero-based. So the first element is at index 0. So in the array i have created i will have two columns (0 and 1) and ten rows (0 to 9).
You reference an element in an array using its indices. So to change the value in the second column of the fifth row you would use People[4,1] = "something"; ([4,1] not [5,2] because of the zero-based arrays).

That gives you an intro to arrays. You can create an array of type Person using ddanbes approach.
However, if you havent been speficially instructed to use arrays i would recommend looking into a List<> instead. You have to decide how many items your array can store before you add anything to it, whilst a list can grow as you add to it.

What ddanbe is suggesting is a more OO correct way to approach your problem. By creating a class to store the information relating to a Person you can then create an array of "People".

To just store ID and Description you will need a 2-dimensional array.
Since a standard array can only store one 'type' of value (Object arrays are messy and bloated) youy will need to store the ID as a string. The way to declare an array is to add a set of square bracers after the type declaration:

string[,] People = new string[10,2];
            People[0, 0] = "1";
            People[0, 1] = "First Person";

            People[1, 0] = "2";
            People[1, 1] = "Second Person";

The ',' in the bracers indicates that it is a multidimensional array. More then one comma would indicate more than 2 dimensions.
Once you have declared it you must initialise it. 'new string[10,2]' initialises the array as having an extent of 10 in the first dimension and 2 in the second. In other words 10 rows, 2 columns.
Be aware that in C# arrays are zero-based. So the first element is at index 0. So in the array i have created i will have two columns (0 and 1) and ten rows (0 to 9).
You reference an element in an array using its indices. So to change the value in the second column of the fifth row you would use People[4,1] = "something"; ([4,1] not [5,2] because of the zero-based arrays).

How do i make an array which doesn't have a limit, like it will be like

string [] hdID
String [] hdDescription

i know that's all sorts of wrong, i want someone to be able to enter IDs and descriptions without any predefined limits i.e. you can only enter 2 names. I don't want any predefined limit....

Sorry if im making no sense

You can't with an array. You have to define its size when you initialise it.
One workaround is to redefine it each time you add to it, eg copy all items to a temporary array of same size, re-initialise original array to make it one item larger, add old items back in and new item at the end. But this can get computationally expensive if your array gets very large.

Much better to look at a List<>. It behaves similarly to arrays except you can use .Add to add new items without having a size limit (among other useful methods).

You could also go for something simpler like a struct instead of a class:

struct Person
{
    string ID;
    string info;
}

and now make a list of person struct:

List<Person> People = new List<Person>();

You now have an empty List called People, you can add as many persons as you like:

Person John; // no nee to instantiate Person is a struct.
John.ID = "12345";
John.info = "whatever";
People.Add(John); // add to our list

You can even adress items in your list with array syntax:

Console.WriteLine(People[0].ID);

See the link Ryshad gave for more infi about List

ddanbe's solution is definitely the best. However, it doesnt use an array. If you have been set a specific assignment that requires an array then you will need to adapt it.
If this is an open problem and you aren't required to use specific elements then i would certainly recommend you try to understand what he has given you. Lists are very useful, a good understanding of their use will serve you well.

You can't with an array. You have to define its size when you initialise it.
One workaround is to redefine it each time you add to it, eg copy all items to a temporary array of same size, re-initialise original array to make it one item larger, add old items back in and new item at the end. But this can get computationally expensive if your array gets very large.

Much better to look at a List<>. It behaves similarly to arrays except you can use .Add to add new items without having a size limit (among other useful methods).

i'm so confused!

Ok i have ID and description...
i cant use variables because people can enter more than one AND the id number has to match the description that's entered with it. THEN i have to be able to search for a particular id number and the description should pop up...should i not use arrays to do this? :S

should i not use arrays to do this?

Unless you know the exact number of records you will have (or it can be determined at runtime e.g, by the user entering a value), it is far better in most situations to use a list. Take a look at some of the information and links that the other posters have provided for you. A list will expand itself as it needs more storage (to a finite limit of course but far far larger than anything you would need). A list also has built in methods that make it highly searchable and sortable. C# arrays provide a few more bells and whistles than their C++ counterparts but they are still limited.

i cant use variables because people can enter more than one

I'm not sure what you mean by this... do you mean more than one record. You can use the Add() method of the list ad nauseum. Make a new object, add it to your list (if you're feeling adventurous you could simply add a "new" object myList.Add(new myObject(constructor parameters)); and save a step).

I hope I haven't made you more confused. It might be helpful for you to give us a brief summary of what you know so far about arrays and lists and why each would or wouldn't be appropriate for your problem (1-2 sentences not a term paper).

commented: very well put :) +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.