views:

2367

answers:

5

I have to do an exercise using arrays.

The user must enter 3 input (each time, information about items) and the inputs will be inserted in the array

Then i must to display the array.

The difficulty i face is that i don't know how to increase the array's length without changing the information within it and how to allow the user to enter another information again?

public string stockNum;
public string itemName;
public string price;

string[] items = new string[3];

public string [] addItem(string[] items)
{
    System.Console.WriteLine("Please Sir Enter the stock number");
    stockNum = Console.ReadLine();
    items.SetValue(stockNum, 0);
    System.Console.WriteLine("Please Sir Enter the price");
    price = Console.ReadLine();
    items.SetValue(price, 1);
    System.Console.WriteLine("Please Sir Enter the item name");
    itemName = Console.ReadLine();
    items.SetValue(itemName, 2);
    Array.Sort(items);
    return items;
}


public void ShowItem()
{
    addItem(items);
    Console.WriteLine("The stock Number is " + items[0]);
    Console.WriteLine("The Item name is " + items[2]);
    Console.WriteLine("The price " + items[1]);
}

static void Main(string[] args)
{
    DepartmentStore depart = new DepartmentStore();
    string[] ar = new string[3];
    // depart.addItem(ar);
    depart.ShowItem();
}
  1. How can i allow the user to enter more than on input of information .. for example, the first time the user will enter the information about item ( socket number, price and name) .. how can i make the user enter another information about other item ?

  2. hHow can i display the socket num, price and name for each item in the array, if i suppose to have more than one item in the array ?

+3  A: 

Try using System.Collections.Generic.List this provides a resizable collection which also has a ToArray() method to return an ordinary array if you need it.

mdresser
+1  A: 

Can you use ArrayList instead? It will resize as needed.

Chris Ballance
The generic lists should be used instead of ArrayList. ArrayList is basically List<object>, so could hold any random object instead of what it's meant to hold.
John Saunders
it doesn't matter if i use array for array list ..but i don't know how to use arraylist in the C#
3yoon af
I provided a link that will show you how ArrayList works.
Chris Ballance
Oh, i will see it now .. Thanks ..
3yoon af
+1  A: 

Use a list, and then convert to an array.

In fact, in this example, you should have a class or struct for StockInfo {stock number, price, item name}, then you should enter the data into a List. Finally, you could convert to StockInfo[] by using the ToArray method of the list.

John Saunders
doesn't allow to have more than one class .. so stock info must enter by the user, add them and display them in the same class but using two methods which are additems and display items ..
3yoon af
+7  A: 

I think you may be going about the problem wrong...

Consider that the three items (stock number, price, and name) are part of a larger object. For now we can just call it item. We want to store a number of items in a list or array. Consider using an object-oriented approach like this:

class Widget
{
  public int StockNum;
  public int Price;
  public string Name;

  Widget(stockNum, price, name)
  {
    this.StockNum= stockNum;
    this.Price = price;
    this.Name = name;
  }
}

Now, you can can instantiate a list of these objects:

List<Widget> items = new List<Widget>();

And add items to them:

for (int i = 0; i < 5; i++)
{
  //Get input from user
  System.Console.WriteLine("Please Sir Enter the stock number");
  stockNum= Convert.ToInt32(Console.ReadLine()); //This isn't very safe...

  System.Console.WriteLine("Please Sir Enter the price");
  price = Convert.ToInt32(Console.ReadLine()); //This isn't very safe...

  System.Console.WriteLine("Please Sir Enter the item name");
  name = Console.ReadLine();

  //Then add it to a new object
  Widget item = new Widget(stockNum, price, name);

  //And add this item to a list
  items.Add(item);
}
Andrew Flanagan
While it's true that there's a better way to do this, his problem is a homework assignment, which probably means he's required to use arrays.
Mystere Man
I agree with Mystere Man, I don't think the point is about the best way to do this, its more likely to highlight the fact that array sizes are fixed and to figure out a workaround.
Tim Jarvis
Thanks for help ..but i try to use this code .. but it give me an handled exception if i don't write any input ..
3yoon af
Yeah -- more checking really needs to be put in place to handle invalid input. The comments "this isn't very safe" is where this problem can happen.
Andrew Flanagan
+4  A: 

Assuming your assignment requires you to use Arrays and not other collection classes, then you will have to do things the hard way.

Arrays are immutable, that is you cannot change them after they have been created. You can change the contents, but not the size of the array. If you need to grow or shrink the array, you will have to create a new array with the new number of items, then copy the items you want from the old array to the new array.

For instance, suppose you want to take an array and only copy the even elements.

int[] a = new int[] {0,1,2,3,4,5,6,7,8,9};
int[] a2 = new int[a.Length / 2];
int counter = 0;
for (int i = 0; i < a.Length; i = i + 2)
{
    a2[counter++] = a[i];
}

Then, you could always assign a2 back to a

a = a2;
Mystere Man