从表单中访问main中的实例 [英] accessing an instance in main from a form

查看:159
本文介绍了从表单中访问main中的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *您好,我想让我的第一个c#应用程序在视觉工作室。我已经在main中创建了一个类和一个类的实例,我只是试图在窗体上点击事件中查询该实例的成员,但它告诉我实例名称在当前上下文中不存在。任何帮助将感激这里的我的代码。
* /

/* Hi i am trying to make my first c# application on visual studio. I have created a class and an instance of that class in main and i am simply trying to query a member of that instance inside a click event on a form but its telling me the instance name does not exist in the current context. Any help would be appreciated here's my code. */

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;

    namespace WindowsFormsApplication10
    {

     public class character         // this is my class
        {
            public bool hair_black;
        }


        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());

                character deviljin = new character();  // instance of my class

                deviljin.hair_black = true;      // initiating a member of the instance


            }
        }
    }

    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 WindowsFormsApplication10
    {


        public partial class Form1 : Form
        {

            int cs1 = 0,cs2=0;


            public Form1()
            {
                InitializeComponent();


            public void pictureBox1_Click(object sender, EventArgs e)
            {

                flowLayoutPanel1.Visible = true;
                if (deviljin.hair_black == true)   // trying to access instance member
                {                                  // but getting deviljin does not
                                                   // exist in the current context
                    pictureBox28.Visible = false;
                }            

            }

        }
    }


推荐答案

您的实例变量 deviljin 对于main方法是本地的,不能在该方法之外直接引用。

Your instance variable deviljin is local to the main method and cannot be directly referenced outside of that method.

您需要传递Form1实例中的引用,您正在尝试使用它。
这可以通过form1的构造函数直接传递引用。

You need to pass the reference inside the instance of Form1 where you are trying to use it. This could be done passing the reference directly via the constructor of form1

 Application.Run(new Form1(deviljin));

当然将Form1的构造函数更改为

and of course changing the constructor of Form1 to

private character _devReferenceToCharacterPassed

public Form1(character mycharInstance)
{
    _devReferenceToCharacterPassed = mycharInstance;
    InitializeComponent();
}

现在您可以在点击代码中使用传入的实例

now you can use the passed in instance in your click code

public void pictureBox1_Click(object sender, EventArgs e)
{
    flowLayoutPanel1.Visible = true;
    if (_devReferenceToCharacterPassed.hair_black == true)   
    {                                  
         pictureBox28.Visible = false;
    }            
}

也注意到


Application.Run(new Form1(deviljin));

Application.Run(new Form1(deviljin));

阻塞调用。这意味着您的代码不会退出此调用,直到Form1打开,因此您需要在Run调用之前移动创建 deviljin 变量。

is a blocking call. It means that your code doesn't exit from this call till the Form1 is open, so you need to move the creation of the deviljin variable before the Run call.

编辑:我强烈反对在整个应用程序中使用具有全局可见性的变量。有时它们是必要的,但是它们导致非常快地创建不可维护的代码。最好学习使用尽可能少的全局变量进行编程

I'm strongly opposed to use of variables with global visibility across an application. Sometimes they are necessary, but they lead to the creation of unmaintainable code very quickly. It 'better to learn to program with the fewest possible global variables

这篇关于从表单中访问main中的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆