C # - transferring a structure from one object to another

I am trying to pass a structure 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

Structures in each class are declared public variables of the class and are identical.

Thanks for the help in advance!

+4
source share
5 answers

Have you determined the actual structure within each of the classes?

Even if you use the same name for them, the compiler will not consider them as the same type.

You must define the structure in one place and make it an object from both classes that must use it.

Example

 public class A{ public struct MyStruct{ ... } } public class B{ public struct MyStruct{ ... } } A.MyStruct struct1 = new B.MyStruct(); 

This is not permitted, and in fact this is what you are trying to do. I suggest moving the structure from the class and placing it where both classes can access it.

Define it like this instead

 public struct MyStruct{ ... } public class A{ ... } public class B{ ... } MyStruct struct1 = new MyStruct(); 
+9
source

Well, this does not mean that they must be indentical, they must be of the same type ... two identical structures (one name, the same fields, etc.) in different namespaces cannot be involuntarily converted from one to another.

Either you write an implicit conversion from one structure to another, or you use the same thing everywhere (which would be the recommended way to do this).

+3
source

Option

  • Delete the definition of one of the structures and use the same definition in both places. Most likely, it would be better not to have it as a member of another class, you use it in several places.

  • Create a constructor for one structure that takes an instance of the one that is being copied and copies all values. This is sometimes useful (if the structures have different member methods and overlapping but different goals), but basically it's just a dumb way to discard both developer time and runtime.

If you are not sure you want option 2 (and you probably would not ask this question if you were), go to option 1.

+2
source

These are two different structures; the fact that they are defined identically does not matter ... they are different. You must use this or that code.

+1
source

if there are two unique descriptions of structures that you cannot convert from one to another in the same way as no matter how similar or identical they are. the compiler will flag this as an error.

Naturally, it would be necessary to have only one definition of the structure (make it public, define it in your own .cs file), and then use it in both places

Alternatively, you can use your own conversion method. If you need to do this “left hand - right hand”, then I would definitely recommend something like http://automapper.codeplex.com/ , although I believe that this only works with objects.

0
source

All Articles