0

I can't seem to figure out how to do this. I have a small section of code in c++ and I need to have the same in C#. Here is the c++ code:

struct Texel { unsigned char r, g, b, a; }; 
Texel mytexturedata[] = 
{
   {0x00, 0xFF, 0x00, 0xFF}, // green
   {0xFF, 0xFF, 0x00, 0xFF}, // yellow
   {0xFF, 0x00, 0x00, 0xFF}, // red
};

How do you do this is c#?

Thanks!

2
  • Texel[] mytexturedata = { new Texel() { r = 0x00, g=0xFF, b=0x00,a=0xFF} , .. }; with the change of class Texel { byte r {get;set;} byte g {get;set;} byte b {get;set;} byte a {get;set;} }. BTW, unsigned char corresponds to byte in C#, char is 16-bit wide (UTF-16). Commented Jan 13, 2017 at 16:51
  • I think that byte is a better type than unsigned char in c# . . . Commented Jan 13, 2017 at 16:51

1 Answer 1

3

Convert your struct to a class.

public class Texel
{
    public byte r { get; set; }
    public byte g { get; set; }
    public byte b { get; set; }
    public byte a { get; set; }
}

Then you can do your assignment like this.

Texel[] mytexturedata =
{
    new Texel() { r = 0x00, g = 0xFF, b = 0x00, a = 0xFF }, // Green
    new Texel() { r = 0xFF, g = 0xFF, b = 0x00, a = 0xFF }, // Yellow
    new Texel() { r = 0xFF, g = 0x00, b = 0x00, a = 0xFF }  // Red
};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.