C# 从另一个类访问公共字节[]数组
在我编写的程序中,我有一个类,其中有一个公共字节数组,我想访问和使用它C# 从另一个类访问公共字节[]数组,c#,class,oop,scope,public,C#,Class,Oop,Scope,Public,在我编写的程序中,我有一个类,其中有一个公共字节数组,我想访问和使用它 class HasByte { public byte[] theByteArray = new byte[4]; public HasByte(IPAddress someAddress) { theByteArray = someAddress.GetAddressBytes(); } } class WantsByte { IPAddress address = IPAddress.P
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
}
目前,我通过Instance.theByteArray访问的字节数组都是0,原因我想知道
谢谢。除了我在评论中提到的封装之外,下面是应该适用于您的代码,请注意,您不能在声明该实例时初始化该实例,因此您将其移动到构造函数:
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
{
IPAddress address = IPAddress.Parse("192.168.1.1");
HasByte theInstance;
public WantsByte()
{
theInstance = new HasByte(this.address);
}
}
在类
WantsByte
中,您试图通过另一个非静态成员地址来初始化成员实例
,编译器必须对此进行投诉。您可以将INSTANCE
初始化移动到构造函数:
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
{
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
中,您可以将地址
设置为静态
成员,这将保证在首次使用类之前对其进行初始化。然后,您可以在instance
初始值设定项中引用它:
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
将类成员声明为公共的做法不好,最好根据需要使用get/set将其封装为属性。至于你的问题,构造器是否按它应该的方式工作?你试过调试它吗?@FelixAv我还没有学习get/set,但我会研究一下。构造函数可以工作,在实际代码中,我可以访问更多的公共变量,它们保留它们的值。这只是数组的问题。你确定你发布了正确的代码;您的代码将无法编译,因为
实例
是用非静态地址
初始化的。也许WantsByte是一个方法而不是一个类?我试着运行我发布的代码,它工作得很好,所以你不应该对它有任何问题,如果你这样做,请让我们知道你的代码工作,它似乎是错误的东西。我会继续找的。谢谢,顺便说一句。我是不是遗漏了什么?他的代码在静态/非静态方面完全正确。他在操作局部变量IPAddress address=IPAddress.Parse(“192.168.1.1”);HasByte theInstance=新的HasByte(地址)代码>不会抛出警告/编译器错误查看类WantsByte
我可以看到变量地址
和实例
都不是局部变量,而是类字段。它们都是非静态的,编译器确实会发出错误。字段初始值设定项不能在初始化实例的行中引用非静态字段、方法或属性“WantsByte.address”。你完全正确-我有个大脑屁;我把WantsByte
理解为一种方法,在实际代码中,这些部分位于main()中,但数组仍然都是0