ArrayList Issue

K

Kevin

I'm converting some code from VB.NET to C#. I have strange behavior
in an implemntation of an ArrayList. In VB.NET, the ArrayList is type
safe at compile time, where the C# is not type safe at compile time
(good). The C# code breaks at run time (bad). Here is the sample
code:

VB.NET
Public Class Country
Private _countryName As String
Private _capitalName As String

Public Sub New(ByVal countryName As String, ByVal capitalName As
String)
Me._countryName = countryName
Me._capitalName = capitalName
End Sub

Public ReadOnly Property CountryName() As String
Get
Return _countryName
End Get
End Property
Public ReadOnly Property CapitalName() As String
Get
Return _capitalName
End Get
End Property
End Class

Public Class CountryArrayList : Inherits ArrayList
Public Shadows Sub Add(ByVal inputCountry As Country)
MyBase.Add(inputCountry)
End Sub
End Class

Dim country1 As Country = New Country("USA", "Washington D.C.")
Dim country2 As Country = New Country("Canada", "Ottawa")
Dim country3 As Country = New Country("France", "Paris")
Dim country4 As Country = New Country("Australia", "Canberra")
Dim country5 As Country = New Country("Mexico", "Mexico City")
Dim dr As DataRow = Nothing

Dim al As New CountryArrayList

al.Add(country1)
al.Add(country2)
al.Add(country3)
al.Add(country4)
al.Add(country5)
al.Add(dr) ' compiler catches this :)

C# Code
public class Country
{
private string _countryName;
private string _capitalName;

public Country(string countryName, string capitalName)
{
this._countryName = countryName;
this._capitalName = capitalName;
}

public string CountryName
{
get {return _countryName;}
}
public string CapitalName
{
get {return _capitalName;}
}
}
public class CountryArrayList : System.Collections.ArrayList
{
public new int Add(Country country)
{
return base.Add(country);
}
}

Country country1 = new Country("USA", "Washington D.C.");
Country country2 = new Country("Canada", "Ottawa");
Country country3 = new Country("France", "Paris");
Country country4 = new Country("Australia", "Canberra");
Country country5 = new Country("Mexico", "Mexico City");

DataRow dr;

dr = null;

CountryArrayList al = new CountryArrayList();

al.Add(country1);
al.Add(country2);
al.Add(country3);
al.Add(country4);
al.Add(country5);
al.Add(dr); // compiler does NOT catch this, runtime error :(

Any thoughts??
 
F

Frecklefoot

Kevin said:
I'm converting some code from VB.NET to C#. I have strange behavior
in an implemntation of an ArrayList. In VB.NET, the ArrayList is type
safe at compile time, where the C# is not type safe at compile time
(good). The C# code breaks at run time (bad).

When you add the DataRow, the CountryArrayList is using the base class's
Add() which takes an object. You have to somehow mask it (I think you have
to override it or some such thing, I am not an expert), by making it private
so it can't be used as a public method.

HTH :^)
 
I

Ignacio Machin

Frecklefoot said:
When you add the DataRow, the CountryArrayList is using the base class's
Add() which takes an object. You have to somehow mask it (I think you have
to override it or some such thing, I am not an expert), by making it private
so it can't be used as a public method.

This is way you should derive from BaseCollection if you want a strong typed
collection.

Cheers,
 
K

Kevin

I know I should be deriving from CollectionBase, but we have some
existing VB.NET code that is similar to the sample I provided. We as
a company are going to be doing our development in C# and I need to
convert the VB.NET ArrayList code without drastically converting it to
inherit from CollectionBase.

So, my ultimate question is why does
public new int Add(Country country)
{
return base.Add(country);
}

not behave like
Public Shadows Sub Add(ByVal inputCountry As Country)
MyBase.Add(inputCountry)
End Sub

In looking on MSDN, they say to replicate Shadows in C# to use new
modifier. So, the new modifier on the C# Add method does not override
and hide the base class' Add method at design time. I get a runtime
error when I call it, but not a design time error, like the VB.NET Add
method.

Is there any way to alter the Add method in C# to get design time
errors, when inheriting from ArrayList?
 
J

Jay B. Harlow [MVP - Outlook]

Kevin,
Is there any way to alter the Add method in C# to get design time
errors, when inheriting from ArrayList?
One way you could get compile warning when you use Add(Object) is to replace
the normal Add function on ArrayList. And put the Obsolete attribute on your
replacement. You can still compile however you will have a second compile
warning. The first warning of course being the new keyword on Add(Country)
is not needed.

Something like:

[Obsolete]
public new int Add(Object value)
{
throw new NotImplementedException();
}

Of course the "correct" way to do this is to have inherited from
CollectionBase in the first place! ;-)

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top