开发者

C# - Passing struct from one object to another

I'm trying to pass a struct from one object to another. I have the f开发者_JAVA百科ollowing 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

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?

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.


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. 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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜