将winform.cs的struct变量传递给class [英] pass struct variable of winform.cs into class

查看:61
本文介绍了将winform.cs的struct变量传递给class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将结构转换为form1 [Design]背后的代码。

i made structure into code behind of form1[Design].

struct ImagedetailsStructure
{
   internal UInt32 Imagesize;
   internal UInt16 versionNum;
}



然后创建struct i的对象form1.cs(form1 [Design]后面的代码)。


then create object of struct i the form1.cs(code behind of form1[Design]).

ImageHeaderStructure IHS = new ImageHeaderStructure();



然后我在此使用此变量也是在form1.cs中创建的函数。




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

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
   { }
}



我把这个函数调成了class1。



和struct Imagedetailstructure的变量是我想得到的imagesize和versionnumber值。

但是如果我正在创建结构对象并访问变量值即将到来0.

但我想得到IHS.ImageSize的值。在imagedetail函数中分配。



请帮助我。


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.

推荐答案

你是通过引用来获取的。与价值类型问题。最近有一些关于它的讨论 - 我的经验法则:当你可以在C#中使用类时,永远不要使用struct。我认为你没有理由使用结构(更有可能你来自另一种语言,就像我做的那样......)。如果要将指针传递给valuetype arround,请在参数定义和调用中使用ref(或out)关键字。但不要,只需更改为类,它将按预期工作 - 我假设(您没有显示相关代码 - 您将调用的代码转换为class1)。



Off-Topic:很有趣,即使对于快速示例我也用有意义的名称命名我的所有对象(表单,类,...),并且对于生产我有一个编码规则(FXCop)变量不能像类名+数字一样命名(比如button1和所有VS设计器默认值),因为如果有人没有找到时间来命名按钮,那么后面的代码质量很高( Click-handler无论如何也不是好......
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...


这篇关于将winform.cs的struct变量传递给class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆