473,395 Members | 1,473 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,395 software developers and data experts.

parameters: struct unexpectedly not passed by value

BeemerBiker
I am getting unexpected behavior in parameter passing, and was wondering if by adding "code" to a "public struct", I have made it behave like a class.

Expand|Select|Wrap|Line Numbers
  1.         static void SpinOneCard(board tb, ref OneSavedBoard osb, int SrcCol, int SrcCard, int DesCol, int DesCard)
  2.         {
  3.             tb.nSpins = 0;
  4.             osb.moveto(ref tb , SrcCol, SrcCard, DesCol, DesCard);
  5.             osb.init(ref tb, BoardSeries.Count);
  6.             BoardSeries.Add(osb);
  7.         }
  8.  
The struct "board" is passed by value and then referenced in the call to .moveto and .init. This works as expected and the cards on the board table reflect the new move. Unexpectedly, the program that calls SpinOneCard also has its board changed.

Expand|Select|Wrap|Line Numbers
  1.                 {
  2.                     OneSavedBoard osb = new OneSavedBoard();
  3.                     tb.nSpins++;
  4.                     SpinOneCard(tb, ref osb, iColumn, nCard, i, DesLoc);  
  5.                 }
  6.  
Setting a breakpoint after the last line, I observe that the struct "tb" has been changed by the .moveto. I did not expect that.

I started off with a simple struct for "board" but ended up added all sorts of stuff to it.

Expand|Select|Wrap|Line Numbers
  1.         public struct board
  2.         {
  3.             public column[] ThisColumn;
  4.             public int score;
  5.             public int from;
  6.             public void init()
  7.             {
  8.                 int i;
  9.                 ThisColumn = new column[11];
  10.                 for (i = 0; i < 11; i++)
  11.                     ThisColumn[i].init();
  12. ..etc.. same for that column[] it also contains a lot of code
  13.  


SOLVED - The part I was testing was a reference, everything else was passed by value. SoRRy-mY BAd
Aug 25 '10 #1
0 889

Sign in to post your reply or Sign up for a free account.

Similar topics

48
by: Adam Ruth | last post by:
Hello, Has there ever been any talk to adding named parameters to C? I enjoy using them in my Python and Ada code and can see their usefulness in C. I can envision an implementation where the...
1
by: Natan Vivo | last post by:
I have a struct, and i always use it like "myStruct.Contents" to get it's value... I was wondering if I can make a struct to behave like a value. Something like struct MyStruct { private int...
26
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of...
8
by: Derrick | last post by:
I saw the ref param keyword relating to base types. Just checking, if I pass an object around to other class constructors, there will only be one instance of the object, correct? Thanks! ...
3
by: danbraund | last post by:
Hi everyone, I'm a long time C coder, who is coding his final year project in C++ to run under the MIT click routing system. Being fairly new to the OO side of the language, my problem is this: ...
1
by: qwerty | last post by:
My end goal is to have a script that takes user input from one pageA, submits to pageB which then displays the parameter values entered on PageA. My problem is that the values are being displayed...
11
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
1
by: DaTurk | last post by:
I'm having an issue converting an unmanaged struct to a CLI managed struct. It's a value struct and this cannot be changed. public value struct UpdateItem { public: System::String ...
10
by: Lilith | last post by:
I have a simple class method that receives as its second parameter an unsigned int. But, regardless of what I do it never receives the value that I pass to it. Instead it always comes up 4198769....
3
by: vzrandom | last post by:
Hi guys, I have one easy question for you. I have class with public static variable. I enter value in one form (in this public static variable) and when I passed it to another form in textbox I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.