C#Can';t从与在中实例化的方法不同的方法访问类实例
我遇到的问题可能是一个相当明显的问题,但我对c#和一般的编码都是新手。我在网上到处找了找,但找不到任何对我有帮助的东西。这是我为我所处的情况准备的演示代码C#Can';t从与在中实例化的方法不同的方法访问类实例,c#,C#,我遇到的问题可能是一个相当明显的问题,但我对c#和一般的编码都是新手。我在网上到处找了找,但找不到任何对我有帮助的东西。这是我为我所处的情况准备的演示代码 class Program { class Person //The class { public string jobTitle = "Cashier"; public void Greet()
class Program
{
class Person //The class
{
public string jobTitle = "Cashier";
public void Greet()
{
Console.WriteLine("Hi, im bob.");
}
}
public static void Main(string[] args) //Method it was instantiated in
{
Person Bob = new Person();
Bob.Greet();
}
public static void OtherMethod() //Method I want to access it in
{
Bob.Greet();
Console.WriteLine(Bob.jobTitle); //"'Bob' does not exist in the current context"
}
}
这对我来说是一个严重的问题,因为我正在处理一些有很多类的东西,我被迫使用临时变量将类数据从Main()传输到另一个方法的参数中。另一方面,我试图访问的数据都不是静态的。我可以在Main()中访问它
我只是想知道我是否遗漏了什么 在课堂上上课没有任何意义(
Person
class inProgram
class)
如果需要访问同一类中的Bob
,请检查下面的代码
public class Person
{
public string jobTitle = "Cashier";
public void Greet()
{
Console.WriteLine("Hi, im bob.");
}
}
public static class Program
{
private Person _bob;
public static void Main(string[] args)
{
_bob = new Person();
_bob.Greet();
OtherMethod();
}
public static void OtherMethod()
{
_bob.Greet();
Console.WriteLine(_bob.jobTitle);
}
}
你的问题涉及范围和背景。从编程开始,通常会出现这些问题 指出第二种方法中无法访问Bob的原因:
class Program
{
class Person //The class
{
public string jobTitle = "Cashier";
public void Greet()
{
Console.WriteLine("Hi, im bob.");
}
}
public static void Main(string[] args) //Method it was instantiated in
{
// Bob is instantiated local to the main method
Person Bob = new Person();
Bob.Greet();
//You didn't call OtherMethod() here
}
public static void OtherMethod() //Method I want to access it in
{
// Since Bob was not passed in as a parameter or accessible via a global var, here you dont have access to it. access it
Bob.Greet();
Console.WriteLine(Bob.jobTitle); //"'Bob' does not exist in the current context"
}
}
在此,以您的示例为基础,提供一个有效的版本:
class Program
{
static Person GlobalBob; // must be static because we are instantiating it inside a static method
class Person
{
public string jobTitle = "Cashier";
public void Greet()
{
Console.WriteLine("Hi, im bob.");
}
}
public static void Main(string[] args) //Method it was instantiated in
{
GlobalBob = new Person();
GlobalBob.Greet();
OtherMethod(); //Call your OtherMethod here otherwise it's not gonna be executed
}
public static void OtherMethod() //Method I want to access it in
{
GlobalBob.Greet();
Console.WriteLine(GlobalBob.jobTitle); //Here we have access to GlobalBob
Console.ReadLine(); //Wait for you to press a key so you can read the output !
}
}
希望有帮助
我遇到的问题可能是一个相当明显的问题,但我对c#和一般的编码都是新手
这是一个明显的问题,但也是初学者常见的问题;我们曾经都是初学者
我在网上到处找了找,但找不到任何对我有帮助的东西
你可能会受益于一个更加专注的学习课程,而不仅仅是浏览互联网,期待最好的结果。考虑投资一本好的初学者书。
我被迫使用临时变量将类数据从Main()传输到另一个方法的参数中
如果不走极端,这是正常和好的。当走极端时,您描述的模式称为“流浪数据”,因为您最终会在方法之间“流浪”数据
过于努力避免不定期数据的程序员最终会在另一端陷入一种糟糕的模式:依赖全局状态
没有一种神奇的方式是“永远正确的”。然而,人们普遍认为方法的操作和返回值主要取决于它们的参数;记住,“这”在逻辑上是一个论点
让我们看看你的代码,让你在还是初学者的时候摆脱坏习惯
class Program
{
class Person
{
class Person
{ ... }
class Program
{ ... }
嵌套类在C#中是合法的,但最常用于高级场景,其中类型是其他类型的实现细节将其拆分为两个非嵌套类
class Program
{
class Person
{
class Person
{ ... }
class Program
{ ... }
让我们来看看这个人:
class Person //The class
不要留下解释显而易见的东西的评论<代码>类人显然是一个类;你不必再说了。我的指导方针是编写清晰的代码,不需要注释来解释它的功能。使用注释解释代码必须存在的原因
公共字段在C#中是一种糟糕的编程实践。使用公共属性,然后CaseItLikeThis
。在C#7中,这非常简洁:
public string JobTitle => "Cashier";
在早期版本的C#中,你会说
public string JobTitle { get { return "Cashier"; } }
继续:
public void Greet()
{
Console.WriteLine("Hi, im bob.");
}
}
优秀的程序员甚至会密切关注最微小的细节。
Console.WriteLine("Hi, I'm Bob.");
好的,这是为人
做的
您实际询问的Program
中的问题是OtherMethod
无法看到局部变量Bob
——应该重命名为Bob
;局部变量是casedLikeThis
通常有三种方法可以解决这个问题
bob
作为参数传递;makeOtherMethod
将Person
作为形式参数bob
成为私有静态字段。在Main
中分配字段,并从其他方法使用它
bob
设为私有实例字段。创建一个newprogram()
,分配bob
字段,并将OtherMethod
作为实例方法。然后,您可以从OtherMethod
访问this.bob
我给你的建议是:用三种方法编写程序。这将是一个很好的实践。
Bob
是在Main
中声明的变量。请重新阅读有关变量范围和可见性的学习资料。如果您没有找到关于范围的任何内容,请参阅以下文章:。基本上,Bob只对声明它的方法可见。“在类中包含类没有任何意义”-您确实可以在其他类中包含类;这些被称为。或者你是说在这种情况下使用嵌套类型没有意义?对于这种情况,它没有意义。但通常很少使用嵌套类。