C# 错误无法将类型byte[]隐式转换为byte
我有一个名为C# 错误无法将类型byte[]隐式转换为byte,c#,C#,我有一个名为animal的类,它有公共字符串名称,物种,和公共动物 在另一种形式中,我创建了名为Simba的动物,我想设置Simba的值,但当我想设置photoAnimal时,我得到了错误。我使用filestream和binarydreader读取数据,然后从filestream和二进制读取器创建byte[]imageData=binary数据。我无法设置Simba.photoAnimal=imageData,以下是我的一些代码: animal Simba = new animal();
animal
的类,它有公共字符串名称
,物种
,和公共动物
在另一种形式中,我创建了名为Simba的动物,我想设置Simba的值,但当我想设置photoAnimal
时,我得到了错误。我使用filestream
和binarydreader
读取数据,然后从filestream
和二进制读取器创建byte[]imageData=binary数据。我无法设置Simba.photoAnimal=imageData,以下是我的一些代码:
animal Simba = new animal();
string fileName = textBox5.Text;
byte[] ImageData;
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
br = new BinaryReader(fs);
ImageData = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
Simba.name = textBox1.Text;
Simba.species = textBox2.Text;
Simba.photoAnimal = ImageData; // error
ImageData是一个字节[]。因此,在动物类中,替换
public Byte photoAnimal
借
错误消息表示它无法将您的
图像数据
(类型为byte[]
)分配给photoAnimal
,该类型似乎为byte
在您的类animal
中,将photoAnimal
的类型更改为数组:
public class animal
{
public byte[] photoAnimal;
}
作为旁注,您颠倒了命名约定。变量应为camelCase,类应为大写camelCase。在C语言中,你通常使用
animal Simba=new animal(),
而不是animal Simba=new animal()
编译器错误会告诉你哪里出错了。当您将byte[]
分配给byte
变量时,您预计会发生什么情况?+1,因为它不仅起作用,还提到了命名约定,可以避免其他C#dev的痛苦。
public class animal
{
public byte[] photoAnimal;
}