ref object in ArrayList

R

Ralf B.

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
 
M

Marina

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.
 
R

Ralf B.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top