473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ref object in ArrayList

Hi everybody

I feel kind of dumb for not finding anything useful to point me in the right
direction..

basically, I want to create a class with an Arraylist. I would like to add
strings and integers etc.
as referenced objects. Once all items are added I want to loop through the
ArrayList and
manipulate all items depending on their type which, once i get it working
will save a lot of coding.

the class is looks kind of like this (simplified)

public MyClass
{
private ArrayList items = new ArrayList();

public void AddItem(ref object item); // other parameters skipped for
post
{
items.Add(item);
}

public void Execute()
foreach (object obj in items)
{
if (obj is string)
obj = "my string value";
}
}

... main code

MyClass myClass = new MyClass();

myClass.AddItem((object) myString);

......

The code compiles and runs just fine with the annoying exception that
myString does not receive the new
value 'my string value'. I am sure it is something absolutely simple but I
am just looking at a black wall :)

Any help would be greatly appreciated.

Thanks
rb
Nov 16 '05 #1
2 5963
Strings are different then most other objects. They are immutable. Meaning,
when you say:

myString = "blah";

You are not changing what is in the myString string object. It is actually
now pointing to a completely different string object that contains 'blah'.

Now, as an aside, what you are trying to do, would not work with any other
conventional object either.

The 'ref' is only applicable inside the AddItem method. That is the point in
time when you are getting the object reference (as opposed to a copy of the
object reference you would normally get).

However, adding this objec to an arraylist - the arraylist contains a copy
of the reference to the object. The fact that the original parameter was
'byref', has nothing to do with the fact that the arraylist maintains its
own set of references for the objects in it.

"Ralf B." <ra*********@mortgageuk.com> wrote in message
news:eX**************@TK2MSFTNGP09.phx.gbl...
Hi everybody

I feel kind of dumb for not finding anything useful to point me in the right direction..

basically, I want to create a class with an Arraylist. I would like to add
strings and integers etc.
as referenced objects. Once all items are added I want to loop through the
ArrayList and
manipulate all items depending on their type which, once i get it working
will save a lot of coding.

the class is looks kind of like this (simplified)

public MyClass
{
private ArrayList items = new ArrayList();

public void AddItem(ref object item); // other parameters skipped for
post
{
items.Add(item);
}

public void Execute()
foreach (object obj in items)
{
if (obj is string)
obj = "my string value";
}
}

.. main code

MyClass myClass = new MyClass();

myClass.AddItem((object) myString);

.....

The code compiles and runs just fine with the annoying exception that
myString does not receive the new
value 'my string value'. I am sure it is something absolutely simple but I
am just looking at a black wall :)

Any help would be greatly appreciated.

Thanks
rb

Nov 16 '05 #2
Thank you Marina and David for your responses

what i basically need is to store object pointers and assign values to the
object value.

my idea is to create a SQLSelectStatement class that builds the sql command
string, executes the
string and, using a datareader, assigning the values to class variables.

if i succeeded I would have a clean structured code.

for example:

SqlStatement sql = new SqlStatement("MyTable"); // to build froam clause
//creates: "from MyTable " string

then I want to add items

sql.AddItem("Name", myNameString); // creates body string "Name" and should
add pointer to MyNameString in Arraylist
sql.AddItem("Age", myAgeInt); // extends body string to "Name, Age"
etc...

//myNameString and myAgeInt are simply what they are.. a string and an
int.variables of a class instance

sql.Execute();
//execute select statement using SqlDataReader, run through ArrayList and
assign retrieved values to the objects

since i have to create custom select statements that can often reach
significant lengths it seems obvious
to me that such a class saves a lot of time, is easier to maintain and sqls
can be changed rather fast.

is this possible at all?

regards
rb
Nov 16 '05 #3

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

Similar topics

4
by: Tamir Khason | last post by:
How can I set the type of the object added to ArrayList (type of Array List Members) Here is the code: protected ArrayList tabs = new ArrayList(); public ArrayList Tabs {
5
by: zfeld | last post by:
How do I cast an object to its proper class at runtime given its System.Type I have code that looks like this: MyObject class has subclasses of MySubObjectA & MySubObjectB: MyObject obja =...
4
by: Arjen | last post by:
Hi, I have a class with some attributes. For example class person with name as attribute. I have add multiple persons in an arraylist. Now I need a function to get/find a person by the name...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
1
by: Jeremy S. | last post by:
I suspect I may be fundamentally misunderstanding how object variable references behave - and I'd sure appreciate some clarification - given my particular scenario. Summary: In an ASP.NET (1.1)...
3
by: Patrick.O.Ige | last post by:
I'm loading an Array below but getting the error "Object reference not set to an instance saying 'ItemNumber = CType(Args.Item.FindControl("ItemNumber"), TextBox).Text' is the error line. I DON'T...
0
by: a | last post by:
I need to create an instance of a custom object 'School.Teacher' and use it in a Profile object. I'm developing a bad case of "Pretzel Logic" thinking about this. Filling the custom object ...
2
by: dotnetnoob | last post by:
i got this program that will fetch the data in the excel spreadsheet, it was working before then i make some adjustment and it now give me an error of "Object reference not set to an instance of an...
11
by: KMinFL | last post by:
This is a C# VS 2008 question... Our system has 2 base classes, SingleEntity and NewPluralEntity. SingleEntity provides access to properties and methods related to manipulating data in a database...
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
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,...
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
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.