What if I really need to build a list using pointers and not use an ArrayList?

Joined
Jun 25, 2005
Messages
73
Reaction score
0
Hello and thank you for your help in advance!

I develop a program in C# and reached a point where I really need to use
pointers. I do not
want to use them in an unsafe context though.

As with Java, I know that I can use an unassigned variable as a pointer to
another object,
for example:

SomeClass someClass = new SomeClass();
SomeClass refClass = null;

refClass = someClass;


Recently I spoke with an experienced programmer and he told me that you do
not want to use
this kind of pointers in C# or Java. Unfortunately, I forgot to ask him
about the reason why I
would not want to use pointers like this.

So my question is:
Why shouldn't I use a pointer like this? Especially why would I want to
refrain from creating
an array list using the technique shown above?

I need quick access to an array list where the elements are sorted by more
than 1 criteria.

To give more details, I want to use it in code like this:

class MyClass
{
int X;
int Y;
int Z;
MyClass nextX, nextY, nextZ;
}
ContainerStruct

....and then use some code like this (when there are already some MyClass
instances):

MyClass searchClass = headClass.nextX; // I define the headClass as the
first element of the list here
int x = 3; // any value

while( searchClass.X > x )
{
searchClass = searchClass.nextX;
}

Are there any more elegant ways to do this?

Thank you!
 
J

Jon Skeet [C# MVP]

Hello and thank you for your help in advance!

I develop a program in C# and reached a point where I really need to use
pointers. I do not
want to use them in an unsafe context though.

As with Java, I know that I can use an unassigned variable as a pointer to
another object,
for example:

SomeClass someClass = new SomeClass();
SomeClass refClass = null;

refClass = someClass;

In what way is that an unassigned variable? It's not doing anything
special - it's just making two variables have the same value.
Recently I spoke with an experienced programmer and he told me that you do
not want to use this kind of pointers in C# or Java. Unfortunately, I forgot
to ask him about the reason why I would not want to use pointers like this.

So my question is:
Why shouldn't I use a pointer like this?

There's nothing wrong with using references like that.
Especially why would I want to
refrain from creating an array list using the technique shown above?

Um, because ArrayList already exists in the framework?
I need quick access to an array list where the elements are sorted by more
than 1 criteria.

Any reason not to have more than one ArrayList? You could have multiple
lists containing the same elements and sort each one independently.
To give more details, I want to use it in code like this:

class MyClass
{
int X;
int Y;
int Z;
MyClass nextX, nextY, nextZ;
}
ContainerStruct

...and then use some code like this (when there are already some MyClass
instances):

MyClass searchClass = headClass.nextX; // I define the headClass as the
first element of the list here
int x = 3; // any value

while( searchClass.X > x )
{
searchClass = searchClass.nextX;
}

Are there any more elegant ways to do this?

There's nothing particularly wrong with the above...
 
Last edited by a moderator:
Joined
Jun 25, 2005
Messages
73
Reaction score
0
Jon,

thank you for your quick reply.
Any reason not to have more than one ArrayList? You could have multiple
lists containing the same elements and sort each one independently.

That is exactly what I was looking for. So far, I was not sure if an
ArrayList
stores copies of the objects I put into it or just references to them.

So now I suppose that the ArrayList stores references, right?

By the way, I did not inted to create an ArrayList that is equal to the one
which is
already included in the framework. ;-)

Regards,
--kmk
 
J

Jon Skeet [C# MVP]

That is exactly what I was looking for. So far, I was not sure if an
ArrayList stores copies of the objects I put into it or just references to
them.

So now I suppose that the ArrayList stores references, right?
Yes.

By the way, I did not inted to create an ArrayList that is equal to the one
which is already included in the framework. ;-)

Goodo.

For more information about value types and reference types, and
parameter passing, see

http://www.pobox.com/~skeet/csharp/parameters.html
 
Last edited by a moderator:

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