错误无法将类型byte []隐式转换为byte

时间:2013-12-18 15:35:49

标签: c#

我有一个名为animal的班级,它有public string namespeciespublic Byte photoAnimal

在另一种形式中,我创建了名为Simba的动物,我想设置Simba的值,但是当我想设置photoAnimal时,我得到了错误。我使用filestreambinaryreader来读取数据,然后使用来自create byte[] imageData = binary和二进制阅读器的filestream数据。我无法设置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    

2 个答案:

答案 0 :(得分:7)

ImageData是一个byte []。所以在动物类中,替换

public Byte photoAnimal 

通过

public Byte[] photoAnimal.

答案 1 :(得分:1)

错误消息表示无法将您的ImageData(类型为byte[])分配给photoAnimalbyte类似animal

在您的班级photoAnimal中,将public class animal { public byte[] photoAnimal; } 的类型更改为数组:

animal Simba = new animal(),

作为旁注,你颠倒了命名惯例。变量应该在CamelCase和UpperCamelCase中的类中。您通常使用Animal simba = new Animal()

而不是C#中的{{1}}
相关问题