简体   繁体   English

C# - 将struct从一个对象传递到另一个对象

[英]C# - Passing struct from one object to another

I'm trying to pass a struct from one object to another. 我正在尝试将结构从一个对象传递给另一个对象。 I have the following code: 我有以下代码:

    private void mainMenuStripNewProject_Click(object sender, EventArgs e)
    {
        frmNewProject frmNewProject = new frmNewProject(this);
        if (frmNewProject.ShowDialog() == DialogResult.OK)
        {
            StructProjectSettings tempProjectSettings = frmNewProject.getSettings();
            newProjectEvent(tempProjectSettings);                   //Fetchs settings from the new project form
        }
    }

However, I get the following error: 但是,我收到以下错误:

Error 14 Cannot implicitly convert type 'NathanUpload.frmNewProject.StructProjectSettings' to 'NathanUpload.Main.StructProjectSettings' o:\\daten\\visual studio 2010\\Projects\\NathanUpload\\NathanUpload\\Main.cs 43 错误14无法将类型'NathanUpload.frmNewProject.StructProjectSettings'隐式转换为'NathanUpload.Main.StructProjectSettings'o:\\ daten \\ visual studio 2010 \\ Projects \\ NathanUpload \\ NathanUpload \\ Main.cs 43

The structs in each class are declared public class variables and are identical. 每个类中的结构都声明为公共类变量,并且是相同的。

Thanks for the help in advance! 我在这里先向您的帮助表示感谢!

Have you defined the actual struct inside each of the classes? 你有没有在每个类中定义实际的struct?

Even if you use the same name on them, the compiler won't treat them as the same type. 即使您对它们使用相同的名称,编译器也不会将它们视为相同的类型。

You have to define the struct in one place, and make object of it from both classes that needs to use it. 您必须在一个地方定义结构,并从需要使用它的两个类中创建它的对象。

An example 一个例子

public class A{
  public struct MyStruct{
    ...
  }
}

public class B{
  public struct MyStruct{
    ...
  }
}

A.MyStruct struct1 = new B.MyStruct();

This is not allowed, and this is actually what you are trying to do. 这是不允许的,这实际上是你想要做的。 I suggest moving the struct out of the class and put it somewhere both classes can access it. 我建议将结构移出类,并将它放在两个类都可以访问它的地方。

Define it like this instead 相反,定义它

public struct MyStruct{
  ...
}

public class A{
  ...
}

public class B{
  ...
}

MyStruct struct1 = new MyStruct();

Well, it's not that they have to be indentical, they have to have the same type... two identical struct (same name, same fields, etc...) in different namespaces cannot impolicitly converted from one to another. 好吧,并不是说它们必须是相同的,它们必须具有相同的类型......不同名称空间中的两个相同的结构(相同的名称,相同的字段等)不能强制地从一个转换为另一个。

Either you write an implicit conversion from one struct to another, or you use the same everywhere (which would be the recommended way of doing this). 你可以写一个从一个结构到另一个结构的隐式转换 ,或者你在任何地方都使用相同的转换(这是推荐的方法)。

Option 选项

  1. Delete the definition of one of the structs, and use the same definition in both places. 删除其中一个结构的定义,并在两个地方使用相同的定义。 Most likely it would be better not to have it as a member of another class is you are using it in more than one place. 最有可能的是,如果你在不止一个地方使用它,最好不要将它作为另一个类的成员。

  2. Create a constructor for one struct that takes an instance of the one being copied to and copies all the values. 为一个结构创建一个构造函数,该结构采用被复制的实例并复制所有值。 Sometimes this is useful (if the structs have different member methods and overlapping but different purposes) but mostly it's just a silly way to waste both developer and execution time. 有时这很有用(如果结构具有不同的成员方法和重叠但不同的目的),但大多数情况下,它只是浪费开发人员和执行时间的愚蠢方式。

Unless you are sure you want option 2 (and you probably wouldn't be asking this question if you were), go for option 1. 除非你确定你想要选项2(你可能不会问这个问题),请选择选项1。

Those are two different structs; 这是两种不同的结构; the fact that they are defined equally does not matter... they are different. 他们被平等定义的事实并不重要......他们是不同的。 You need to use one or the other in your code. 您需要在代码中使用其中一个。

if there are two eparate struct definitions you cannot convert from one to the other just like that no matter how similar or identical they are. 如果有两个eparate结构定义,则无论它们有多么相似或相同,您都无法将它们从一个转换为另一个。 the compiler will flag it as an error. 编译器会将其标记为错误。

the natural thing to do would probably be to only have one struct definition (make it public, define it in its own .cs file) and then use it in both places 自然要做的可能是只有一个结构定义(公开,在自己的.cs文件中定义),然后在两个地方使用它

Alternatively you could have your own conversion method. 或者,您可以拥有自己的转换方法。 If you need to do this "left hand - right hand" stuff a lot then I would definately recommend omething like http://automapper.codeplex.com/ although I believe it only operates on objects. 如果你需要做很多“左手 - 右手”的东西,那么我肯定会推荐像http://automapper.codeplex.com/这样的东西,虽然我相信它只适用于物体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM