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![Smile :) :)](/styles/default/custom/smilies/smile.gif)
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![Frown :( :(](/styles/default/custom/smilies/frown.gif)
Any thoughts??
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
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
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
![Frown :( :(](/styles/default/custom/smilies/frown.gif)
Any thoughts??