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
thumper631Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsaboCommented:
I think you need to add the owrd override

public override void add(string s)
0
Chester_M_RagelCommented:
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?
0
eekjCommented:
I doubt you can actually override the method "public int Add(string s)"
I would do this: make a method called MyAdd that checks if the duplicates are allowed and if the value exists then calls the parent method Add(s), something like.....

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

      public MyStrCollection(): base()
      {
      }

      public MyStrCollection(bool bAllowDuplication): base()
      {
            this.bAllowDuplication = bAllowDuplication;
      }

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

      public int MyAdd(string s)
      {
            if  (AllowDuplication && (! this.Contains(s)))
                  return Add(s);
            else
                  return -1;
      }
}
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

thumper631Author Commented:
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
0
eekjCommented:
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.
0
Chester_M_RagelCommented:
But you can use new keyword to hide base class methods. Modified version of eekj's code,

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

     public MyStrCollection(): base()
     {
     }

     public MyStrCollection(bool bAllowDuplication): base()
     {
          this.bAllowDuplication = bAllowDuplication;
     }

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

     public new int Add(string s)
     {
          if  (AllowDuplication && (! this.Contains(s)))
               return Add(s);
          else
               return -1;
     }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eekjCommented:
aha, thats cool.
0
eekjCommented:
... maybe it should be base.Add(s)?? Dont want an infinite loop is all
0
Chester_M_RagelCommented:
yep, must be

base.Add(s);

0
thumper631Author Commented:
Thank you all very much for your help with this problem

James
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.