Link to home
Start Free TrialLog in
Avatar of thumper631
thumper631

asked on

Inheriting from a class and adding properties

I am trying to extend the functions of the StringCollection class and want to add a feature that will not allow for duplicates to be added.  What I need help with is how the constructor and other things are used

public class MyStrCollection :StringCollection
{
      private bool bAllowDuplication = true;

            public bool AllowDuplication
            {
                  get
                  {
                        return bAllowDuplication ;
                  }
                  set
                  {
                        bAllowDuplication = value;
                  }

            }

unknown what to do in this area on overriding Add method or constructor

public void add(string s) // override
  {
     if  AllowDuplication  or (! AllowDuplication && (! self.contains(s))
        {
             inherited add(s)  //  not sure on this part either  
        }
  }

}

I hope to then just get it to the point of

MyStrCollection myStrCtnl = new MyStrCollection ();

hope that this make sense on what I am trying to do.

thx for any help

James
Avatar of dsabo
dsabo
Flag of Venezuela, Bolivarian Republic of image

I think you need to add the owrd override

public override void add(string s)
Avatar of Chester_M_Ragel
Chester_M_Ragel

I really don't understand your question fully. What do you mean by 'that will not allow for duplicates to be added'. Where you are trying to add? Are you speaking about singleton? Do you want only one instance kind of stuff?
SOLUTION
Avatar of eekj
eekj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thumper631

ASKER

Sorry if I did not explain clearly what it was that I wish for the class to do.  If I were to add the following strings to the containor and I do not want any duplicates,

.add("A");
.add("B");
.add("D");
.add("A");
.add("C");

then this would be the end result would be
A
B
D
C

I come from a delphi background so the code would be something like this (may not be syntax correct)

 if  AllowDuplication or (not AllowDuplication and (not self.contains(s)) then
inherited add(S);

Hope this makes a little more sense.  The other way I could have written it was to create a method that would be something like the following (not syntax correct)

public static void UpdateMyStringCollection(StringCollection strCollection, string s, bool allowDuplication)
{
  if allowDuplication
{
  strCollection.add(s)
}

if ! allowDuplication && (! self.contains(s))
{
  strCollection.add(s)
}
}

Thanks for all of your help with this problem.

James

p.s.

Always looking for good books on C# if you have any suggestion, please pass them on

thanks again
In .NET a base method has to have been declared as 'abstract', 'virtual' or 'override' in order for you to be able to override it.

Obviously whoever wrote the implementation for StringCollection decided they didnt want you overriding that particular method. Welcome to the wonderful world of .NET.

Also as an extra note, Add returns int. Your method signature was wrong to begin with.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
aha, thats cool.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you all very much for your help with this problem

James