I
I
Ivan Baturin2020-05-16 14:38:27
C++ / C#
Ivan Baturin, 2020-05-16 14:38:27

How to create a public instance of a class?

I need to create an instance of the Equipment class that will be available throughout the solution, how can I do this? It should be created on the button.
Here is the current definition:

private void btnOk_Click(object sender, EventArgs e)
        {
            lblCurrentRun.Text = $"Текущий заход: {txtRunCount.Text}";

            Equipment eq = new Equipment();
            eq.countRun = Int32.Parse(txtRunCount.Text);
        }

And you have to use it here:
private void btnHelmAdd_Click(object sender, EventArgs e)
        {
            int countRun = Int32.Parse(new string(lblCurrentRun.Text.Where(Char.IsDigit).ToArray()));
            MessageBox.Show($"Текущий забег: {countRun}");
            
            eq.Boots = txtHelmSelect.Text;
            
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2020-05-16
@MrLumus

Do you have code similar to WinForms?
- make a field Equipment eq = default;in the form
- eq = new Equipment();in btnOk_Clickbut not a fact, maybe you can initialize it right away
- btnHelmAdd_Clickyou can use it. in fact, if you initialize (assign an instance by ) in the form constructor, then you can use psnew anywhere , everything that has been said is true until you grow up to multithreading

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question