字段初始值设定项不能引用非静态字段

时间:2014-06-12 13:21:54

标签: c# methods

我试图了解全局变量的工作原理以及如何修改它们。这是我的代码,但是当一些变量是函数并且需要调用它们时,我完全感到困惑。这是我写的代码:

public class Globals
{
  public Boolean Ready = false;
  public TcpClient Client = new TcpClient(server, port);
  public NetworkStream NwStream = Client.GetStream();
  public StreamReader Reader = new StreamReader(NwStream);
  public StreamWriter Writer = new StreamWriter(NwStream);
}

public void Main()
{
  // How do I access to Globals.Writer?

  Writer.WriteLine("Hello World.");
  Writer.Flush();

  // How do I access to Globals.Reader?
  while ((inputLine = Reader.ReadLine()) != null)
  {
    Console.WriteLine(inputLine);
    ParseData(inputLine);
  }
}

public static void ParseData(string data) {
  if (data.Length > 4)
  {
    if (data.Substring(0, 4) == "TRUE")
    {
      Globals globals = Globals();
      globals.Ready = true;
      // How do I access to Globals.Writer?
      Writer.WriteLine("Hello World");
      Writer.Flush();
    }
  }
}

3 个答案:

答案 0 :(得分:2)

由于Globals中的字段不是静态的,因此您需要一个Globals实例来访问它们。 Globals的每个实例都有自己的一组字段。

Globals g = new Globals();

然后,您可以访问此实例的字段:

g.Reader, g.Writer, etc

但是,您也可以使用静态字段。如果所有成员都是静态的,您还可以将Globals设为静态类:

public static class Globals
{
  public static Boolean Ready = false;
  public static TcpClient Client = new TcpClient(server, port);
  public static NetworkStream NwStream = Client.GetStream();
  public static StreamReader Reader = new StreamReader(NwStream);
  public static StreamWriter Writer = new StreamWriter(NwStream);
}

然后,您可以通过提供类名来访问这些字段:

Globals.Reader, Globals.Writer, etc

答案 1 :(得分:1)

忽略我认为你真的想在这个实例中使用static(或者可能是单个对象),你只是错过了Globals上的new(你试图使用Globals作为函数)然后你可以简单地访问Global实例的属性(实际上是一个名称错误的局部变量):

      Globals globals = new Globals();
      globals.Ready = true;
      // How do I access to Globals.Writer?
      globals.Writer.WriteLine("Hello World");
      globals.Writer.Flush();

您正在采取的方法不是创建“全局变量”。您只是在Global函数中创建一个名为ParseData的对象并使用它的属性。

这将不允许您从该功能外部访问属性。

*注意:您通常不会以全局方式使用这些类型的对象

这里不需要Globals:

更好的解决方案是根据需要创建,传递和使用对象。在您显示的示例中,您根本不需要“Globals”:

public void Main()
{
  Boolean Ready = false;
  TcpClient client = new TcpClient(server, port);
  NetworkStream nwStream = Client.GetStream();
  StreamReader reader = new StreamReader(NwStream);
  StreamWriter writer = new StreamWriter(NwStream);

  writer.WriteLine("Hello World.");
  writer.Flush();

  // How do I access to Globals.Reader?
  while ((inputLine = reader.ReadLine()) != null)
  {
    Console.WriteLine(inputLine);
    ParseData(inputLine, writer);
  }
}

// ParseData is not well named, as it both parses data AND writes it to a stream
// You should name them after what they actually do (e.g. ParseAndStoreTrueData)
public static void ParseData(string data, StreamWriter writer) 
{
  if (data.Length > 4)
  {
    if (data.Substring(0, 4) == "TRUE")
    {
      writer.WriteLine("Hello World");
      writer.Flush();
    }
  }
}

答案 2 :(得分:1)

不要把"全局"在一个单独的课堂里。

将它们置于任何方法之外,它们将是当前类的全局,这应该足够了。

在您需要它们之前不要实例化它们,特别是当它们需要参数时。这样,您可以将它们用于多种方法以用于不同目的。

示例:

public Boolean _Ready;
public TcpClient _Client;
public NetworkStream _NwStream;
public StreamReader _Reader;
public StreamWriter _Writer;

public void Main()
{
    _Writer = new StreamWriter(_NwStream);
    _Writer.WriteLine("Hello World.");
    _Writer.Flush();

    while ((inputLine = _Reader.ReadLine()) != null)
    {
        Console.WriteLine(inputLine);
        ParseData(inputLine);
    }
}

public static void ParseData(string data)
{
    if (data.Length > 4)
    {
        if (data.Substring(0, 4) == "TRUE")
        {
            _Ready = true;
            _Writer.WriteLine("Hello World");
            _Writer.Flush();
        }
    }
}

这段代码不会编译,但我希望你能得到一般的想法。

相关问题