Click here to Skip to main content
15,891,981 members
Home / Discussions / C#
   

C#

 
GeneralRe: Private Object goes NULL after entering Public Function Pin
Heath Stewart24-Feb-04 12:37
protectorHeath Stewart24-Feb-04 12:37 
GeneralInsure Numeric Data in TextBox Pin
ronboy24-Feb-04 9:39
ronboy24-Feb-04 9:39 
GeneralRe: Insure Numeric Data in TextBox Pin
Heath Stewart24-Feb-04 12:36
protectorHeath Stewart24-Feb-04 12:36 
GeneralRe: Insure Numeric Data in TextBox Pin
Huseyin Altindag25-Feb-04 10:20
Huseyin Altindag25-Feb-04 10:20 
QuestionHow to get Key-Press Event in "Web Browser Control" Pin
Moloy Bagchi24-Feb-04 9:24
Moloy Bagchi24-Feb-04 9:24 
GeneralProperty Procedure Pin
ajkumar24-Feb-04 8:53
ajkumar24-Feb-04 8:53 
GeneralRe: Property Procedure Pin
Werdna24-Feb-04 9:28
Werdna24-Feb-04 9:28 
GeneralRe: Property Procedure Pin
ian mariano24-Feb-04 11:29
ian mariano24-Feb-04 11:29 
You have to create an indexer into the class. For your example, Price on an object, it could refer to a PriceCollection:

//  type-safe price collection
//  we implement ICollection and IEnumerable to have some good functionality
public class PriceCollection : ICollection, IEnumerable
{
   private ArrayList  _prices = new ArrayList();

   //  this is the INDEXER to the contained prices.  Decimal is a good Type for
   //  holding monetary values.
   public Decimal   this[int index]
   {
      get  {  return (Decimal)_prices[index];  }
      put  {  _prices[index] = value;  }
   }

   public int   Count   {  get   {   return _prices.Count;   }   }

   public bool  IsFixedSize   {   get   {   return _prices.IsFixedSize;   }   }

   public bool  IsReadOnly   {  get  {  return _prices.IsReadOnly;   }   }

   public bool  IsSynchronized   {   get   {   return _prices.IsSynchronized;   }   }

   public object   SyncRoot   {   get   {   return _prices.SyncRoot;   }   }

   public void   CopyTo(Array array, int index)
   {
      _prices.CopyTo(array, index);
   }

   public IEnumerator   GetEnumerator()   {   return _prices.GetEnumerator();   }

   public int   Add(Decimal price)
   {
      return _prices.Add(price);
   }

   public void   Remove(int index)
   {
      _prices.RemoveAt(index);
   }
}


This way, your object can have a Price property which is a type-safe collection, and allows what you asked:

public class   MyObjectWithPrices
{
   //   ...

   private PriceCollection   _prices = new PriceCollection();

   //   ...

   //   Aha!
   public PriceCollection   Prices   {  get   {   return _prices;   }   }


   //   ...
}


Now if you have an instance of MyObjectWithPrices, myInstance, you can do fun things like:

Decimal  price = myInstance.Prices[4];   //   gets the price at index 4


Or

//   dump prices
foreach (Decimal price in myInstance.Prices)
{
   Console.WriteLine(price.ToString("C"));   //   print as currency
}


Or

//   dump prices another way
for (int i = 0; i < myInstance.Prices.Count; i++)
{
   Console.WriteLine("{0} = {1}", i,
      ((Decimal)myInstance.Prices[i]).ToString("C"));   //   print as currency
}


Using type-safe collections is a good idea.

Ian Mariano - http://www.ian-space.com/


"We are all wave equations in the information matrix of the universe" - me

GeneralForm using too much memory Pin
Shaun Becker24-Feb-04 7:52
Shaun Becker24-Feb-04 7:52 
GeneralRe: Form using too much memory Pin
je_gonzalez24-Feb-04 9:41
je_gonzalez24-Feb-04 9:41 
GeneralRe: Form using too much memory Pin
Shaun Becker24-Feb-04 10:45
Shaun Becker24-Feb-04 10:45 
GeneralRe: Form using too much memory Pin
Heath Stewart24-Feb-04 12:30
protectorHeath Stewart24-Feb-04 12:30 
GeneralRe: Form using too much memory Pin
Charlie Williams24-Feb-04 12:36
Charlie Williams24-Feb-04 12:36 
GeneralRe: Form using too much memory Pin
Charlie Williams24-Feb-04 12:35
Charlie Williams24-Feb-04 12:35 
GeneralRe: Form using too much memory Pin
CWIZO24-Feb-04 23:43
CWIZO24-Feb-04 23:43 
GeneralRe: Form using too much memory Pin
Charlie Williams25-Feb-04 10:28
Charlie Williams25-Feb-04 10:28 
GeneralRe: DateTime.Now in a label Pin
Nick Parker24-Feb-04 7:40
protectorNick Parker24-Feb-04 7:40 
GeneralRe: DateTime.Now in a label Pin
Dave Kreskowiak24-Feb-04 7:42
mveDave Kreskowiak24-Feb-04 7:42 
GeneralHelp, How to add a footer to a windows form Datagrid Pin
mdolby24-Feb-04 6:32
mdolby24-Feb-04 6:32 
GeneralRe: Help, How to add a footer to a windows form Datagrid Pin
Heath Stewart24-Feb-04 12:29
protectorHeath Stewart24-Feb-04 12:29 
GeneralHelp using C# COM Interop in VBA / Excel Pin
BeeBar24-Feb-04 4:49
sussBeeBar24-Feb-04 4:49 
GeneralRe: Help using C# COM Interop in VBA / Excel Pin
Nick Parker24-Feb-04 6:44
protectorNick Parker24-Feb-04 6:44 
GeneralRe: Help using C# COM Interop in VBA / Excel Pin
BeeBar24-Feb-04 7:01
sussBeeBar24-Feb-04 7:01 
GeneralAdd image to database Pin
Amirjalaly24-Feb-04 4:24
Amirjalaly24-Feb-04 4:24 
GeneralRe: Add image to database Pin
Judah Gabriel Himango24-Feb-04 4:53
sponsorJudah Gabriel Himango24-Feb-04 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.