Storing a "pointer" in a Tag property

J

Joe

Coming from C++ I'm so used to being able to use pointers in Tag properties
that I'm a little lost.

I want to assign an instance of a class to a Tag property and be able to
change one of it's members. I need this change to take effect in the
original class instance (as it would with a pointer).

How can this be done?

Thanks
 
J

Joe

well it seems I narrowed down my problem some.

I have a control which I assign a class instance to the Tag property. This
is fine.
The class also has a tag property where I store a new struct like this
class.Tag = new mystruct(1, false);

In an event I was to update the boolean member of the struct but it doesn't
take.

myclass = (MyClass)Control.Tag;
ItemInfo iteminfo = (ItemInfo)myclass.Tag;

iteminfo.selected = checkbox.checked;

If I check iteminfo.selected after the above executes it's fine but when I
check the actual myclass and drill down to the selected of the struct the
value is unchanged.

hhmmm.... I must be missing something here....
 
C

Chuck

Joe said:
Coming from C++ I'm so used to being able to use pointers in Tag
properties
that I'm a little lost.

I want to assign an instance of a class to a Tag property and be able to
change one of it's members. I need this change to take effect in the
original class instance (as it would with a pointer).

How can this be done?

Thanks


public class Business
{
...
}

public class User
{
....

public loadList()
{
....
ListViewItem lvi = new ListViewItem()
...
BusinessObject bo = new BusinessObject();
lvi.Tag = bo; //Implicit cast to Object.
}

public ViewTag(ListViewItem lvi)
{
BusinessObject local = (BusinessObject)lvi.Tag; //this will throw if
Tag can't cast to BusinessObject;

local.MyParamerter = GetData();
// As BusinessObjects a class you automatically have 'pointer' type coding.
// That is local == lvi.Tag so changing local also changes the value
referenced by Tag.
}
}

Hope this helps

Chuck
 
J

Joe

Well I fixed it. It seems a struct doesn't work the same as a class. When I
changed the struct to a class everything worked fine.
 
C

Chuck

Joe said:
well it seems I narrowed down my problem some.

I have a control which I assign a class instance to the Tag property. This
is fine.
The class also has a tag property where I store a new struct like this
class.Tag = new mystruct(1, false);

In an event I was to update the boolean member of the struct but it
doesn't
take.

myclass = (MyClass)Control.Tag;
ItemInfo iteminfo = (ItemInfo)myclass.Tag;

iteminfo.selected = checkbox.checked;

If I check iteminfo.selected after the above executes it's fine but when I
check the actual myclass and drill down to the selected of the struct the
value is unchanged.

hhmmm.... I must be missing something here....

structures are value objects. The assignment operator makes a copy of the
'value' and creates an independent object that is:
for structures:
x = y;
y.prop = z;

x.prop != z // no connection between x and y.

for classes
x = y;
y.prop = z;

x.prop == z; !!! // x and y both reference the same object

So change the object in the tag from struct to class and things will work!

Chuck
 
J

Jon Skeet [C# MVP]

Joe said:
Well I fixed it. It seems a struct doesn't work the same as a class. When I
changed the struct to a class everything worked fine.

Structs and classes do indeed work very differently - structs are value
types, and classes are reference types. Understanding the difference
between them is crucial to working effectively in .NET. The following
pages have some information about the difference between the two - I
keep meaning to write an article which is just about this difference,
but I haven't found the time yet...

http://www.pobox.com/~skeet/csharp/parameters.html
http://www.pobox.com/~skeet/csharp/memory.html
 

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

Similar Threads


Top