Getting info from classes in another form

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 942 Views - Last Post: 26 February 2013 - 08:54 AM Rate Topic: -----

#16 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,267
  • Joined: 05-May 12

Re: Getting info from classes in another form

Posted 18 February 2013 - 04:20 PM

Huh? What is so hard with:
class Sword
{
    :
}

class CommonData
{
    public List<Sword> Swords = new List<Sword>;
    :
}

class Form1 : Form
{
    CommonData _commonData;

    public Form1(CommandData data)
    {
        _commonData = data;
    }
    :
}

class Form2 : Form
{
    CommonData _commonData;

    public Form2(CommandData data)
    {
        _commonData = data;
    }
    :
}

class Shell
{
    static void Main()
    {
        CommonData commonData = new CommonData();

        var form1 = new Form1(commonData);
        form1.ShowDialog();

        var form2 = new Form2(commonData);
        form2.ShowDialog();
    }
}


Was This Post Helpful? 1
  • +
  • -

#17 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Getting info from classes in another form

Posted 21 February 2013 - 02:50 PM

Hi there,

I did the following:

Quote

1) created the Sword Class();
2) created the Common Class();
3) created the Shell Class();


Then, I added the code to Form1 and Form2:

 public partial class Form1 : Form
    {
        common _commonData;

        public Form1(common data)
        {
            InitializeComponent();
            _commonData = data;

        }
    }
 public partial class Form2 : Form
    {
        common _commonData;
        public Form2(common data)
        {
            _commonData = data;
            InitializeComponent();
        }
    }

But it gives me the following error:

Quote

Inconsistent accessibility: parameter type 'WindowsFormsApplication1.common' is less accessible than method 'WindowsFormsApplication1.Form2.Form2(WindowsFormsApplication1.common)

Was This Post Helpful? 0
  • +
  • -

#18 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,267
  • Joined: 05-May 12

Re: Getting info from classes in another form

Posted 21 February 2013 - 03:02 PM

You'll have to make your call common public because your classes Form1 and Form2 are public and have public constructors that take a class common as a parameter.
Was This Post Helpful? 0
  • +
  • -

#19 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Getting info from classes in another form

Posted 25 February 2013 - 09:08 AM

Hi there!

So, I continue to try doing this..but I got some troubles. Here's my class Sword to test:
public class sword
    {
        int a;
        public int get_a() {
            return this.a;
        }
        public void set_a(int B)/>/> {
            this.a = b;
        }
    }

Common class:
public class common
    {
        public List<sword> sss = new List<sword>();
    }

Then, in Form1:
 private void button1_Click(object sender, EventArgs e)
        {
            sw[0] = new sword();
            sw[0].set_a(3);
            sw[1] = new sword();
            sw[1].set_a(1);
            _commonData.sss.Add(sw[0]); // this should insert sw[0] = 3 :: but inserts test.sword
           // So, I would like to have: 
          // _commonData.sss.Add(sw[0].get_a());  1)
            var form2 = new Form2(_commonData);
            form2.ShowDialog();

        }


1) If I try to put sw[0].get_a(), it gives me an error saying that I have invalid arguments.
2) If I eventually do this successfully, when I open form2, the form1 still opened..how do I pass again the arguments for him? Maybe a timer after closing form2 to update info from form1?
Was This Post Helpful? 0
  • +
  • -

#20 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,267
  • Joined: 05-May 12

Re: Getting info from classes in another form

Posted 25 February 2013 - 06:57 PM

View PostKeylogger, on 25 February 2013 - 11:08 AM, said:

sw[0] = new sword();
:
_commonData.sss.Add(sw[0]); // this should insert sw[0] = 3 :: but inserts test.sword


Why are you asserting that it should insert sw[0] == 3? Your code clearly showed that you set sw[0] to a sword class, so is exactly what it is inserting.

View PostKeylogger, on 25 February 2013 - 11:08 AM, said:

_commonData.sss.Add(sw[0]); // this should insert sw[0] = 3 :: but inserts test.sword
// So, I would like to have:
// _commonData.sss.Add(sw[0].get_a());  1)


1) If I try to put sw[0].get_a(), it gives me an error saying that I have invalid arguments.

That is because sss is a List<sword>. You can't add an integer to a list of swords.

View PostKeylogger, on 25 February 2013 - 11:08 AM, said:

2) If I eventually do this successfully, when I open form2, the form1 still opened..how do I pass again the arguments for him? Maybe a timer after closing form2 to update info from form1?

Remember that in C# that classes are passed by reference. You are passing Form2 a reference to common, and so when you update values in common either in Form1 or Form2, the change is seen automatically because both forms are accessing the same reference.

I'm sorry if this sounds harsh, but I highly recommend that you go back several chapters in your C# book and review the basic concepts of classes, references, member variables, member methods, and then brush back up on containers. You are wandering into deeper water than you seem capable of handling. I know that some people's philosophies of learning how to swim (or handle a job) is to throw them into the deep end, but there are better ways to learn.
Was This Post Helpful? 1
  • +
  • -

#21 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Getting info from classes in another form

Posted 26 February 2013 - 04:01 AM

I just solve this with your guys help. But i'm not finished yet.
When I change the info in form2, I want to automatically update in form1..when I say automatically is when I close the form2. For now I have a button ("Update") in form1 that work..
But what I really want is something like a timer to iniciate when I close the form2. How can I do this?

This piece of code doesn't work, I know why: because I start a new reference of a new form..and not the actual open.
Form1 form_ = new form1();
form_.Timer3.Enabled = true;


Btw, when this become complete, I'll put my whole code to help in future others with the same problem.
Was This Post Helpful? 0
  • +
  • -

#22 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,267
  • Joined: 05-May 12

Re: Getting info from classes in another form

Posted 26 February 2013 - 06:16 AM

I think a timer is overkill. Since you are calling ShowDialog(), the user can't do anything with Form1 anyway because Form2 is modal. So just update the Form1 UI after the call to ShowDialog(). In pseudo-code:
class Form1 : Form
{
    CommonData _common;

    public Form1(CommonData common)
    {
        _common = common;
        RefreshUI();
    }

    void RefreshUI()
    {
        swordTextBox.Text = _common.Value.ToString();
    }

    void button_Click()
    {
        var form2 = new Form2(_common);
        form2.ShowDialog();
        RefreshUI();
    }
}


Was This Post Helpful? 1
  • +
  • -

#23 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Getting info from classes in another form

Posted 26 February 2013 - 08:54 AM

Hi,

A few moments after my post I found that solution you gave me.

I have everything running perfectly and I did understand what I have to do to communicate between forms.

Thank you all.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2