【问题标题】:Accessing a public byte[] array from another class从另一个类访问公共 by​​te[] 数组
【发布时间】:2016-04-22 12:39:43
【问题描述】:

在我正在编写的程序中,我有一个类,其中有一个我想要访问和使用的公共字节数组。

class HasByte
{
  public byte[] theByteArray = new byte[4];

  public HasByte(IPAddress someAddress)
  {
    theByteArray = someAddress.GetAddressBytes();
  }
}

class WantsByte
{ 
  IPAddress address = IPAddress.Parse("192.168.1.1");
  HasByte theInstance = new HasByte(address);
  //do something with theInstance.theByteArray[2] for example
}

目前,由于某种我想知道的原因,我通过 theInstance.theByteArray 访问的字节数组全为 0。

谢谢。

【问题讨论】:

  • 将类成员声明为公共是不好的做法,最好根据您的需要将其封装为具有 get/set 的属性。至于您的问题,构造函数是否正常工作?你试过调试吗?
  • @FelixAv 我还没有了解 get/set,但我会研究一下。构造函数有效,在实际代码中,我可以访问更多公共变量并保留它们的值。只是数组有问题。
  • 你确定你发布了正确的代码吗?由于theInstance 是使用非静态address 初始化的,因此您的代码将无法编译。也许 WantsByte 是一个方法而不是一个类?

标签: c# class oop scope public


【解决方案1】:

除了我在评论中所说的封装之外,这里的代码应该适合你,注意你不能在声明它的时候初始化实例,所以你把它移到构造函数:

    public class HasByte
    {
        public byte[] theByteArray = new byte[4];

        public HasByte(IPAddress someAddress)
        {
            theByteArray = someAddress.GetAddressBytes();
        }
    }

    public class WantsByte
    {
        IPAddress address = IPAddress.Parse("192.168.1.1");
        HasByte theInstance;
        public WantsByte()
        {
            theInstance = new HasByte(address);
        }

        //do something with theInstance.theByteArray[2] for example
    }

【讨论】:

  • 我已经尝试运行我发布的代码,它工作正常,所以你应该没有任何问题,如果你这样做,请告诉我们
  • 您的代码有效,似乎错误是别的东西。我会继续寻找。谢谢顺便说一句。
【解决方案2】:

在您的类 WantsByte 中,您正试图通过另一个非静态成员 address 初始化成员 theInstance,编译器必须向 Error CS0236 抱怨。您可以将theInstance 初始化移动到构造函数:

class WantsByte
{ 
    IPAddress address = IPAddress.Parse("192.168.1.1");
    HasByte theInstance;

    public WantsByte()
    {
        theInstance = new HasByte(this.address);
    }
}

演示示例:

using System;
using System.Net;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var wants = new WantsByte();    
    }
}

class HasByte
{
    public byte[] theByteArray = new byte[4];

    public HasByte(IPAddress someAddress)
    {
        theByteArray = someAddress.GetAddressBytes();
    }
}

class WantsByte
{ 
    IPAddress address = IPAddress.Parse("192.168.1.1");
    HasByte theInstance;

    public WantsByte()
    {
        theInstance = new HasByte(this.address);

        // do something with theInstance.theByteArray[2] for example
        // Let's print all elements of the array
        Console.WriteLine(String.Join(",", theInstance.theByteArray.Select(o => o.ToString()).ToArray()));
    }
}

给出输出:

192,168,1,1

或者,在WantsByte 类中,您可以将address 设为static 成员,这将保证在首次使用类之前对其进行初始化。然后你可以在theInstance初始化器中引用它:

using System;
using System.Net;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var wants = new WantsByte();
        wants.DoSomethingWithHasByte();
    }
}

class HasByte
{
    public byte[] theByteArray = new byte[4];

    public HasByte(IPAddress someAddress)
    {
        theByteArray = someAddress.GetAddressBytes();
    }
}

class WantsByte
{ 
    static IPAddress address = IPAddress.Parse("192.168.1.1");

    HasByte theInstance = new HasByte(WantsByte.address);

    public void DoSomethingWithHasByte()
    {
        Console.WriteLine(String.Join(",", theInstance.theByteArray.Select(o => o.ToString()).ToArray()));
    }
}

也给出相同的输出:

192,168,1,1

【讨论】:

  • 我错过了什么吗?他的代码在静态/非静态方面是完全正确的。他正在对局部变量进行操作。 IPAddress address = IPAddress.Parse("192.168.1.1"); HasByte theInstance = new HasByte(address); 不会抛出警告/编译器错误
  • 查看WantsByte类我可以看到addresstheInstance这两个变量都不是局部变量而是类字段。它们都是非静态的,编译器确实会发出错误 A field initializer cannot reference the non-static field, method, or property 'WantsByte.address' in line which initializes theInstance.
  • 你说得对——我脑子里放了个屁;我将WantsByte 作为一种方法阅读
  • 在实际代码中,这些部分位于 main() 中,但数组仍然全为 0
猜你喜欢
  • 2014-03-22
  • 2013-02-01
  • 2013-03-25
  • 1970-01-01
  • 2020-11-17
  • 2014-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多