开发者

How do you create and use a Collection that has as one of its members a Collection

I’m trying to create a Collection that will have a Collection as one of its members. The application is to track stock prices.

The primary collection should look like this: Symbol, Stock Name, (price collection’)

The ‘price collection’ should look like this: Date, Price

Some example data

APPL, Apple

[1/1/2011,$245;1/2/2011,$247;1/3/2011,$242; ....开发者_JS百科.]

MS, Microsoft [1/1/2011,$35;1/2/2011,$32;1/3/2011,$42; .....]

So in c# how do I create a collection class that has a collection class as a member? Then how do I access the ‘price collection’ members of that collection? (I’ve managed to figure out the creation and able to add at both levels, but I can only access the top level to see contents. I can’t figure out how to access ‘prices’ that I’ve added nor can I see any of its methods


List<T> is a generic class that can hold a collection of class instances of any type. So just create a class that holds another collection as one of its members (sample only):

class Stock
{
  public Symbol {get;set;}
  public Name {get;set;}
  public List<Quote> Quotes {get;set;}
}

Now create a List<Stock> and populate the Prices member for each Stock you add to the collection, i.e.

var myStocks = new List<Stock>();
Stock stock = new Stock() 
{ 
  Symbol = "MSFT",
  ..
  Quotes = new List<Quote>()
}
myStocks.Add(stock);

You can access the prices by accessing it just like any other member:

foreach(Stock stock in myStocks)
{
  foreach(Quote quote in stock.Quotes)
     Console.WriteLine(string.Format("{0} : {1}$", quote.Date, quote.Price));
}


You can go with List inside List. For your example, it can be

public class PriceList
{
  DateTime priceTime;
  Double price;
}
public class QuoteList
{
  String symbol;
  String stockName;
  PriceList priceCollection;
}

List<QuoteList> myQuote = new List<QuoteList>();

QuoteList will be treated as the normal List collection like integer list or string list with Add, Remove, Fetch operations.


So you'll probably have a Stock and a Price class, for example:

public class Price 
{
    public DateTime Date { get; set; }
    public decimal Price { get; set; }
}

The Price class has a date and a price property (I don't like the name of this class, but just for illustration). The Price class will be used in the collection of prices for each Stock:

public class Stock 
{
    private readonly IList<Price> _prices = new List<Price>();

    public string Symbol { get; set; }
    public string Name { get; set; }

    public IList<Price> Prices 
    {
        get { return _prices; }
    }
}

I chose a generic IList to keep it simple. There are other collection objects to choose from but this should suit your needs sufficiently. To get a list of Stock objects that each have an empty list of Price objects, you can simply build up new IList<Stock>:

IList<Stock> stocks = new List<Stock>();

And then create and add Stock objects to it with new additions to the Prices collection:

var appleStock = new Stock { Name = "Apple", Symbol = "APPL" };
appleStock.Prices.Add(new Price { Date = new DateTime(8, 23, 2011), Price = 199.43 });
appleStock.Prices.Add(new Price { Date = new DateTime(8, 25, 2011), Price = 206.28 });

stocks.Add(appleStock);

var googleStock = new Stock { Name = "Google", Symbol = "GOOG" };
googleStock.Prices.Add(/* same as above */);

stocks.Add(googleStock);

And on and on until you have your list of Stocks built up. I've made some basic assumptions about where you're data is coming from so your actual code may be very different, but I hope this gets you started.


class Price
{
  DateTime priceTime;
  Double price;
}
class Quote
{
  String symbol;
  String stockName;
  Price[] priceCollection;
}

List<Quote> myQuote = new List<Quote>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜