简体   繁体   English

在另一个类中使用实例化的类

[英]Using an Instantiated Class in another Class

Please excuse my ignorance, I am transitioning from VB6 to C# (very steep learning curve). 请原谅我的无知,我正在从VB6过渡到C#(学习曲线非常陡峭)。 I have googled this to death and I am not able to figure this out. 我已经用谷歌搜索将其杀死,但我无法弄清楚。 I instantiated a Class on my main form: 我在主要形式上实例化了一个类:

namespace App1_Name
{
    public partial class Form1 : Form
    {
        public cConfig Config = new cConfig();
    }
}

In my Config Class I am instantiating two other classes: 在我的Config类中,我将实例化另外两个类:

namespace App1_Name
{
    class cConfig
    {
        //Properties
        public cApplication Application = new cApplication();
    }
}

In my cApplications Class I have the following: 在我的cApplications类中,我具有以下内容:

namespace App1_Name
{
    class cApplication
    {
        //Properties
        public string ApplicationName { get { return "App Name"; } }
    }
}

So in another class I am looking to use the class I instantiated on Form1 as so: 因此,在另一个类中,我希望这样使用在Form1上实例化的类:

namespace App1_Name
{
    class cXML
    {
        public void Method1()
        {
            Console.WriteLine(Config.Application.ApplicationName);)
        }
     }
}

But I am getting an error that states "Config" doesn't exist in the current context, what am I doing wrong here? 但是我收到一条错误消息,指出当前上下文中不存在“ Config”,这是我在做什么错?

Don't you miss instance of Form1? 您不错过Form1的实例吗?

Form1 form = new Form1();    
Console.WriteLine(form.Config.Application.ApplicationName);

because you are working with properties... not static classes and static methods. 因为您正在使用属性,而不是静态类和静态方法。

I think you want this: 我想你想要这个:

Console.WriteLine(Form1.Config.Application.ApplicationName);

EDIT: Dampe is correct; 编辑:Dampe是正确的; you need an instance of Form1, since Config is not a static member of the class. 您需要Form1的实例,因为Config不是该类的静态成员。 Please refer to his answer. 请参考他的答案。

以上所有内容,或单行:

Console.WriteLine(new Form1().Config.Application.ApplicationName);

Ok, in order to get my original code to work I made the cApplication Static. 好的,为了使我的原始代码正常工作,我将cApplication设置为静态。 This is because I was instantiating the cXML in Form1's Form_Load event so the above solutions just created an endless loop. 这是因为我在Form1的Form_Load事件中实例化了cXML,所以上述解决方案只是创建了一个无限循环。

What I did to resolve the issue was to pass the Config Class as a reference to the cXML class as follows: 我要解决的问题是按如下方式将Config类作为对cXML类的引用:

namespace App1_Name
{
    public partial class Form1 : Form
    {
        public cConfig Config = new cConfig();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        cXML XML = new cXML();
        XML.Config = Config; //Passing the Config Class by Reference to cXML
    }
}

In the cXML class I am doing the following: 在cXML类中,我正在执行以下操作:

namespace App1_Name
{
    class cXML
    {
        public cConfig Config;

        public string AppName
        {

            Console.WriteLine(Config.Application.AppName);
        }
    }
}

This does exactly what I originally set out to do. 这正是我最初打算做的。 My new question is is this an acceptable way of doing this? 我的新问题是这样做是否可以接受?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM