从另一个 class 访问一个 public byte[] 数组

Accessing a public byte[] array from another class

在我正在编写的程序中,我有一个 class,其中有一个 public 字节数组,我想访问和使用它。

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。

谢谢。

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

    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
    }

在你的 class WantsByte 中你试图通过另一个 non-static 成员 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

或者,在 class WantsByte 中,您可以使 address 成为 static 成员,这将保证在首次使用 class。然后你可以在 theInstance initializer:

中引用它
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