473,881 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting ArrayList entries as different structures

Joe
Hello All:

I am writing a function which accepts an ArrayList as its parameter and
converts the contents of the ArrayList into an XmlNodeList. The ArrayList
will contain one of several different structures. For example, the
structures could be either a Customer structure (which contains a customer id
and name) or an Address structure (which contains an address type and street
number, city, etc). The XmlNodeList is uded to populate a DropDownList where
the id or address type are the ListItem's value and other information is the
ListItem's text.

I want the function to determine which structure is in the ArrayList
(Customer or Address) and cast the ArrayList entry appropriately. I can code
everything except the how to determine which structure is in the ArrayList.

For example:

Public Function Foo(AL as ArrayList) as XmlNodeList
Dim e as System.Collecti ons.iEnumerator = AL.GetEnumerato r
Do While e.MoveNext
' Determine which structure is contained in e.Current
' Perform other tasks with the e.Current entry of the ArrayList
Loop
'return some XmlNodeList
End Function

Does anyone know how to do this?

TIA,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
Nov 28 '05 #1
1 1743
You could do something like:

For each O as object in AL
if typeof O is CUSTOMER then ' Process as customer
elseif typeof O is ADDRESS then ' process as address
elseif etc...
end if
Next

Or you could create an interface that all your items share, and you could
cast all the items in the arraylist to that interface and treat them
identically.

----
700cb Development, Inc.
http://www.700cb.net
..NET utilities, developer tools,
and enterprise solutions

=?Utf-8?B?Sm9l?= <jo******@donot spam.yahoo.com> wrote in
news:C1******** *************** ***********@mic rosoft.com:
Hello All:

I am writing a function which accepts an ArrayList as its parameter
and converts the contents of the ArrayList into an XmlNodeList. The
ArrayList will contain one of several different structures. For
example, the structures could be either a Customer structure (which
contains a customer id and name) or an Address structure (which
contains an address type and street number, city, etc). The
XmlNodeList is uded to populate a DropDownList where the id or address
type are the ListItem's value and other information is the ListItem's
text.

I want the function to determine which structure is in the ArrayList
(Customer or Address) and cast the ArrayList entry appropriately. I
can code everything except the how to determine which structure is in
the ArrayList.

For example:

Public Function Foo(AL as ArrayList) as XmlNodeList
Dim e as System.Collecti ons.iEnumerator = AL.GetEnumerato r
Do While e.MoveNext
' Determine which structure is contained in e.Current
' Perform other tasks with the e.Current entry of the
ArrayList
Loop
'return some XmlNodeList
End Function

Does anyone know how to do this?

TIA,


--
Dec 4 '05 #2

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

Similar topics

14
3009
by: tweak | last post by:
I'm struggling with the concept of typecasting when setting up a TCP client. Here's a code snip (modified from W. Richard Stevens Unix Programming book) to demonstrate where I am struggling: int sockfd; struct sockaddr_in servaddr; bzero(&servaddr, sizeof(servaddr));
2
3596
by: Rustam Asgarov | last post by:
Hi. I have ArrayList that contains structures... I need to iterate through all structures in this array and update one of members in this structures... The first thing that comes to mind is just use foreach, store struct in temp variable , update it , then write it back to array to the same position. Is there more efficient way? Thanks..
2
5485
by: MattC | last post by:
Hi, How can do runtime casting? MyCollection derives from ArrayList I will store lost of different objects that all derive from the same parent class. I then want to be able to pass in the object type and collection type I have a number of classes that derive from ArrayList and have if pick out on ly those i asked for.
18
4766
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
8
1497
by: Maileen | last post by:
Hi, I would like to have something like an array (something like a collection) in order to store the following things : company name1, new1, old1, in1, out1 company name2, new2, old2, in2, out2 .... but i should be able to dynamically redimension it.
18
3262
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
3
5484
by: mosscliffe | last post by:
Newbie struggling with VB.NET I wish to create an arraylist of structures. I am very new to this coding syntax, so bear with my poor syntax - please struct mystruct Fld1 as short Fld2 as integer Fld3 as string Fld4 as short
3
2636
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();
4
15361
by: Schkhan | last post by:
Hi, I am sturggling with a peace of code mainly its about casting a pointer to one type of structure to a pointer to another type of structure. the confusion begins with the following line...(modified to highlight the actual prob) #define TEST_MACRO(first) ((struct second *) &first) /*here it would return a pointer to second, but the question is why it uses Here in this example "first" and "second" are two different structures*/
0
9928
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11100
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10401
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9554
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7110
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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

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.