473,881 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C#: value of one object's private variable affects another object (but it shouldn't)

17 New Member
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine

This is my first post, though I've "lurked" for a while because I find these forums very helpful.

Ok my problem is the following. I have a class that contains a "MakeByteAr ray" function.
I have many objects of that class. Inside that function, I have a private variable,
that is NOT static. It seems that when I put all these objects in other objects, and
then into a list, and I iterate through every object, the PRIVATE variable that i use
in "MakeByteAr ray" is altered depending on what the value was in the previous object.
This makes no sense since the objects don't know about each other, and the variable is
not static.
In short, the following happnes:

Print contents of object 1; object 1 has private variable "priv_var" that is a short
and has a value of "X"; correct contents printed

Print contents of object 2; object 2 has private variable "priv_var" that is a short
and has a value of "Y"; incorrect contents printed; "Y" should have been printed, but,
"Y+Z" was printed instead

OR, if I reset priv_var to 0 at end of MakeByteArray function in object 1, then the value
(0) carries over to object 2's priv_var (when it shouldn't)

How do I know that object 2's "priv_var" got the value "Y+Z" from object 1, and NOT from
somewhere else? I did tests. When object 1's value changed, so did object 2's. I tried
many different (short) values, and the results were the same. However, this doesn't
happen always. I know *when* it happens, but I don't know *why*.
I this summarizes my problem, however, if you want to see more
(very long), including code, scroll down.
I'd appreciate any help with this.
Thank you.

Before you start, I apologize in advance for the formatting (or lack of) of my code.
Now, my code. This is the code for the class whose objects are put into another object.

Expand|Select|Wrap|Line Numbers
  1. namespace ConsoleApplication1
  2. {
  3.     public class MyPara : MyFormat
  4.     {
  5.         public static long totalSum = 0; // public for testing purposes
  7.         protected short rc; 
  9.         public MyPara()
  10.         {
  11.         /* 
  12.         various variables initialized here 
  13.         */
  15.             rc = 0;
  16.         }
  18.     /* 
  19.     more constructors here; all of them do rc = 0;
  20.     multiple (irrelevant) functions here as well;
  21.     */
  23.     public bool IncRc()
  24.     {
  25.         rc++;
  26.     }
  28.     public byte[] MakeByteArray()
  29.         {
  30.             short rc2 = rc;
  31.             totalSum = totalSum + rc2;
  32.             buffer = new byte[byteLength]; // byteLength is defined in a parent class
  34.             recordCount2 = IPAddress.HostToNetworkOrder((short)(rc << 4));
  35.             byte[] temp_ba = BitConverter.GetBytes(rc2);
  37.             if ((temp_ba[0] == 0x00) && (temp_ba[1] == 0x00)) // used to check if value of '0' is present
  38.                 Console.WriteLine("bad!!!!!!!!"); // code never reaches this line
  40.         System.Buffer.BlockCopy(temp_h, 0, buffer, 0, 2);
  41.         /*
  42.         copy more stuff into buffer using System.Buffer.BlockCopy here;
  43.         when done copying, return buffer;
  44.         */
  46.     // rc = 0; <--- when this line is included, totalSum is correct, but many objects then use this value instead of their own
  47.             return (buffer);
  48.         }
  49.     }
  50. }
Before I go onto my next class... Why do I declare another variable (rc2) that I just give the
value of rc? It was for a test. The same thing happens if i use the variable "rc" and rc2",
the results I get are identical.
now, this following class is a class whose objects "contain" the objects of the above class
Expand|Select|Wrap|Line Numbers
  1. namespace ConsoleApplication1
  2. {
  3.     public class MyPage : MyPageFormat
  4.     {
  5.         protected static short page_number = 0;
  7.         private List<MyPara> paras;
  9.         public MyPage()
  10.         {
  11.         /*
  12.         initializes a bunch of variables
  13.         */
  14.         paras = new List<MyPara>();
  15.         }
  17.     /*
  18.     more constructors go here; all of them do paras = new List<MyPara>();
  19.     multiple (irrelevant) functions here as well
  20.     */
  22.         public bool AddPara(MyPara mp) // returns true if paragraph was added successfully, false otherwise
  23.         {
  24.             if ((byteLength + mp.ByteLength) <= maxSize) // maxSize defined through constructor or parent class
  25.             {
  26.                 paras.Add(mp);
  27.                 byteLength = (short)(byteLength + mp.ByteLength); // byteLength defined in parent class
  28.                 page_length = page_length + mp.ByteLength; // page_length defined in parent class
  29.                 return (true);
  30.             }
  31.             else
  32.                 return (false);
  33.         }
  35.         public override void WriteToStream(MemoryStream ms)
  36.         {
  37.             BinaryWriter bw = new BinaryWriter(ms);
  38.         /*
  39.         write a bunch of private variables;
  40.         ex: bw.Write(var_1); bw.Write(var_2); etc.
  41.         */
  43.             for (short z = 0; z < paras.Count; z++)
  44.                 bw.Write(paras[z].MakeByteArray());
  46.             ms.Position = 0;
  47.         }
  48.     }
  49. }
This class basically contains objects that are of type MyPara. As you can see, we can either
write the paragraph to a byte array (using MyPara's MakeByteArray() ) or we can write the
whole page to a stream (using MyPage's WriteToStream which calls each MyPara's MakeByteArray() ).

Now, the class that does stuff with the above classes:
Expand|Select|Wrap|Line Numbers
  1. namespace ConsoleApplication1
  2. {
  3.     public class ppTest
  4.     {
  5.     /*
  6.     constructors go here;
  7.     various other functions here as well;
  8.     */
  10.         public void TestSchedulePP()
  11.         {
  12.         List<MyPage> pages = new List<MyPage>();
  13.         /* 
  14.         code to 'fill' pages list goes here; 
  15.         pages contain multiple 'paragraphs';
  17.         IMPORTANT: At this point, before putting paragraphs
  18.         into pages, each paragraph's private rc variable was
  19.         checked and it DID have the CORRECT value
  20.         */
  22.         //pages are now 'filled' with 'paragraphs'
  23.         //write them to a file
  24.         ToFile<MyPage>(pages);
  25.     }
  27.         public void ToFile<E>(List<E> pages) where E : MyPageFormat // MyPageFormat is parent class of MyPage listed above
  28.         {
  29.             byte[] barray;
  30.             FileStream fs = new FileStream("mypages.bin", FileMode.Append);
  31.             MemoryStream ms;
  32.             BinaryWriter bw = new BinaryWriter(sp);
  34.             for (int e = 0; e < pages.Count; e++)
  35.             {
  36.                 ms = new MemoryStream();
  38.                 pages[e].WriteToStream(ms);
  40.                 barray = new byte[ms.Length];
  41.                 ms.Read(barray, 0, barray.Length);
  42.                 bw.Write(barray);
  43.                 ms.Flush();
  44.                 ms.Close();
  45.                 barray = null;
  46.             }
  48.             bw.Close();
  49.             sp.Close();
  50.         }
  51.     }
  52. }
The first function basically "fills" my list of pages, and then calls ToFile which prints the
contents of the list (all pages including all paragraphs).

Finally, my main program code:
Expand|Select|Wrap|Line Numbers
  1.             ppTest pp = new ppTest();
  2.             pp.TestSchedulePP();
So, after all that, the flow looks like this:
Main program --> TestSchedulePP( ) in ppTest --> ToFile() in ppTest --> WriteToStream() in MyPage --> MakeByteArray() in MyPara

I have ensured that after I fill my pages, I NEVER call IncRc(). In other words, between each
WriteToStream() call (or each MakeByteArray() call), I DO NOT call IncRc at all. The last IncRc() call occurs BEFORE the pages are "filled" with paragraphs.

To reiterate my question, the problem value/variable is in MyPara. "rc" should NEVER have the value of the previous object, but sometimes, it does! I don't understand how a bunch of objects of MyPara that are in an object of MyPage can know about each other, and use each other's values.
So when I am going through the loop in MyPage (in WriteToStream() ), I call each MyPara's MakeByteArray() , but why would one have the value of the previous one? For example, how can the rc var in paras[z].ToByteArray() have the same value as paras[z+1].ToByteArray() ??? I realise that if it happens that BOTH paragraphs had the same number of IncRc() calls, the rc value will be the same, but they don't. In fact, I check all the rc values (of each paragraph) before putting them in pages and they are ALL correct. So how does the rc value of object 1 get into the rc of object 2??????

Two things to add: I know "rc" is a 16-bit number and I am shifting it by 4 bits. I never increment rc past a 12-bit number (checked) so thats ok. Also, if you look at the last commented line in MakeByteArray() in MyPara, when it is uncommented the totalSum is correct, but many paragraphs have rc = 0 which should never happen. When the line is left commented, like in my example above, no paragraphs have rc = 0 (which is correct), but totalSum is wrong.

I am sorry that this was very long winded.
As I said above, I would appreciate any assistance with this problem.
Thank you.
Sep 11 '07 #1
0 2052

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

Similar topics

by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the graphics object of the form/pictureBox. Should It be better if make a graphics object from my pictureBox in load event handler of the form and store it as member variable of the form , make
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any typos I am a newbie here ;-)
by: Patrick | last post by:
Hi I have the following problem. When starting my asp.net application, i read a encrypted string from a file, decrypt it and want this values to be available in the complete application. they should be stored in a global variable, but it shouldn't be possible to modify this variable. so what i tried first was using the application object to store that string. that was fine, but the problem is, that it can be modified from outside, so...
by: hb | last post by:
Hi, I need to declare a variable who's value can be preserve through the same ASP.Net page. I tried the following code, only the static variable s2 keeps its value=22 after lnk1_Click followed by lnk2_Click(). The others return to their original values initiated during their declaration. But the static variable's life doesn't end when the page is closed and it causes
by: summer00 | last post by:
Hi everyone, I found that the value of a variable(string type for example) is lost after the aspx page postback. E.G: private void Page_Load(object sender, System.EventArgs e) {
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset): Public Class DatePicker2...
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right thing to do. And I wonder why VB .NET keeps silence and makes such function return some default value instead. Isn't it error-prone? -- Dmitriy Lapshin
by: ST | last post by:
Hello everyone, Can anyone help me with this error above when I debug my web app project in vstudio.net?? I can't figure it out! It was working fine for months, and now all of a sudden it's not!! This is the error: biopsy.searchsubject.btnSubject_Click(Object Sender, EventArgs e) in C:\INetPub\WWWRoot\biopsy\searchsubject.aspx.vb:198 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On http://www.webreference.com/programming/javascript/gr/column9/ they say: <snip> The undefined property A relatively recent addition to JavaScript is the undefined property.
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.