First of all ,please be patient with me as I am trying to learn c#(moving from vb) + use of generics Collections and I am problems in trying to improve my collections
I have created a set of collection all inheriting from the original ones.
I would like to extend the collections that MS gives us by either overriding or adding my own methods etc.However I seem to have all sorts of problems
1)Trying to override Contains to give my own implemention tells that I cannot override it.
making the class internal tells me that I have to use the New keyword as I might hide a method.
WHat do they mean add New keyword.Also How can I do it.Shall I simply call it something else "IfContains"
using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; internal bool Contains(T item) } |
//error message
//Warning 1
//'WindowsApplication1.BindingListBase<T>.Contains(T)'
//hides inherited member 'System.Collections.ObjectModel.Collection<T>.Contains(T)'.
//Use the new keyword if hiding was intended.
2)I would like to overload the Add Function of any sort of collection so that the user of the collection does not have
to input myCol.Add(New Employee(name,Surname) .the user would simply do
private BindingListBase<Employees>=New BindingListBase<Employees>(); Employees.add("John","Smith"); I/O Employees.add(New Employee("John","Smith")); I/O |
using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; } using System; namespace WindowsApplication1 public Customer(string name, string surname) public string Name private string mSurname; public string Surname
|
Any suggestions
Thanks a lot as usual

Problem in overriding or adding methods in Collections .Can you help?
wrtoomes
Just want to say that I am a vb programmer shifting to c# because the market is all about c#.People who say that the difference is only in the syntax are wrong.
Many things that I took for granted in vb.net are not in c#.
So please be patient with me as i am learning c#.
It's not easy as i have to get used to so many concepts .
thanks .Now let me digest your answer and I come back to you
thanks again.
Eclipsyo
Not quite sure what your question is but if you want to invoke a base class method from your derived class and you want to ensure that you call only the base class version (and not your overridden version) then precede the method with base. like so:
class Base
{
virtual void Foo ( ) { }
}
class Derived
{
override void Foo ( )
{
//Call base class first (or last, doesn't matter where you call it really)
base.Foo();
//Now do my own work
}
}
This is only necessary if you want to actually do some additional work in your derived class. If you don't have anything useful to do then don't even bother adding the method to the derived class because it will automatically inherit the base class member. So given the following implementation:
class Base
{
virtual void Alpha ( ) { }
void Beta ( ) { }
virtual void Gamma ( ) { }
}
class Derived
{
override void Gamma ( ) { }
}
Here Derived contains the following methods:
Alpha - Uses the base class implementation because Derived didn't explicitly override it
Beta - Not virtual so always use the base implementation.
Gamma - Overridden in Derived so it will always be called.
Note that there times when you'll want to call the base class version and times when you'll want to call the derived version. In a great many cases you will find that when overriding a method you will call the base class version at some point during the method invocation since the base class probably has useful work to do as well. However if you need to invoke a method (such as Gamma) from some other method in your class you should simply use the method name (Gamma). This causes your derived class version to be called (if you overrode it). An example may help:
class List<T>
{
virtual void Add ( T item )
{
//Add to internal list
}
}
class AddressList : List<Address>
{
override void Add ( Address item )
{
//Do some validation
//Call base class to actually add it - if you just call Add() directly then you'll get a
//stack overflow
base.Add(item);
}
void Add ( string name, string address )
{
//Build our custom object
Address addr = new Address(name, address);
//To ease maintenance we'll simply pass the request to our standard Add method
Add(addr);
}
}
In the second Add method we won't blow the stack because it is calling the Add(Address) method, which then calls base.Add(). This is a very common paradigm. You should try to isolate your methods that do similar work (such as Add) such that they all call to some base method (in your derived class) to do the grunt work including calling the base class. This makes it easier to maintain. Assume for example that later you find that there is even more validation to do. Since all your Add methods call into the same one you can isolate your validation code there.
I don't see anything wrong with your code to cause the compiler to complain. Could you post more of the code and the line causing the problem
Michael Taylor - 10/28/05
David Mann &#91;MVP&#93;
Thanks again for the very good explanation!!!.I meant very clear thanks for your time in writing it.
I would appreciate you could help clarify why I get the following error:
//error I get
//Cannot create an instance of the variable type 'Employee' because it does not have
//the new() constraint . Even adding this still fails "where Employee:new ()"
I have simply added a class "employee" with a constructor(name,surname)
and an employee Collection adding an add method but i keep getting the error.
Any Ideas
//Employee BindingLIst Class
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace WindowsCollection
{
class EmployeeBindingList<Employee> : BindingList<Employee> //where Employee : new()
{
//public void Add(string name, string surname)
//{
// Add(new Employee(name, surname));
//}
void Add(string name, string surname)
{
//Build our custom object
Employee NewEmp = new Employee(name, surname);
//To ease maintenance we'll simply pass the request to our standard Add method
Add(NewEmp);
}
}
}
//EmployeeClass
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsCollection
{
class Employee
{
public Employee(string name, string surname)
{
mName = name;
mSurname = surname;
}
private string mName;
public string Name
{
get { return mName; }
set { mName = value; }
}
private string mSurname;
public string Surname
{
get { return mSurname; }
set { mSurname = value; }
}
}
}
THanks a lot for your time and suggestions
Ed Maia
But the reson is simple.
You don't have a empty constructor in your employee class.
//EmployeeClass
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsCollection
{
class Employee
{
public Employee(){}
public Employee(string name, string surname)
{
mName = name;
mSurname = surname;
}
private string mName;
public string Name
{
get { return mName; }
set { mName = value; }
}
private string mSurname;
public string Surname
{
get { return mSurname; }
set { mSurname = value; }
}
}
}
Deo M.
Hi,I have read your answer a couple of times and what you say makes perfect sense.In c# am making mistakes that I dont usually
make in vb.net.Thats my excuse anyway :-)
Regarding the concept of overloading and overriding it's clear however I had never encountered the New concept used in that way in vb.net.
YOur explanation if very good and has reinforced the concept in my head.
The only doubt I have is this:
Suppose you happy with a method in your base class and you dont want to override it nor overload it but you want to execute only
certain part of that method in the base class .How do you do this
Also
class EmployeeBindingList : BindingList<Employee>
{
void Add ( string first, string last )
{
Add (new Employee(first, last));
}
}
class Employee
{
public Employee(string name, string surname){
mName = name;
mSurname=surname;
}
}the above still complains about new constraint parameter.Cannot spot why
Thanks a lot for your very good explanation and sorry for silly questions.
Jia Li MSFT
When we talk about overriding a member we are talking about redefining a previously defined version (in the base class) with a new one. This permits us to modify (in a derived class) the functionality specifies in the base class by using a keyword (in C# it is override). This forms the basis of inheritance because it permits us to extend (or even completely change) the functionality provided by a base class. When designing a base class not all members should be overriddable for 2 reasons: 1) the implementation is such that it doesn't make sense or could cause problems if a derived class changed the functionality, and 2) performance. Therefore by default a member is not overridable. You must mark members as virtual in order to identify to the compiler and to users that an implementation can be overridden. If you do not mark a member virtual then the compiler will not let you override it. Now comes a C#-specific extension called new. By using the keyword new on a member you are telling the compiler that even though the base class member is not virtual you want it to be overridden anyway. This is almost always a bad idea. The only time this is remotely a good thing to do is if you do not have control over the base class and you must override the member. Again you never have to do this for a virtual member.
The problem with new is that it only works when you are dealing with the derived class and its children. If you refer to the base class then new has no effect. So given the following hierarchy:
class Base
{
void Foo ( ) { }
}
class Derived : Base
{
new void Foo ( ) { }
}
If you call it as follows (using the derived class or one of its descendants) then it calls Derived.Foo.
Derived d = new Derived();
d.Foo();
However if you call it through Base then it will still call Base.Foo because the method wasn't marked virtual so the compiler will never do a runtime lookup on the method.
Base b = new Derived();
b.Foo();
Note that this is the more common approach because you'll often call methods that require Base instead of Derived (otherwise why bother using inheritance). Before you go off an mark everything as virtual however be warned that virtual methods take an extra step to execute at runtime because the runtime can't rely on the static method table to determine the method to call. Instead the runtime has to look at the object being referenced and determine if it overrides the method and if so it calls the overriden method otherwise it calls the base method. The runtime optimizes this but it will always require a little extra time so the general rule of thumb is to mark members as virtual only when you need to be able to extend it. There are many people who say that everything should be virtual so they can change the implementation but I generally disagree. The recommended approach (at least by me) is to expose public non-virtual methods and use virtual protected methods when I want derived classes to be able to override some functionality.
That's overriding. Now for overloading. Overloading is the concept of defining a member in multiple ways to make it easier to use. You can overload any method irrelevant of how it was defined. No keywords are added. The only requirement for overloading is that the overload must differ by all other overloads by at least one parameter. You can add parameters, remove them or change their type but at least one difference must exist. It is not possible in C++/C#-style languages to overload based on return type because the return type is not mandatory in these languages. As an example we could modify the Derived class from above to do the following:
class Derived ( )
{
void Foo ( string message ) { }
}
Notice now that you we actually have 2 Foo methods: one that takes no parameters and one that takes a string. Callers of Derived (or its descendants) can call either version while callers of Base can only call the version that takes no parameters. Overloading is a powerful technique used in OOP all the time. Note that you can't overload a property because you can't change the type of a property. The only exception is indexed properties (this[] in C#). In this case you can overload on the index type but that is it. You'll often see collections overriden as follows:
class Collection : CollectionBase
{
public object this[int index] { }
public object this[type key] { }
}
In this case the collection can be enumerated by index but you can also give the collection some key and it can get the object associated with the key (like a dictionary). This, of course, would only work in the case where you can look up item by some key (such as a unique identifier) in your collection.
So, back to your original question. You can not override Contains because it is not virtual. I wouldn't recommend that you use new either because it doesn't work in all cases. If you must override the Contains method then your best bet is to implement the IBindingList<T> interface directly and use BindingList<T> as a private field internally to implement the functionality. You can then override the method as you see fit. Note however that you can overload the Contains method like so:
bool Contains ( string first, string last );
This exposes both the original Contains version and your overload that permits searching employees by name.
For Add you can create multiple overloads like so:
void Add ( string first, string last )
{
Add (new Employee(first, last));
}
However in order for this to make sense you should really change your class definition to this:
class EmployeeBindingList : BindingList<Employee>
{
}
The reason is that your original class you are creating a generic binding list type and are adding a new overload for Add but since the list is generic there is no reasonable way for you to implement this overload because, for example, if I then create an instance of your class using int then you can't convert a first and last name to an int. Instead you should create your non-generic wrapper class and add the overload there.
Hope this helps,
Michael Taylor - 10/28/05