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());
}
}
}