Results 1 to 4 of 4
  1. #1

    Adding Indices to a full pre-initialized Array C#

    I am in the process of creating an online store/shopping cart for my company and have run into a problem. I need to create an array of Items but when I call the class and initialize this array as a member variable of the class I have no idea how many items the customer may eventually add to the cart. When I declare this array I have to initialize it with a number of indices within it but once I do this I don't know how to add to the end of the array once it is full. Does anyone have any idea how to do this. I know I could assign it to a temporary array and loop through it to assign the old values to the newly initialize and larger array but this seems like overkill for what I would assume would be a common problem. Is there function to do this? If not how would you guys handle this?
    Thank you,
    tjc1027

  2. #2
    Set up your arrays like the following and they will grow as you need them. My example is an array that keeps track of computer information, yours would be "CartItems" and "CartItem." Then you can have a field for quantity and item number.


    Stick this in both class files (.cs):
    using System;
    using System.Collections;
    using System.Data;


    Code:
         public class Computer
         {
              private string name;
              private string ipaddress;
    
              public string Name
              {
                   get
                   {
                        return name;
                   }
                   set
                   {
                        name = value;
                   }
              }
    
              public string IPAddress
              {
                   get
                   {
                        return ipaddress;
                   }
                   set
                   {
                        ipaddress = value;
                   }
              }
    }
    Code:
         public class ComputerCollection : CollectionBase
         {
    
              public Computer Add()
              {
                   Computer computer = new Computer();
                   return Add(computer);
              }
    
              public Computer Add(Computer computer)
              {			
                   List.Add(computer);
                   return computer;
              }
    
              public Computer this[int index]
              {
                   get
                   {
                        return (Computer) List[index];
                   }
                   set
                   {
                        base.List[index] = value;
                   }
              }
    
              public int IndexOf(Computer computer)
              {
                   return List.IndexOf(computer);
              }
    
         }


    Thanks,
    mjcolo

  3. #3
    Join Date
    Aug 2004
    Location
    Here & There
    Posts
    175
    Just create a strongly typed collection class to contain item objects.

  4. #4
    Thanks guys that helps a lot I have never used collections before I will try that.
    Thanks,
    tjc1027

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •