Transferring information between forms

The task is as follows. It is necessary that the Form2 form has access to the data created in the stroka class from the Form1 form (in other words, to provide work with an instance of the stroka class) At the end of the listing, create the sroka class, which has a string type variable.

Listing in Form 1.

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
    private stroka str; //делаю ссылку на класс
    public Form1()
    {
        InitializeComponent();
        str = new stroka(); // создаю экземпляр
        str.a = "asdfg";        // вот передать значения переменных без
        textBoxMain.Text = str.a; // проблем, но речь не об этом.
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void buttonMain_Click(object sender, EventArgs e)
    {
        str.a = textBoxMain.Text; // По нажатию кнопки открывается вторая
                    // форма Form2. Данные передаются без      проблем 
                    // Но речь не об этом)))
        Form2 frm2 = new Form2(str); // А вот и ссылка на объект и плавно переходим
                     // ко второму листингу.
        frm2.ShowDialog();
    }
}

class stroka
{
    public string a;
}
}

Listing 2. Form 2.

namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
    private stroka str2; // Делаю указатель

    public Form2(stroka Astr) // И вот тут начинается. Подчеркивает
                // Form2 синей волнистой и пишет… (см текст ниже)
    {

        InitializeComponent();
        str2 = Astr;

    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }
}
}

Here's what he writes:

" Error 1 Availability incompatibility : Availability the type of the "WindowsFormsApplication1.stroka" parameter is lower than the availability of the "WindowsFormsApplication1.Form2.Form2(WindowsFormsApplication1.stroka)"method C:...\Form2.cs"

I would have continued to pick at it myself, but I came across a similar code in the network, then I did it on the fly, and still it turns out to be an error.

Author: αλεχολυτ, 2014-05-18

1 answers

First, open the class for other modules.

public class stroka  
{  
     public string a;  
}

If you do not specify the visibility zone, the class automatically implicitly becomes private, i.e. it is visible only inside the module in which it is described.


Secondly, if you use the stroka class in several modules of the program, it is more reasonable to put it in a separate module. It won't hurt you.


Third, keep an eye on namespaces. If you have form1 in in the namespace namespace1, and form2 in the namespace namespace2, then they won't see each other until you show the modules these spaces, for example:

using namespace2;  // для формы1
 1
Author: teanЫЧ, 2014-05-20 12:43:21