I have a class that inherits a List
Thank you,
Scott
public class PerfAll : List<PerfCategory>
{
private PerfAll _PerfAllCatCounters;
public PerfAll()
{
_PerfAllCatCounters = new List<PerfCategory>(); // <-- this line is a compiler error
PerfAll _All;
PerfCategory _PerfCat;
PerformanceCounterCategory[] _CounterCategories = PerformanceCounterCategory.GetCategories();
foreach (PerformanceCounterCategory cat in _CounterCategories)
{
_PerfCat = new PerfCategory(cat.CategoryName);
_All.Add(_PerfCat);
}
_PerfAllCatCounters = _All;
}
public PerfAll PerformanceAllCatCounters
{
get
{
return _PerfAllCatCounters;
}
}
}
Custom class:
public class PerfCategory
{
private string _CategoryName;
private List<PerformanceCounter> _CounterList;
public PerfCategory()
{
_CategoryName = string.Empty;
_CounterList = new List<PerformanceCounter>();
}
public PerfCategory(string CategoryName)
: this()
{
PerformanceCounter[] _CounterArray;
_CategoryName = CategoryName;
PerformanceCounterCategory _Counter = new PerformanceCounterCategory(CategoryName);
_CounterArray = _Counter.GetCounters();
foreach (PerformanceCounter counters in _CounterArray)
{
_CounterList.Add(counters);
}
}
public string CategoryName
{
get
{
return _CategoryName;
}
set
{
_CategoryName = value;
}
}
public List<PerformanceCounter> CounterList
{
get
{
return _CounterList;
}
}
}
Afzal PawaskarPosted Dec 26, 2010, 2:12 AM
You don't need to create the PerfAll List
Check this out:
public class PerfAll : List
{
//private PerfAll _PerfAllCatCounters; //No need of this as PerfAll class is actually a list itself
public PerfAll()
{
//_PerfAllCatCounters = new List
// If a class Car inherits from a class Vehicle, you can initialise Car car=new Vehicle() as every car is definitely a vehicle
// but you cannot initialise Vehicle vehicle = new Car() as all vehicles are not neccessarily cars...
//PerfAll _All; //No need of this as PerAll class is actually a list itself
PerfCategory _PerfCat;
PerformanceCounterCategory[] _CounterCategories = PerformanceCounterCategory.GetCategories();
foreach (PerformanceCounterCategory cat in _CounterCategories)
{
_PerfCat = new PerfCategory(cat.CategoryName);
//_All.Add(_PerfCat); //Not needed. Same reason as above
base.Add(_PerfCat); //Surprised???Remember, PerfAll is a actually a list of type 'PerfCategory'...
}
//_PerfAllCatCounters = _All; //Not needed. Same reason as before
}
//public PerfAll PerformanceAllCatCounters ///////////////////////////////////
//{ //Not needed. You can use:
// get //PerfAll perfAll = new PerfAll();
// { //List
// return _PerfAllCatCounters; //in your main() method. The car,
// } //vehicle example applies here...
//} ///////////////////////////////////
}
However, your problem doesn't end here. You are going to face issues relating to SingleInstance and MultiInstance Category Types which I hope you can take care of...^_^
Sam HobbsPosted Dec 24, 2010, 4:30 PM