Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. passing a struct

passing a struct

Scheduled Pinned Locked Moved C#
tutorial
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VanEtienne
    wrote on last edited by
    #1

    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.

    R R 2 Replies Last reply
    0
    • V VanEtienne

      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.

      R Offline
      R Offline
      rakesh_nits
      wrote on last edited by
      #2

      use the struct name which you have defined not the struct keyword.

      1 Reply Last reply
      0
      • V VanEtienne

        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.

        R Offline
        R Offline
        Robert Rohde
        wrote on last edited by
        #3

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

        V 1 Reply Last reply
        0
        • R Robert Rohde

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

          V Offline
          V Offline
          VanEtienne
          wrote on last edited by
          #4

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

          R R 2 Replies Last reply
          0
          • V VanEtienne

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

            R Offline
            R Offline
            rakesh_nits
            wrote on last edited by
            #5

            try using namespace1(in the second file) in datatype use DrawGraph.data

            V V 2 Replies Last reply
            0
            • V VanEtienne

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

              R Offline
              R Offline
              ryancrawcour
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • R rakesh_nits

                try using namespace1(in the second file) in datatype use DrawGraph.data

                V Offline
                V Offline
                VanEtienne
                wrote on last edited by
                #7

                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?

                G 1 Reply Last reply
                0
                • R rakesh_nits

                  try using namespace1(in the second file) in datatype use DrawGraph.data

                  V Offline
                  V Offline
                  virz
                  wrote on last edited by
                  #8

                  nm Thanking You. Sincerely, VIRAL PATEL

                  1 Reply Last reply
                  0
                  • V VanEtienne

                    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?

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    Substitute "namespace1" with the name of the namespace containing the struct. If the namespacec are in different assembplies, you also have to add a reference to the assembly containing the struct. --- b { font-weight: normal; }

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups