passing a struct
-
Hi, can someone please tell me how to send a struct to a different class file. In the other class file (receiving end) you have to specify the type that is sent to it. What must I put there, 'struct' doen't work.
-
Hi, can someone please tell me how to send a struct to a different class file. In the other class file (receiving end) you have to specify the type that is sent to it. What must I put there, 'struct' doen't work.
use the struct name which you have defined not the struct keyword.
-
Hi, can someone please tell me how to send a struct to a different class file. In the other class file (receiving end) you have to specify the type that is sent to it. What must I put there, 'struct' doen't work.
Hard to understand your question but I'll try it:
public struct MyStruct {
//...
}public class MyClass2 {
private MyClass1 _myClass1;
public void DoSomething() {
_myClass1.Send(myStruct);
}
}public class MyClass1 {
public void Send(MyStruct myStruct) {
//...
}
}You have to use the name of the structure (the same as if you would use a normal class).
-
Hard to understand your question but I'll try it:
public struct MyStruct {
//...
}public class MyClass2 {
private MyClass1 _myClass1;
public void DoSomething() {
_myClass1.Send(myStruct);
}
}public class MyClass1 {
public void Send(MyStruct myStruct) {
//...
}
}You have to use the name of the structure (the same as if you would use a normal class).
Works if both classes are in the same file, but I want to do it between 2 different files. //DrawGraph.cs public partial class DrawGraph { public struct Data { public int a; } public void DoSomething() { Draw draw = new Draw(); //Create an instance of Draw.cs class file Data data = new Data(); //Create an instance of Data struct data.a = 5; Image1.ImageUrl = draw.DrawGraph2(data); //Sending struct to Draw.cs class,DrawGraph2 method } } //Draw.cs public class Draw { public String DrawGraph2(@Data@ data) // what must I put between the @ signs { String img = "whatever"; return img; } }
-
Works if both classes are in the same file, but I want to do it between 2 different files. //DrawGraph.cs public partial class DrawGraph { public struct Data { public int a; } public void DoSomething() { Draw draw = new Draw(); //Create an instance of Draw.cs class file Data data = new Data(); //Create an instance of Data struct data.a = 5; Image1.ImageUrl = draw.DrawGraph2(data); //Sending struct to Draw.cs class,DrawGraph2 method } } //Draw.cs public class Draw { public String DrawGraph2(@Data@ data) // what must I put between the @ signs { String img = "whatever"; return img; } }
try using namespace1(in the second file) in datatype use DrawGraph.data
-
Works if both classes are in the same file, but I want to do it between 2 different files. //DrawGraph.cs public partial class DrawGraph { public struct Data { public int a; } public void DoSomething() { Draw draw = new Draw(); //Create an instance of Draw.cs class file Data data = new Data(); //Create an instance of Data struct data.a = 5; Image1.ImageUrl = draw.DrawGraph2(data); //Sending struct to Draw.cs class,DrawGraph2 method } } //Draw.cs public class Draw { public String DrawGraph2(@Data@ data) // what must I put between the @ signs { String img = "whatever"; return img; } }
VanEtienne wrote:
Works if both classes are in the same file, but I want to do it between 2 different files.
as long as the struct is declared as public it should be no problem. same syntax / method as passing a class an instance of another class.
-
try using namespace1(in the second file) in datatype use DrawGraph.data
Gives me: The type or namespace name 'namespace1' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'DrawGraph' could not be found (are you missing a using directive or an assembly reference?) I want to send about 30 variables through the struct, coz with struct its easy and nice to access or change the values of variables. I've read somewhere that its not good to send too much through a struct. Should I just send the variables directly then?
-
try using namespace1(in the second file) in datatype use DrawGraph.data
-
Gives me: The type or namespace name 'namespace1' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'DrawGraph' could not be found (are you missing a using directive or an assembly reference?) I want to send about 30 variables through the struct, coz with struct its easy and nice to access or change the values of variables. I've read somewhere that its not good to send too much through a struct. Should I just send the variables directly then?