473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

How to change attributes of an ArrayList?

mb
If an ArrayList has an object named "myTest" which has properties ".Name"
and ".Number". now how do I change the .Name of the object in the
arraylist?

I have had bad experiences so far with ArrayLists. It seem like a lot of
extra work to get value out of the ArrayList. Maybe I am doing it wrong.

Thanks for the help.
Nov 16 '05 #1
3 1519
MB,

MB> If an ArrayList has an object named "myTest" which has properties
MB> ".Name" and ".Number". now how do I change the .Name of the
MB> object in the arraylist?

using System;
using System.Collections;

class Test {
public string Name;
public int Number;

public static void Main() {
Test myTest = new Test() ;
ArrayList myList = new ArrayList();

myList.Add(myTest);

((MyTest) myList[0]).Name = "MB";
((MyTest) myList[0]).Number = 7 ;

Console.Write(myTest.Name) ;
Console.Write(myTest.Number);
}
}

HTH,

Stefan
Nov 16 '05 #2
mb
I guess that will work. It's funny, none of the books I have looked at show
how to do this. In my experience so far, ArrayLists are cumbersome and
awkward. Are they supposed to be like this?

"Stefan Holdermans" <st****@mimmick.nl> wrote in message
news:41**************@mimmick.nl...
MB,

MB> If an ArrayList has an object named "myTest" which has properties
MB> ".Name" and ".Number". now how do I change the .Name of the
MB> object in the arraylist?

using System;
using System.Collections;

class Test {
public string Name;
public int Number;

public static void Main() {
Test myTest = new Test() ;
ArrayList myList = new ArrayList();

myList.Add(myTest);

((MyTest) myList[0]).Name = "MB";
((MyTest) myList[0]).Number = 7 ;

Console.Write(myTest.Name) ;
Console.Write(myTest.Number);
}
}

HTH,

Stefan

Nov 16 '05 #3
On one hand an ArrayList is flexible, because you can stuff any type
of object into an arraylist for storage. It's getting the object back
out and manipulating the object as a specific type that is indeed
awkward. Some people will try to get rid of the awkwardness by
creating thier own strongly typed container classes (i.e. a class that
would only store Test instances, and only return Test instances), but
this involves additional code. Generics, in .NET 2.0, will make the
process much easier:
http://msdn.microsoft.com/msdnmag/issues/03/09/NET/

--
Scott
http://www.OdeToCode.com/

On Sat, 2 Oct 2004 07:58:35 -0600, "mb" <mm@hotmail.com> wrote:
I guess that will work. It's funny, none of the books I have looked at show
how to do this. In my experience so far, ArrayLists are cumbersome and
awkward. Are they supposed to be like this?

"Stefan Holdermans" <st****@mimmick.nl> wrote in message
news:41**************@mimmick.nl...
MB,

MB> If an ArrayList has an object named "myTest" which has properties
MB> ".Name" and ".Number". now how do I change the .Name of the
MB> object in the arraylist?

using System;
using System.Collections;

class Test {
public string Name;
public int Number;

public static void Main() {
Test myTest = new Test() ;
ArrayList myList = new ArrayList();

myList.Add(myTest);

((MyTest) myList[0]).Name = "MB";
((MyTest) myList[0]).Number = 7 ;

Console.Write(myTest.Name) ;
Console.Write(myTest.Number);
}
}

HTH,

Stefan


Nov 16 '05 #4

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

Similar topics

2
by: Daniel Lidström | last post by:
Hi, I would like to know the cleanest way to change the serialization of my Line class from: <Line staStart="2327.02" length="10.00000003390744"> <End>549016.570965 57945.741122</End>...
3
by: ree | last post by:
I have an arraylist and I want to read attributes of the object in it. But with C++ using vectors its was straight forward. But I am lost with arraylists as these can hold variety of objects.
5
by: Martin | last post by:
Hi All, I'm trying to get at the data in a variable marked with my custom attribute. I'm not entirely sure where to begin. I've created my attribute class which is okay but at runtime I want to...
2
by: Tor Inge Rislaa | last post by:
How to change row height in a DataGrid I have DataGrid that is filled with data from a table in a DataSet. The content of the cells is text of more than one line (as a note field). What I...
3
by: wg | last post by:
I have written a class to contain tags that are loaded from a spreadsheet. I can load the value ok. But from another class I would like to change a value inside the arraylist. Here is my code (see...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
4
by: sherifffruitfly | last post by:
Hi, I have an xml file with structured like this: <?xml version="1.0" encoding="UTF-8"?> <Soldiers> <Soldier name="Billy Smith" rank="Private" serial="34" /> (a bunch more soldiers)
2
by: Shannon | last post by:
Hello, thank you for taking the time to read this. I am totally stumped on this one. I have a ton of TD attributes that have to be changed based on the data in the DB. All TD elements are ID...
6
by: thanigaimani83 | last post by:
Hi guys. i can explain for my doubt with example.see.my table have multiple type of employee details like as Mangemment,Programmer,Designers,Accountants,ContractEmployees.Each type of department...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.