473,513 Members | 2,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested Struct Bug??

Please help me, add a button to a new for and copy and paste the simple code
below. According to me, the last message box in the Click event of the
button should popup a "123" but instead it pops up a "0". Why?? Is this a
bug?? Thank you.

---------------- CODE TO COPY ----------------

public struct struct1
{
public int a;
public struct2 b;
}

public struct struct2
{
public int x;
}

private void button1_Click(object sender, System.EventArgs e)
{
struct1[] arrayWithStructs = new struct1[1];
struct2 s2 = arrayWithStructs[0].b;
s2.x = 123;

MessageBox.Show(s2.x.ToString()); // Good.
MessageBox.Show(arrayWithStructs[0].b.x.ToString()); // Bad
}
Nov 16 '05 #1
2 1113
Rene <no****@nospam.nospam> wrote:
Please help me, add a button to a new for and copy and paste the simple code
below. According to me, the last message box in the Click event of the
button should popup a "123" but instead it pops up a "0". Why?? Is this a
bug?? Thank you.


It's not a bug - it's because structs are value types. When you do

struct s2 = ...;

you're creating a new copy of the value which is then completely
separate from the array.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Got it, thank you.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Rene <no****@nospam.nospam> wrote:
Please help me, add a button to a new for and copy and paste the simple code below. According to me, the last message box in the Click event of the
button should popup a "123" but instead it pops up a "0". Why?? Is this a bug?? Thank you.


It's not a bug - it's because structs are value types. When you do

struct s2 = ...;

you're creating a new copy of the value which is then completely
separate from the array.

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

Nov 16 '05 #3

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

Similar topics

9
8611
by: Joolz | last post by:
Could someone please give me an example of how to make pointers to nested structures? Pointers to normal structures are no problem... struct structA { int a; }structA_s; structA *pStructA =...
6
2165
by: jwvai316 | last post by:
I don't really know how to say it so I just say it a nested linklist. How do you make LinkLists inside LinkList? Can anyone help me for this? I think an example program will help me a lot. ...
11
1963
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
0
1335
by: Kurt Ng | last post by:
Need help quick!!! Am really stuck on this problem! I have a C dll, and it uses a nested struct. (see below) The struct has 2 layers of nested structs. The first field in the first struct is...
2
1676
by: symbol | last post by:
I am having this problem in a managed c++ DLL which mixes managed and unmanaged C/C++ code. I tried to assign value to a struct array nested in another struct. but I can only write to the first...
1
4130
by: symbol | last post by:
I am having this problem in a managed c++ DLL which mixes managed and unmanaged C/C++ code. I tried to assign value to a struct array nested in another struct. but I can only write to the first...
8
5898
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
4
2287
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
5
3720
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
4
2757
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.