User-Define Class Library usage and indexing  
Author Message
Vincent Hung





PostPosted: Visual C# Language, User-Define Class Library usage and indexing Top

hi, I am relatively new to c#.

I created several class in a class library project (see the code below). And these classes have a
relationship like:

HSLibrary.HSLibA.ClassA.ClassB

But now I have 3 problem,
1.In the old version, HSLibA only has one child classA, and ClassA also has only one
child ClassB. But now in new version, HSLibA has collection of ClassA, and ClassA
also has collection of ClassB.I used the System.Collections.ArrayList (I learned it
from some articles I read).
So now I can use ClassA.HSSettings.Add to add a new ClassB class without any error
message, but I can't get any property or method of the ClassB.
(Ex. HSLibA.ClassA.HSSettings[index] )
2.When I import this class in other projects, can I use the string value as the index
Like the usage in the DataSet object, DataSet.DataTable[index] & DataSet.DataTable["string"].
And how do I create the code
3.I built an event in ClassB, and will fire when some properties have been changed.
And I want to handle this event in ClassA. How do I know the event was fired from
which one of the ClassB collection.

//ClassA

namespace HSLibrary.HSLibA
{
public class ClassA : System.Collections.ArrayList
{
private string sSettingID;
private string sSettingName;
private string sSettingDescription;
private ClassB oHSSetting;

public ClassA()
{
oHSSetting = new ClassB();
oHSSetting.SettingChanged += new HSCEvent_SettingChanged(oHSSetting_SettingChanged);
}
.
.
public ClassB HSSettings
{
get { return oHSSetting; }
set { oHSSetting = value; }
}
.
.
void oHSSetting_SettingChanged(object sender, string e)
{ ...... }

}
}

//ClassB

namespace HSLibrary.HSLibA
{
public delegate void HSCEvent_SettingChanged(HSCHourSetting sender, string e);

public class ClassB : System.Collections.ArrayList
{
private string sSettingID;
private string sSettingName;
private string sSettingDescription;

public ClassA()
{
}
.
.
public event HSCEvent_SettingChanged SettingChanged;

protected virtual void SettingChange(string e)
{
if (SettingChanged != null)
{
SettingChanged(this, e);
}
}
.
.
.
.
}
}




Visual C#17  
 
 
Paul Louth





PostPosted: Visual C# Language, User-Define Class Library usage and indexing Top

Sorry but your use of generic terms (ClassA, ClassB etc) is quite confusing. Can you provide a real example of what the problem is
 
 
Figo Fei - MSFT





PostPosted: Visual C# Language, User-Define Class Library usage and indexing Top

hi,Vince

The code is somewhat messy.

And about Q2: you must implement index["string"] first, I give you another example to show how to implement with string indexer value:

// Using a string as an indexer value
class DayCollection
{
    string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };

    // This method finds the day or returns -1
    private int GetDay(string testDay)
    {
        int i = 0;
        foreach (string day in days)
        {
            if (day == testDay)
            {
                return i;
            }
            i++;
        }
        return -1;
    }

    // The get accessor returns an integer for a given string
    public int this[string day]
    {
        get
        {
            return (GetDay(day));
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        DayCollection week = new DayCollection();
        System.Console.WriteLine(week["Fri"]);
        System.Console.WriteLine(week["Made-up Day"]);
    }
}

output:

5

-1


BR



 
 
boban.s





PostPosted: Visual C# Language, User-Define Class Library usage and indexing Top

1. Please don't use ArrayList. Use instead either generics, but for your case the best solution is to create collection classes (inherited from CollectionBase) for ClassA and ClassB.
2. As Figo Fei described, you should create indexer that will accept string and return some value using that string to find it.
3. Your ClassB will have event which has some event argument that will be a new class inherited from EventArgs but with additional properties, for examle id of ClassB instance or the instance it self.