Click here to Skip to main content
15,888,303 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to combine two DLL into one ? Pin
bug_aonz10-Sep-07 18:32
bug_aonz10-Sep-07 18:32 
AnswerRe: How to combine two DLL into one ? Pin
Virendrak10-Sep-07 19:43
Virendrak10-Sep-07 19:43 
AnswerRe: How to combine two DLL into one ? Pin
Virendrak10-Sep-07 19:51
Virendrak10-Sep-07 19:51 
QuestionHow to download multiple files in the same url? Pin
bug_aonz10-Sep-07 17:15
bug_aonz10-Sep-07 17:15 
QuestionPlaying Sounds through the Speakers Pin
max2929710-Sep-07 16:36
max2929710-Sep-07 16:36 
AnswerRe: Playing Sounds through the Speakers Pin
Rudolf Jan11-Sep-07 0:02
Rudolf Jan11-Sep-07 0:02 
GeneralRe: Playing Sounds through the Speakers Pin
max2929711-Sep-07 12:17
max2929711-Sep-07 12:17 
Questionone object's private variable affecting another object's, but it shouldn't [modified] Pin
dfn10-Sep-07 16:23
dfn10-Sep-07 16:23 
Hi.
My problem is the following. I have a class that contains a "MakeByteArray" 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 "MakeByteArray" 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+X" was printed instead (note that "X" is the value of "priv_var" in object 1)

OR, if I reset priv_var to 0 at end of MakeByteArray() in object 1, then sometimes 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+X" 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.
Oh, forgot to mention, I am using Visual Studio 2005, Windows XP, on an intel machine.
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.


namespace ConsoleApplication1
{
public class MyPara : MyFormat
{
public static long totalSum = 0; // public for testing purposes

protected short rc;

public MyPara()
{
/*
various variables initialized here
*/

rc = 0;
}

/*
more constructors here; all of them do rc = 0;
multiple (irrelevant) functions here as well;
*/

public bool IncRc()
{
rc++;
}

public byte[] MakeByteArray()
{
short rc2 = rc;
totalSum = totalSum + rc2;
buffer = new byte[byteLength]; // byteLength is defined in a parent class

recordCount2 = IPAddress.HostToNetworkOrder((short)(rc << 4));
byte[] temp_ba = BitConverter.GetBytes(rc2);

if ((temp_ba[0] == 0x00) && (temp_ba[1] == 0x00)) // used to check if value of '0' is present
Console.WriteLine("bad!!!!!!!!"); // code never reaches this line

System.Buffer.BlockCopy(temp_h, 0, buffer, 0, 2);
/*
copy more stuff into buffer using System.Buffer.BlockCopy here;
when done copying, return buffer;
*/

// rc = 0; <--- when this line is included, totalSum is correct, but many objects then use this value instead of their own
return (buffer);
}
}
}


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
(MyPara):

namespace ConsoleApplication1
{
public class MyPage : MyPageFormat
{
protected static short page_number = 0;

private List<mypara> paras;

public MyPage()
{
/*
initializes a bunch of variables
*/
paras = new List<mypara>();
}

/*
more constructors go here; all of them do paras = new List<mypara>();
multiple (irrelevant) functions here as well
*/

public bool AddPara(MyPara mp) // returns true if paragraph was added successfully, false otherwise
{
if ((byteLength + mp.ByteLength) <= maxSize) // maxSize defined through constructor or parent class
{
paras.Add(mp);
byteLength = (short)(byteLength + mp.ByteLength); // byteLength defined in parent class
page_length = page_length + mp.ByteLength; // page_length defined in parent class
return (true);
}
else
return (false);
}

public override void WriteToStream(MemoryStream ms)
{
BinaryWriter bw = new BinaryWriter(ms);
/*
write a bunch of private variables;
ex: bw.Write(var_1); bw.Write(var_2); etc.
*/

for (short z = 0; z < paras.Count; z++)
bw.Write(paras[z].MakeByteArray());

ms.Position = 0;
}
}
}


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:

namespace ConsoleApplication1
{
public class ppTest
{
/*
constructors go here;
various other functions here as well;
*/

public void TestSchedulePP()
{
List<mypage> pages = new List<mypage>();
/*
code to 'fill' pages list goes here;
pages contain multiple 'paragraphs';

IMPORTANT: At this point, before putting paragraphs
into pages, each paragraph's private rc variable was
checked and it DID have the CORRECT value
*/

//pages are now 'filled' with 'paragraphs'
//write them to a file
ToFile<mypage>(pages);
}

public void ToFile<e>(List<e> pages) where E : MyPageFormat // MyPageFormat is parent class of MyPage listed above
{
byte[] barray;
FileStream fs = new FileStream("mypages.bin", FileMode.Append);
MemoryStream ms;
BinaryWriter bw = new BinaryWriter(sp);

for (int e = 0; e < pages.Count; e++)
{
ms = new MemoryStream();

pages[e].WriteToStream(ms);

barray = new byte[ms.Length];
ms.Read(barray, 0, barray.Length);
bw.Write(barray);
ms.Flush();
ms.Close();
barray = null;
}

bw.Close();
sp.Close();
}
}
}


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:

ppTest pp = new ppTest();
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.


-- modified at 12:02 Tuesday 11th September, 2007
Questioncreating a mp3 scratching program [modified] Pin
dman1000110-Sep-07 12:50
dman1000110-Sep-07 12:50 
AnswerRe: creating a mp3 scratching program Pin
VirtualVoid.NET10-Sep-07 21:46
VirtualVoid.NET10-Sep-07 21:46 
QuestionArray of Regions. Elements seem to disappear. [modified] Pin
hain10-Sep-07 11:18
hain10-Sep-07 11:18 
AnswerRe: Array of Regions. Elements seem to disappear. Pin
Giorgi Dalakishvili10-Sep-07 11:24
mentorGiorgi Dalakishvili10-Sep-07 11:24 
GeneralRe: Simple code example [modified] Pin
hain10-Sep-07 12:28
hain10-Sep-07 12:28 
GeneralRe: Simple code example Pin
Patrick Etc.10-Sep-07 12:49
Patrick Etc.10-Sep-07 12:49 
GeneralRe: Simple code example Pin
hain10-Sep-07 16:12
hain10-Sep-07 16:12 
QuestionModal Dialog [modified] Pin
kamalesh8210-Sep-07 9:34
kamalesh8210-Sep-07 9:34 
AnswerRe: Modal Dialog Pin
Christian Graus10-Sep-07 9:52
protectorChristian Graus10-Sep-07 9:52 
GeneralRe: Modal Dialog Pin
kamalesh8210-Sep-07 9:58
kamalesh8210-Sep-07 9:58 
GeneralRe: Modal Dialog Pin
Christian Graus10-Sep-07 10:00
protectorChristian Graus10-Sep-07 10:00 
QuestionPicture Box created by code not showing up Pin
Jordanwb10-Sep-07 9:09
Jordanwb10-Sep-07 9:09 
AnswerRe: Picture Box created by code not showing up Pin
Ermak8610-Sep-07 9:38
Ermak8610-Sep-07 9:38 
GeneralRe: Picture Box created by code not showing up Pin
Jordanwb10-Sep-07 9:53
Jordanwb10-Sep-07 9:53 
GeneralRe: Picture Box created by code not showing up Pin
Christian Graus10-Sep-07 9:55
protectorChristian Graus10-Sep-07 9:55 
GeneralRe: Picture Box created by code not showing up Pin
Jordanwb10-Sep-07 9:58
Jordanwb10-Sep-07 9:58 
GeneralRe: Picture Box created by code not showing up Pin
Ermak8610-Sep-07 10:02
Ermak8610-Sep-07 10:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.