Click here to Skip to main content
14,970,304 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
i created a structure array in c#windows form and i want to transfer this structarray to another form.how can i do this??pls help...
Posted
Updated 6-Aug-14 0:57am
v2
Comments
gggustafson 7-Aug-14 19:29pm
   
Are you talking about a structure containing an array or an array of structures? Are the two forms in the same Visual Studio solution?
Member 10994712 8-Aug-14 0:00am
   
two forms are in same visual studio solution..

struct test

{
public string name;

public int code;


}

test[] array = new test[200];

i want to pass this 'array' from form1 to form2..
gggustafson 8-Aug-14 0:25am
   
See my proposed solution
Member 10994712 8-Aug-14 0:59am
   
and is it possible to convert this 'array' to byte array??


Create a new project, calling it, say, DataStructures. Then declare, within the DataStructures namespace, the following two classes.


C#
// TestStruct.cs

namespace DataStructures
    {

    // ********************************************** class TestStruct

    public class TestStruct
        {

        public struct Test_Struct
            {
            public string name;
            public int code;
            };

         } // class TestStruct

     } // namespace DataStructures


and


C#
// TestArray.cs 

namespace DataStructures
    {
    
    // *********************************************** class TestArray
    
    public class TestArray
        {
        
        public static TestStruct.Test_Struct [ ] test_structs = 
                                  new TestStruct.Test_Struct [ 200 ];

        } // class TestArray

     } // namespace DataStructures


Now in the two forms, use DataStructures as follows. Note that the example is Form1; Form2 code is the same.


C#
// Form1.cs

using System.Windows.Forms;

namespace ArrayOfStructs
    {
    
    // *************************************************** class Form1
    
    public partial class Form1 : Form
        {
        
        // ***************************************************** Form1
        
        public Form1 ( )
            {
            
            InitializeComponent ( );
            
            for ( int i = 0; 
                 ( i < DataStructures.TestArray.test_structs.Length );
                 i++ )
                {
                
                }
            }

        } // class Form1

    } // namespace ArrayOfStructs


Hope that helps.

   
v2
Comments
Member 10994712 8-Aug-14 1:39am
   
but icant declare class teststruct.when i try to declare this it will cause a design error..error is as follows ..The class Form1 can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.
Member 10994712 8-Aug-14 2:30am
   
thank you..thank you so..much..it works fine...
gggustafson 8-Aug-14 11:13am
   
Glad I could help. Please rate and accept the solution.
Member 10994712 12-Aug-14 5:16am
   
i want to convert this structure to byte array..how it possible??i tried one code but it give an error that Type 'structuretest.Form1+test[]' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
gggustafson 12-Aug-14 10:23am
   
Did you rate my solution 1?
There are several ways. For instance you could make it a public property of the first form.
   
Comments
Member 10994712 8-Aug-14 6:40am
   
i didnt understand??can u explain it in detail..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900