Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i made structure into code behind of form1[Design].
C#
struct ImagedetailsStructure
{
   internal UInt32 Imagesize;
   internal UInt16 versionNum;
}

then create object of struct i the form1.cs(code behind of form1[Design]).
C#
ImageHeaderStructure IHS = new ImageHeaderStructure();

then i use this variable in the function that is also made in form1.cs.

C#
public void Imagedetails()
{
   try
   {
       openDialogueBox();
       using (StreamReader sr = new StreamReader(path))
       {
           string ReadImageData = sr.ReadToEnd();
           byte[] readImageDataIntoBytes = StringToByteArray(ReadImageData);
           calculateDataLength = Convert.ToUInt32(readImageDataIntoBytes.Length);
       }
       IHS.Imagesize = calculateDataLength - 16;
       IHS.versionNum = 0x00ff0f;
   }
   catch
   { }
}

the i called this function into class1.

and the variable of struct Imagedetailstructure is imagesize and versionnumber values i want to get.
but if i am creating object of structure and accessing the values of variable its coming 0.
but i want to get the values of IHS.ImageSize. that assigned in imagedetail function.

please help me.
Posted
Updated 20-Nov-13 2:22am
v2
Comments
BillWoodruff 20-Nov-13 14:07pm    
"the i called this function into class1."

Without knowing how you exposed IHS to class1, I don't think we can help you.
bunty swapnil 21-Nov-13 0:06am    
I class i inherit form 1.

class1: form1

then imagedetailsstructure is coming. and when i am writing IHS.Imagesize value is not coming.
i am getting 0.

1 solution

Quite shure you got traped by reference vs. value type problem. Had some discussions about it lately - my rule of thumb: never use struct when you can use class in C#. I see no reason for you using a struct (more likely you come from another "language" like I did...). If you want to pass the pointer to a valuetype arround use ref (or out) keyword in the parameter definition and on the call. But don't, just change to class and it will work as you expect - I assume (you did not show the relevant code - the code where you make the call "into class1").

Off-Topic: Funny, even for quick examples I name all my "objects" (Forms, Classes, ...) with meaningfull names, and for production I have a coding rule (FXCop) that a variable can't be named like the class name + number (like button1 and all the VS designer defaults), cause if someone didn't find the "time" to name the "button" chances are high the quality of the code behind (Click-handler whatever) isn't "good" too...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900