C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,418 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,557 people online right now. Registration is fast and FREE... Join Now!




Sending a structure in an object array to another form

 

Sending a structure in an object array to another form, Is it possilbe to retrieve information stored in the structure?

Mastergeek666

28 Jun, 2009 - 06:39 PM
Post #1

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 145



Thanked: 1 times
Dream Kudos: 75
My Contributions
Hi again. I store structures in a structure that contains object arrays. Now I send the data within that structure of objects(I'm doing this because it's either send 50 things or five with the double structure) to another form. My question is how do I retrieve the information from the very first structure.
Here is an example:
CODE

public struct Troops
{
    public object[] Regiments;
}
public struct RegimentOne
{
     public string General1;
     public string General2;
     public string General3;
     public string General4;
     //etc, etc, etc...and no i don't know if thats correct army groups or whatever      you get what i mean
     public string troop1;
     public string troop2;
     public string troop3;
     public string troop4;
     public string troop5;
     ///etc, etc, etc...
}
// etc, etc, etc...
//now i send all the regiment structures to the army structure
public void Army()
{
     Troops army = new Troops();
     army.Regiments = new object[] { *all my declared regiments in here };
}
//now i send the objects in the army structure to another form
public void button1_click(object sender, EventArgs e){

Form2 frm2 = new Form2(army.Regiment);
frm2.Show();
this.Hide();
}

So how do I pull the something like 'string troop4' from regimentOne out of the object? Much appreciation.

This post has been edited by Mastergeek666: 28 Jun, 2009 - 06:40 PM

User is offlineProfile CardPM
+Quote Post


janne_panne

RE: Sending A Structure In An Object Array To Another Form

28 Jun, 2009 - 11:06 PM
Post #2

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 527



Thanked: 107 times
My Contributions
Try this in your form2:

CODE

// form2 constructor, something like this
public Form2(object[] arrayOfObjects) {
    int index = 0;
    string str = ((RegimentOne)arrayOfObjects[index]).troop4;
}


User is offlineProfile CardPM
+Quote Post

Mastergeek666

RE: Sending A Structure In An Object Array To Another Form

29 Jun, 2009 - 08:51 AM
Post #3

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 145



Thanked: 1 times
Dream Kudos: 75
My Contributions
QUOTE(janne_panne @ 28 Jun, 2009 - 11:06 PM) *

Try this in your form2:

CODE

// form2 constructor, something like this
public Form2(object[] arrayOfObjects) {
    int index = 0;
    string str = ((RegimentOne)arrayOfObjects[index]).troop4;
}




I keep getting the error 'RegimentOne is a 'field' but is used like a 'type.' I believe this is do to two things. A) RegimentOne is a structure and B)It is declared in form1. For 'B' though i get what you mean. I guess the question now is can something be typecast to structure?


User is offlineProfile CardPM
+Quote Post

janne_panne

RE: Sending A Structure In An Object Array To Another Form

29 Jun, 2009 - 02:07 PM
Post #4

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 527



Thanked: 107 times
My Contributions
I decided to give it a try and see if I can get it to work and it worked fine when I made it like this:

Form1 code:
CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace TestForms {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }


        public struct Troops {
            public object[] Regiments;
        }
        public struct RegimentOne {
            public string General1;
            public string General2;
            public string General3;
            public string General4;
            
            public string troop1;
            public string troop2;
            public string troop3;
            public string troop4;
            public string troop5;
        }

        Troops army;

        public void Army() {
            army = new Troops();
            army.Regiments = new object[2];
            RegimentOne r = new RegimentOne();
            r.troop4 = "troops4";
            army.Regiments[0] = r;          
            army.Regiments[1] = new RegimentOne();
        }

        private void button2_Click(object sender, EventArgs e) {
            Army();
            Form2 frm2 = new Form2(army.Regiments);
        }
    }
}



Form2:
CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestForms {
    public partial class Form2 : Form {
        public Form2(object[] arrayOfObjects) {
            InitializeComponent();
            int index = 0;
            string str = ((TestForms.Form1.RegimentOne)arrayOfObjects[index]).troop4;
            MessageBox.Show(str);
        }
    }
}


str contains text "troops4" in the end as set in the Army() method.

I hope this helps you.
User is offlineProfile CardPM
+Quote Post

Mastergeek666

RE: Sending A Structure In An Object Array To Another Form

29 Jun, 2009 - 08:57 PM
Post #5

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 145



Thanked: 1 times
Dream Kudos: 75
My Contributions
QUOTE(janne_panne @ 29 Jun, 2009 - 02:07 PM) *

I decided to give it a try and see if I can get it to work and it worked fine when I made it like this:

Form1 code:
CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace TestForms {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }


        public struct Troops {
            public object[] Regiments;
        }
        public struct RegimentOne {
            public string General1;
            public string General2;
            public string General3;
            public string General4;
            
            public string troop1;
            public string troop2;
            public string troop3;
            public string troop4;
            public string troop5;
        }

        Troops army;

        public void Army() {
            army = new Troops();
            army.Regiments = new object[2];
            RegimentOne r = new RegimentOne();
            r.troop4 = "troops4";
            army.Regiments[0] = r;          
            army.Regiments[1] = new RegimentOne();
        }

        private void button2_Click(object sender, EventArgs e) {
            Army();
            Form2 frm2 = new Form2(army.Regiments);
        }
    }
}



Form2:
CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestForms {
    public partial class Form2 : Form {
        public Form2(object[] arrayOfObjects) {
            InitializeComponent();
            int index = 0;
            string str = ((TestForms.Form1.RegimentOne)arrayOfObjects[index]).troop4;
            MessageBox.Show(str);
        }
    }
}


str contains text "troops4" in the end as set in the Army() method.

I hope this helps you.


This does in a sense however what i'm doing is placing structures inside the army structure object arrays and what i need to do i first take the structure out of the array then take info like 'string troop4' out of that structure. Essentially take the box out of a box thats in a box(string in a structure thats in a object array). Thanks again for all your help thus far. biggrin.gif

Disregard the upper reply i'm just dumb. lol Thank a mil you have solved my problem. I love you! biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 11:37PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month