简体   繁体   中英

C# public variables problem

This is a noob question

using System.name;

class class_name
{
    private className Obj;

    public class_name()
    {
    }

    public function()
    {
       Obj.function     <----- why i cant acesss the global varible here ??
    }
}

When i type the class the instellisence docent show any thing:-s

I'm assuming there was just some confusion with the names and, by function , you meant class_name , or instead of class_name you meant className .

In order to access a method this way, it must be declared as static. Otherwise, you must first create an instance of the class and access the method through the instance.

EDIT The code you posted is very confusing. The following works just fine for me.

class Class1
{
    public void Function1()
    {
    }
}

class Class2
{
    private Class1 obj;

    public void Function2()
    {
        obj.Function1();
    }
}

Have you instantiated that class?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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