ArrayList Issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin

    #1

    ArrayList Issue

    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 CountryArrayLis t : Inherits ArrayList
    Public Shadows Sub Add(ByVal inputCountry As Country)
    MyBase.Add(inpu tCountry)
    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("Austra lia", "Canberra")
    Dim country5 As Country = New Country("Mexico ", "Mexico City")
    Dim dr As DataRow = Nothing

    Dim al As New CountryArrayLis t

    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._countryNa me = countryName;
    this._capitalNa me = capitalName;
    }

    public string CountryName
    {
    get {return _countryName;}
    }
    public string CapitalName
    {
    get {return _capitalName;}
    }
    }
    public class CountryArrayLis t : System.Collecti ons.ArrayList
    {
    public new int Add(Country country)
    {
    return base.Add(countr y);
    }
    }

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

    DataRow dr;

    dr = null;

    CountryArrayLis t al = new CountryArrayLis t();

    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??
  • Frecklefoot

    #2
    Re: ArrayList Issue

    "Kevin" <kboesel@sbcglo bal.net> wrote in message
    news:bd3d2c93.0 309190730.672b9 fbb@posting.goo gle.com...[color=blue]
    > 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).[/color]

    When you add the DataRow, the CountryArrayLis t 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 :^)


    Comment

    • Ignacio Machin

      #3
      Re: ArrayList Issue


      "Frecklefoo t" <Chris_nospam@n ospam_nsageotec h.com> wrote in message
      news:ObW$QgsfDH A.1760@TK2MSFTN GP09.phx.gbl...[color=blue]
      > "Kevin" <kboesel@sbcglo bal.net> wrote in message
      > news:bd3d2c93.0 309190730.672b9 fbb@posting.goo gle.com...[color=green]
      > > 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).[/color]
      >
      > When you add the DataRow, the CountryArrayLis t 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[/color]
      private[color=blue]
      > so it can't be used as a public method.[/color]

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

      Cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation


      Comment

      • Kevin

        #4
        Re: ArrayList Issue

        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(countr y);
        }

        not behave like
        Public Shadows Sub Add(ByVal inputCountry As Country)
        MyBase.Add(inpu tCountry)
        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?

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: ArrayList Issue

          Kevin,[color=blue]
          > Is there any way to alter the Add method in C# to get design time
          > errors, when inheriting from ArrayList?[/color]
          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 NotImplementedE xception();
          }

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

          Hope this helps
          Jay

          "Kevin" <kboesel@sbcglo bal.net> wrote in message
          news:bd3d2c93.0 309211457.478b6 cdf@posting.goo gle.com...[color=blue]
          > 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(countr y);
          > }
          >
          > not behave like
          > Public Shadows Sub Add(ByVal inputCountry As Country)
          > MyBase.Add(inpu tCountry)
          > 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?[/color]


          Comment

          Working...