-
[Resolved] Overriding...
Let's say I want to inherit Hashtable, and restrict it to allow only strings. I have this...
Code:
public class Class1:Hashtable
{
public Class1() {}
public override void Add(string key, string value)
{
}
}
Which works great in VB (when converted to VB syntax, that is) but in C# I get "no suitable method found to override".
What am I missing?
:)
-
Apparently, it won't let you override the Add() because the signature of the baseclasses Add() takes 2 objects and the method you are trying to use to override it takes 2 strings.
The code below worked for me, but not when the 2 parameters in the Add method were strings.
Code:
using System;
using System.Collections;
public class myClass : Hashtable
{
public override void Add(object key, object value)
{
Console.WriteLine("Key is {0}, value is {1}", key, value);
}
}
class newClass
{
public static void Main()
{
myClass y = new myClass();
y.Add("test", "test2");
}
}
-
Well, the basic goal here is to make a type safe collection. So how would I go about only allowing certain object types, string in this case to be added? And why does this work in VB.NET and not C#?
-
-
Quote:
Originally posted by Memnoch1207
This may help you.
Very much so. Thanks
:)