Making a collection inside a class

  • Thread starter Thread starter Sunfire
  • Start date Start date
S

Sunfire

How do you make a collection inside a class and then access it? I am trying
to make a class that is a news item and dont know where to really start. Any
ideas?
 
Hi Stranger,

first at all: Real Names please!

Depending on your need you can declare the Collection
as a variable, with appropriate scope or the way i recommend
and i think is always the right you should make the collection
a class property, also with apropriate scope, where scope stands
for access modifiers like private, protected, internal, public or
whatever your project needs. keep in mind that you should code
with pragmatic in mind: Safe Initialization and complete cleanup.

Thats what i recommend,...others may not or they will follow
another aproach,...


Cheers,...

Kerem

--
---------
New Open Source Tools from me:
Calculate MD5 or SHA1 Hash for Files!
KHash Tools:
http://entwicklung.junetz.de/projects/opensource/khashtools/khashtools v.1.0.zip
---------


Beste Grüsse / Best regards / Votre bien devoue

Kerem Gümrükcü
(e-mail address removed)

Best Quote: "Ain't nobody a badass with a double dose
of rock salt...", Kill Bill Vol.2

Latest Open-Source Projects: http://entwicklung.junetz.de
Sign my guestbook: http://entwicklung.junetz.de/guestbook/
 
Is there a way you can give me a code example? Nothing too huge but
something enough to give me an idea of what I'm doing...
 
Is there a way you can give me a code example? Nothing too huge but
something enough to give me an idea of what I'm doing...













- Show quoted text -

Here's a dotnet 2 class that shows the basics using a generic
dictionary. You can use similar techniques with a List amongst others
depending on your requirements. Collections is quite a broad topic
though.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.CompilerServices;

namespace ClassLibrary1
{
public class Indexy
{
private Dictionary<string, string> _bits = new
Dictionary<string, string>();

[IndexerName("Value")]
public string this[String key]
{
set
{
if (_bits.ContainsKey(key))
{
_bits[key] = value;
}
else
{
_bits.Add(key, value);
}
}
get
{
if (_bits.ContainsKey(key))
{
return _bits[key];
}
else
{
return string.Empty;
}
}
}
public int Count()
{
return _bits.Count;
}
public Dictionary<string, string> MyDictionary
{
get
{
return _bits;
}
}
}
}
 
I was thinking more of a list type of thing. I saw somewhere that you can
use <List> but don't know how to use it. What I'm doing is creating a
NewsItem object. It has the properties Id, Date, Title, Body and
CategoriesList. The CategoriesList is the collection<list> that I need to
create. The other things I need to consider is if it will be static or not.
Any reccomendations on that? There will/can only be 1 copy or instance of
this NewsItem object at any given point in time. The other thing about the
NewsItem object, is that it needs to be passed to a method in the News
helper class that does all the database work for the News section of the
website. Can you advise?


Is there a way you can give me a code example? Nothing too huge but
something enough to give me an idea of what I'm doing...













- Show quoted text -

Here's a dotnet 2 class that shows the basics using a generic
dictionary. You can use similar techniques with a List amongst others
depending on your requirements. Collections is quite a broad topic
though.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.CompilerServices;

namespace ClassLibrary1
{
public class Indexy
{
private Dictionary<string, string> _bits = new
Dictionary<string, string>();

[IndexerName("Value")]
public string this[String key]
{
set
{
if (_bits.ContainsKey(key))
{
_bits[key] = value;
}
else
{
_bits.Add(key, value);
}
}
get
{
if (_bits.ContainsKey(key))
{
return _bits[key];
}
else
{
return string.Empty;
}
}
}
public int Count()
{
return _bits.Count;
}
public Dictionary<string, string> MyDictionary
{
get
{
return _bits;
}
}
}
}
 
I was thinking more of a list type of thing. I saw somewhere that you can
use <List> but don't know how to use it. What I'm doing is creating a
NewsItem object. It has the properties Id, Date, Title, Body and
CategoriesList. The CategoriesList is the collection<list> that I need to
create. The other things I need to consider is if it will be static or not.
Any reccomendations on that? There will/can only be 1 copy or instance of
this NewsItem object at any given point in time. The other thing about the
NewsItem object, is that it needs to be passed to a method in the News
helper class that does all the database work for the News section of the
website. Can you advise?

Something like the below? (watch for line wrap).
There's an enum for the categories (not necessary, but...) the
concrete NewsItem class which holds all the fields, and a
newsItemStatic class which is a very basic singleton. There's quite a
lot to say about singletons, but this will work. Finally there's a
test class which populates it and then prints out all the categories I
added.
As noted in the one comment, this isn't the tidiest code, but I wanted
to keep it compact. As for whether it needs to be static. It sounds
like it might be a candidate for the singleton pattern, but it doesn't
have to be.
Shout if anything isn't clear or I've missed the point :)


using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary1
{
public enum NewsCategories
{
Funny,
Silly,
Boring
}
public class NewsItem
{
public int ID; //usually these would all be private with
public properties
public DateTime Date;
public string Title;
public string Body;
public List<NewsCategories> Categories = new
List<NewsCategories>();

public void Populate(int pID, DateTime pDate, string pTitle,
string pBody, NewsCategories[] pCategories)
{
ID = pID;
Date = pDate;
Title = pTitle;
Body = pBody;
Categories.AddRange(pCategories);
}
}
public static class NewsItemStatic
{
private static NewsItem _newsItem = new NewsItem();

public static NewsItem Item
{
get
{
return _newsItem;
}
}
}
public class NewsTest
{
public void TestIt()
{
NewsCategories[] cats = new NewsCategories[]
{NewsCategories.Silly, NewsCategories.Funny};
NewsItemStatic.Item.Populate(1,DateTime.Now, "dog eats
world", "blah",cats);
NewsItemStatic.Item.Categories.Add(NewsCategories.Boring);
foreach (NewsCategories nc in
NewsItemStatic.Item.Categories)
{
Console.WriteLine(nc.ToString());
}

Console.WriteLine(NewsItemStatic.Item.Categories[2].ToString());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top