473,325 Members | 2,480 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,325 software developers and data experts.

Sorry for the basic question, but...

I am "learning" C# and have run into a problem that, though I can work around
it, I would like to know what the *right* way to handle the issue is.

I have created an "Info" struct and assigned certain default values to it.
I have assigned this struct to a TreeView node via the Tag field. In my
"AfterSelect" processing call, I am casting the node tag value to my "Info"
struct. When I look at the fields they are set to the default values that I
set earlier at instantiation time.

If I change the values in the "Info" struct, though, when the "AfterSelect"
callout is returned from and called again, the values are reset back to the
previous default values.

It looks to me that a new object is being created somewhere along the line,
such that in my "AfterSelect" routine I am changing the values of a "new"
object, and no the one initially set as the Tag value of the TreeView node.

My question is, how can I set the Tag of the TreeView control (or any other
control) to "point" to the struct I created at instantiation time? I only
want one copy floating around, and want changes to the object to persist
across each callout.

The answer is probably very simple (and the question itself gives my
newbieness away<g>).

Any help would be appreciated.

Thanks in advance,
-Brian
Nov 17 '05 #1
5 1341
Brian <Br***@discussions.microsoft.com> wrote:
I am "learning" C# and have run into a problem that, though I can work around
it, I would like to know what the *right* way to handle the issue is.
Excellent - that's exactly the right attitude :)
I have created an "Info" struct and assigned certain default values to it.
I have assigned this struct to a TreeView node via the Tag field. In my
"AfterSelect" processing call, I am casting the node tag value to my "Info"
struct. When I look at the fields they are set to the default values that I
set earlier at instantiation time.
Yup, they would be. That's because structs are value types. When you
set the Tag property of the node, you're creating a new copy with the
same values.
If I change the values in the "Info" struct, though, when the "AfterSelect"
callout is returned from and called again, the values are reset back to the
previous default values.

It looks to me that a new object is being created somewhere along the line,
such that in my "AfterSelect" routine I am changing the values of a "new"
object, and no the one initially set as the Tag value of the TreeView node.

My question is, how can I set the Tag of the TreeView control (or any other
control) to "point" to the struct I created at instantiation time? I only
want one copy floating around, and want changes to the object to persist
across each callout.

The answer is probably very simple (and the question itself gives my
newbieness away<g>).


The answer is almost certainly that you should use a class instead of a
struct. Do you particularly *need* value type semantics elsewhere? You
should normally be create your own structs only very rarely - almost
every type you create should (for most apps) be classes.

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

for more information.

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

what is the "right way" to hanle it, depends on, what the "Info" really is.
My question is, how can I set the Tag of the TreeView control (or any
other
control) to "point" to the struct I created at instantiation time? I
only
want one copy floating around, and want changes to the object to persist
across each callout.
If this is the nature of your Info-type, then it surely should by a
reference-type.
Make a class instead of a struct.
It looks to me that a new object is being created somewhere along the
line,
such that in my "AfterSelect" routine I am changing the values of a "new"
object, and no the one initially set as the Tag value of the TreeView
node.
Since your Info-type is a struct and therefore a valuetype and the Tag
property
is of Type object wich is a reference type, the cast to Info is a unboxing
conversion,
wich means, you get an copy of the boxed instance.
If you want to change the Tag property, (and the Info-type has to be a
valuetype), then you've
got to assign the changed value back to the object instance.

Christof

PS: A very good explanation of valuetype <-> referencetype you can find at:
http://www.pobox.com/~skeet/csharp/memory.html

"Brian" <Br***@discussions.microsoft.com> schrieb im Newsbeitrag
news:61**********************************@microsof t.com...I am "learning" C# and have run into a problem that, though I can work
around
it, I would like to know what the *right* way to handle the issue is.

I have created an "Info" struct and assigned certain default values to it.
I have assigned this struct to a TreeView node via the Tag field. In my
"AfterSelect" processing call, I am casting the node tag value to my
"Info"
struct. When I look at the fields they are set to the default values that
I
set earlier at instantiation time.

If I change the values in the "Info" struct, though, when the
"AfterSelect"
callout is returned from and called again, the values are reset back to
the
previous default values.

It looks to me that a new object is being created somewhere along the
line,
such that in my "AfterSelect" routine I am changing the values of a "new"
object, and no the one initially set as the Tag value of the TreeView
node.

My question is, how can I set the Tag of the TreeView control (or any
other
control) to "point" to the struct I created at instantiation time? I
only
want one copy floating around, and want changes to the object to persist
across each callout.

The answer is probably very simple (and the question itself gives my
newbieness away<g>).

Any help would be appreciated.

Thanks in advance,
-Brian

Nov 17 '05 #3
Thanks Jon for your quick reply!

No, I do not need to use structs instead of classes. I came from an
assembler and C background - and structs seem "natural" to me<g>. I
remembered reading in "Inside C#" that structs were not objects - and hence I
presumed that they would be more effecient - if used wisely (which I
evidently was not<g>).

Just to make sure I am really clear on this, the issue is that since the
"Tag" member is of type Object, it "boxes" the struct into an object before
passing it merrily along - this all makes sense to me (hopefully that is
right). Yet, how does one use the Tag field (in numerous controls) to pass,
say, pointers and the like? I know this is "unsafe" code - and I will avoid
doing so - but it would be nice to know how to pass an address to a
structure, array (C style), position in memory, etc. What happens under the
covers when you set the Tag field to a numeric value? (I guess I could look
at the MSIL and see...).

Maybe I am just really missing the proverbial object boat here<g>?

Thanks again!
-Brian

"Jon Skeet [C# MVP]" wrote:
Brian <Br***@discussions.microsoft.com> wrote:
I am "learning" C# and have run into a problem that, though I can work around
it, I would like to know what the *right* way to handle the issue is.


Excellent - that's exactly the right attitude :)
I have created an "Info" struct and assigned certain default values to it.
I have assigned this struct to a TreeView node via the Tag field. In my
"AfterSelect" processing call, I am casting the node tag value to my "Info"
struct. When I look at the fields they are set to the default values that I
set earlier at instantiation time.


Yup, they would be. That's because structs are value types. When you
set the Tag property of the node, you're creating a new copy with the
same values.
If I change the values in the "Info" struct, though, when the "AfterSelect"
callout is returned from and called again, the values are reset back to the
previous default values.

It looks to me that a new object is being created somewhere along the line,
such that in my "AfterSelect" routine I am changing the values of a "new"
object, and no the one initially set as the Tag value of the TreeView node.

My question is, how can I set the Tag of the TreeView control (or any other
control) to "point" to the struct I created at instantiation time? I only
want one copy floating around, and want changes to the object to persist
across each callout.

The answer is probably very simple (and the question itself gives my
newbieness away<g>).


The answer is almost certainly that you should use a class instead of a
struct. Do you particularly *need* value type semantics elsewhere? You
should normally be create your own structs only very rarely - almost
every type you create should (for most apps) be classes.

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

for more information.

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

Nov 17 '05 #4
Thanks Christof, for your reply.

It looks like my 'C' background and structs got me in trouble<g>. I'll
convert it to a class and all should be well.

Thanks again!
-Brian
"Christof Nordiek" wrote:
Hi Brian,

what is the "right way" to hanle it, depends on, what the "Info" really is.
My question is, how can I set the Tag of the TreeView control (or any
other
control) to "point" to the struct I created at instantiation time? I
only
want one copy floating around, and want changes to the object to persist
across each callout.


If this is the nature of your Info-type, then it surely should by a
reference-type.
Make a class instead of a struct.
It looks to me that a new object is being created somewhere along the
line,
such that in my "AfterSelect" routine I am changing the values of a "new"
object, and no the one initially set as the Tag value of the TreeView
node.


Since your Info-type is a struct and therefore a valuetype and the Tag
property
is of Type object wich is a reference type, the cast to Info is a unboxing
conversion,
wich means, you get an copy of the boxed instance.
If you want to change the Tag property, (and the Info-type has to be a
valuetype), then you've
got to assign the changed value back to the object instance.

Christof

PS: A very good explanation of valuetype <-> referencetype you can find at:
http://www.pobox.com/~skeet/csharp/memory.html

"Brian" <Br***@discussions.microsoft.com> schrieb im Newsbeitrag
news:61**********************************@microsof t.com...
I am "learning" C# and have run into a problem that, though I can work
around
it, I would like to know what the *right* way to handle the issue is.

I have created an "Info" struct and assigned certain default values to it.
I have assigned this struct to a TreeView node via the Tag field. In my
"AfterSelect" processing call, I am casting the node tag value to my
"Info"
struct. When I look at the fields they are set to the default values that
I
set earlier at instantiation time.

If I change the values in the "Info" struct, though, when the
"AfterSelect"
callout is returned from and called again, the values are reset back to
the
previous default values.

It looks to me that a new object is being created somewhere along the
line,
such that in my "AfterSelect" routine I am changing the values of a "new"
object, and no the one initially set as the Tag value of the TreeView
node.

My question is, how can I set the Tag of the TreeView control (or any
other
control) to "point" to the struct I created at instantiation time? I
only
want one copy floating around, and want changes to the object to persist
across each callout.

The answer is probably very simple (and the question itself gives my
newbieness away<g>).

Any help would be appreciated.

Thanks in advance,
-Brian


Nov 17 '05 #5
Brianta <Br*****@discussions.microsoft.com> wrote:
No, I do not need to use structs instead of classes. I came from an
assembler and C background - and structs seem "natural" to me<g>. I
remembered reading in "Inside C#" that structs were not objects - and hence I
presumed that they would be more effecient - if used wisely (which I
evidently was not<g>).

Just to make sure I am really clear on this, the issue is that since the
"Tag" member is of type Object, it "boxes" the struct into an object before
passing it merrily along - this all makes sense to me (hopefully that is
right).
Yup, that's right.
Yet, how does one use the Tag field (in numerous controls) to pass,
say, pointers and the like?
To be honest, I wouldn't like to say - I've never used pointers in
anger before. I basically haven't needed to.
I know this is "unsafe" code - and I will avoid
doing so - but it would be nice to know how to pass an address to a
structure, array (C style), position in memory, etc. What happens under the
covers when you set the Tag field to a numeric value? (I guess I could look
at the MSIL and see...).

Maybe I am just really missing the proverbial object boat here<g>?


If you set the field to a numeric value, that numeric value will be
boxed.

Basically, if you want reference type semantics, use a reference type
:)

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

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

Similar topics

6
by: DH | last post by:
I have a VERY basic question about figuring database size. I've inherited a database which is generally similar to this basic one: Item, Red, Blue, Green, Yellow (text), (int),(int),(int),(int)...
9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
14
by: Agoston Bejo | last post by:
Hi, sorry about the multiple posting, technical difficulties.... ----- What does exactly the size of the int datatype depends in C++? Recenlty I've heard that it depends on the machine's...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
4
by: Stephen Kershaw | last post by:
Hi, I know some basic C and am currently trying to understand some source code. I enclose some relevant snippets of code below. My question is this: The code creates a large array of chars,...
8
by: Vincent RICHOMME | last post by:
Hi, first I would like to apologize about my question because usually I hate specific implementation but in this case I really would like an answer. Besides my example is an example of what's...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.