473,320 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Storing a "pointer" in a Tag property

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
Nov 16 '05 #1
5 1815
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....

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:OE**************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #2

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:OE**************@TK2MSFTNGP12.phx.gbl...
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
Nov 16 '05 #3

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:uu**************@TK2MSFTNGP09.phx.gbl...
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....

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:OE**************@TK2MSFTNGP12.phx.gbl...
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


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


Nov 16 '05 #4
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.
"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:uu**************@TK2MSFTNGP09.phx.gbl...
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....

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:OE**************@TK2MSFTNGP12.phx.gbl...
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


Nov 16 '05 #5
Joe <J_no_spam@_no_spam_Fishinbrain.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

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

Similar topics

4
by: McKirahan | last post by:
This is from a previous post entitled "cursor type": "IE 4.0 through 5.5 didn't support the cursor type 'pointer' (only the non-standard 'hand')." How might the following be modified to...
18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
6
by: murgan | last post by:
Hi people, i am new to this group,this is my first query, friends i want to know the difference between "function pointer" and "pointer to a function" in c lang, so friends please send the...
8
by: solarin | last post by:
Hi all. I'm writting a logger class to write all the debug/info/warning/error messages in a file. Every time a class needs to send any message, should send a code (int) and a message (string)....
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
1
by: piotr.korzeniewski | last post by:
I'm really confused with this one, first please take a look at code below: function Class(obj) { if(!obj) obj = new Object(); obj.Extend = function(obj) { this.__parent__ = new Object();
20
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
3
by: Renzr | last post by:
I have a C++ package which works very well in the 32-bit Linux-like OS. However, it will lead to a "*** glibc detected *** ./ex2: munmap_chunk(): invalid pointer" in 64-bit (Fedora 7-64), when it...
26
by: Prisoner at War | last post by:
Hello, Everyone: Is there any other values or whatever-it's-called to the cursor property in CSS?? So far, I only know of cursor:pointer (or, for MSIE, cursor:hand).... Any way to load in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.