• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

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
0
thumper631
Asked:
thumper631
  • 4
  • 3
  • 2
  • +1
4 Solutions
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now