473,569 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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??
Nov 15 '05 #1
4 1709
"Kevin" <kb*****@sbcglo bal.net> wrote in message
news:bd******** *************** ***@posting.goo gle.com...
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 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 :^)
Nov 15 '05 #2

"Frecklefoo t" <Chris_nospam@n ospam_nsageotec h.com> wrote in message
news:Ob******** ******@TK2MSFTN GP09.phx.gbl...
"Kevin" <kb*****@sbcglo bal.net> wrote in message
news:bd******** *************** ***@posting.goo gle.com...
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 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.


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
Nov 15 '05 #3
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?
Nov 15 '05 #4
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 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" <kb*****@sbcglo bal.net> wrote in message
news:bd******** *************** ***@posting.goo gle.com... 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?

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
455
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a arrayList to the datagrid. private void BindQuestions()
10
3576
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsicollectionclasssyncroottopic.asp) And I'm interested in locking an...
7
1783
by: Dave | last post by:
Hi all, I have... using System.Collections; namespace MyApp.Templates { public class MyPage : System.Web.UI.Page { ArrayList MyArray = new ArrayList();
1
2685
by: sd | last post by:
QUESTION: How can my ASP page - which uses language="VBScript" - pass a "System.Collections.ArrayList" object - as a parameter - to a C# method in the middle-tier ??? ----- Is it possible at all? - if not, what work-a-round is there? I have no issues passing "string" parameters successfully to other methods in the same "middle-tier" -...
4
2806
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been...
48
4425
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should...
6
2790
by: Vinit | last post by:
Hi I am passing an arraylist to a c#/.net webmethod from a perl client using soap:lite. The trace shows the elements in the xml request. The arraylist input in the webmethod however does not contain any values and is empty. It works fine with arrays....but NOT arraylists....does anyone know WHY? and HOW TO SOLVE THIS ISSUE? Regards,
3
2615
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: code:------------------------------------------------------------------------------ class Program { static public ArrayList MyArrayList = new ArrayList();
1
1458
tifoso
by: tifoso | last post by:
I searched here and only found somebody with a round-robin arraylist issue here but what they suggest I tried already and it doesn't work for me I hope somebody can shed some light Scenario: I'm porting a PalmOS App written on C to .NET initially for a desktop and then to the CF, generic stuff is not biggie I have a section where data comes...
8
2582
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array; m_ResultArray.Add ( aFoundObject);
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.