Get a reference to an object in an Arraylist

S

Steve

newbie question. In C++ I could get a pointer to an item in a list, for
example
int* pInt = &myList[2];

then I could do something like:
*pInt = 666;

and then myList[2] would equal 666. Nice, very useful and handy.

In C# I can't figure out how to accomplish this and I have not accepted the
crushing sense that it's not possible at all. I know that pointers area f
oreign thing for the language and I don't want to be "unsafe" or mess with
the language, I'm trying to use it correctly, but isn't there some form of
aliasing in C#, some way of indirect data access?

I tried this C# code:
ArrayList arrayTest = new ArrayList();

int n = 666;

arrayTest.Add(n);

int rn = (int)arrayTest[0];

rn = 333;

it didn't effect the value of arrayTest[0] as I had oh so hoped...

Any ideas or wake up calls welcome!!

Thanks,
Steve
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

If the type that is in the array list is a reference type, then you can
just get the item in the array list, and you will have a reference (which
will do what you want). However, if it is a value type, then there is no
way to get this kind of indirection. Your best bet would be to have a
reference type which exposes the value through a property.

Hope this helps.
 
S

Steve

Hi Nicholas,

A "Reference Type" is something that I can create? I have a struct
"MyStruct"
I store 200 of these in an ArrayList like this ArrayList.Add( new MyStruct )

This will create 200 allocated objects. How can I make MyStruct return a
refence type of itsef?
I'm a little confused. I just read a quick little article on references, it
confused me more. ;)

I'm missing this part: "have a reference type which exposes the value
through a property"
Should I add a "reference type" as a property to "MyStruct" and if so, how
does one... wait... no, I thought I had an idea, but it was nothing.


It *feels* like I want to make my own collection and override Add() to
return a refernence to the item it just added, that would be awesome. Any
pointers/clues how/where to start with that?

Thanks for the quick response!!

I looked at CollectionBase, I could override [] to return a reference... I
think.
Nicholas Paldino said:
Steve,

If the type that is in the array list is a reference type, then you can
just get the item in the array list, and you will have a reference (which
will do what you want). However, if it is a value type, then there is no
way to get this kind of indirection. Your best bet would be to have a
reference type which exposes the value through a property.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve said:
newbie question. In C++ I could get a pointer to an item in a list, for
example
int* pInt = &myList[2];

then I could do something like:
*pInt = 666;

and then myList[2] would equal 666. Nice, very useful and handy.

In C# I can't figure out how to accomplish this and I have not accepted the
crushing sense that it's not possible at all. I know that pointers area f
oreign thing for the language and I don't want to be "unsafe" or mess with
the language, I'm trying to use it correctly, but isn't there some form of
aliasing in C#, some way of indirect data access?

I tried this C# code:
ArrayList arrayTest = new ArrayList();

int n = 666;

arrayTest.Add(n);

int rn = (int)arrayTest[0];

rn = 333;

it didn't effect the value of arrayTest[0] as I had oh so hoped...

Any ideas or wake up calls welcome!!

Thanks,
Steve
 
S

Steve

OK, I just read your post for the 4th time. Not sure why I was having such
a hard time. I see what you mean. So let's say, using the int example I
would want to do something like
public class CMyInt
{
int m_data = 0;
public int Data
{
// get - set - etc
}
}

but, what's weird is that in my code, I did that. In this thread I used int
to keep things simple, but in code I'm actually using a class which as I
understand it is a reference type
so.. the confusion continues.....




Nicholas Paldino said:
Steve,

If the type that is in the array list is a reference type, then you can
just get the item in the array list, and you will have a reference (which
will do what you want). However, if it is a value type, then there is no
way to get this kind of indirection. Your best bet would be to have a
reference type which exposes the value through a property.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve said:
newbie question. In C++ I could get a pointer to an item in a list, for
example
int* pInt = &myList[2];

then I could do something like:
*pInt = 666;

and then myList[2] would equal 666. Nice, very useful and handy.

In C# I can't figure out how to accomplish this and I have not accepted the
crushing sense that it's not possible at all. I know that pointers area f
oreign thing for the language and I don't want to be "unsafe" or mess with
the language, I'm trying to use it correctly, but isn't there some form of
aliasing in C#, some way of indirect data access?

I tried this C# code:
ArrayList arrayTest = new ArrayList();

int n = 666;

arrayTest.Add(n);

int rn = (int)arrayTest[0];

rn = 333;

it didn't effect the value of arrayTest[0] as I had oh so hoped...

Any ideas or wake up calls welcome!!

Thanks,
Steve
 
S

Steve

I said class... I lied, I meant struct. Bas memory, should have checked,
cause it was a struct... and structs are VALUE types. It's all very clear
now.

Thank you again :)


Steve said:
OK, I just read your post for the 4th time. Not sure why I was having such
a hard time. I see what you mean. So let's say, using the int example I
would want to do something like
public class CMyInt
{
int m_data = 0;
public int Data
{
// get - set - etc
}
}

but, what's weird is that in my code, I did that. In this thread I used int
to keep things simple, but in code I'm actually using a class which as I
understand it is a reference type
so.. the confusion continues.....




message news:%[email protected]...
Steve,

If the type that is in the array list is a reference type, then you can
just get the item in the array list, and you will have a reference (which
will do what you want). However, if it is a value type, then there is no
way to get this kind of indirection. Your best bet would be to have a
reference type which exposes the value through a property.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve said:
newbie question. In C++ I could get a pointer to an item in a list, for
example
int* pInt = &myList[2];

then I could do something like:
*pInt = 666;

and then myList[2] would equal 666. Nice, very useful and handy.

In C# I can't figure out how to accomplish this and I have not
accepted
the
crushing sense that it's not possible at all. I know that pointers
area
form
of
aliasing in C#, some way of indirect data access?

I tried this C# code:
ArrayList arrayTest = new ArrayList();

int n = 666;

arrayTest.Add(n);

int rn = (int)arrayTest[0];

rn = 333;

it didn't effect the value of arrayTest[0] as I had oh so hoped...

Any ideas or wake up calls welcome!!

Thanks,
Steve
 
A

Austin Ehlers

If you want to get a thorough understanding of reference and value
types, check out Jon Skeet's page at:

http://www.yoda.arachsys.com/csharp/parameters.html

Looking back at your code, and if you want to keep it a struct,
consider first adding a constructor to your struct to assign the
necessary fields, then add to the ArrayList. Otherwise, you'll have
to do this:

ArrayList myAL=new ArrayList()
....
myAL.Add(new myStruct);
int index=...;
myStruct t=(myStruct)myAL[index];
t.someintprop=...;
myAL[index]=t;

Austin

I said class... I lied, I meant struct. Bas memory, should have checked,
cause it was a struct... and structs are VALUE types. It's all very clear
now.

Thank you again :)
<snip>
 

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