3

I'm trying to check if a struct from an array has been assigned, I can't check it or it's data for null. Is there a way I can check if it has been assigned yet?

Struct:

    [StructLayout(LayoutKind.Explicit)]
    public struct CharInfo
    {
        [FieldOffset(0)]
        public CharUnion Char;
        [FieldOffset(2)]
        public short Attributes;
    }

Method

    public void render(){
        for (int i = 0; i < (width * height - 1); i++) {
            if (screenBuffer[i].Char.UnicodeChar != Convert.ToChar(" ")) {
                ScreenDriver.screenBuffer[i] = screenBuffer[i];
            }
        }
       // ScreenDriver.screenBuffer = screenBuffer;
    }
Murilo
  • 1,112
  • 1
  • 18
  • 33
user2395615
  • 35
  • 1
  • 5

3 Answers3

5

You can compare the struct to its default value: if (struct==default(CharInfo). However this cannot differentiate between an uninitialized struct and a struct initialized with zeroes. This is because there are no such things as uninitialized structs, a struct is always automatically initialized.

If you can extend the struct, you can give it a bool IsAssigned. Default initialization will set this to false. Another option is to wrap it in a nullable:
CharInfo?[] screenBufferWithNull = new CharInfo?[123];

If extending the struct, or replacing it with a nullable<struct> is not desired, and you want to keep an aray of structs as in your example, the easiest workaround is to keep this information in a separate array of bools:
bool[] screenbufferIsAssigned = new bool[screenbuffer.Length];

HugoRune
  • 13,157
  • 7
  • 69
  • 144
  • Another option is to have an item in the enumeration be very specifically not initialized. For example enum Test { NotIntialized = 0, Red, Green, Blue }. the value 0 is the default. – btlog Sep 13 '15 at 01:05
1

Structs cannot be null as they are value types. Instead you can compare the to its default value using default(CharInfo) or create a Nullable.

Marcelo de Aguiar
  • 1,432
  • 12
  • 31
  • Thanks, but now I'm getting a new error. "Error 2 Operator '!=' cannot be applied to operands of type 'SharpTUI.Krn32.Kernal32.CharInfo' and 'SharpTUI.Krn32.Kernal32.CharInfo' " – user2395615 Sep 13 '15 at 00:41
  • Thats because you need to overload the operators for your struct as seen in this question http://stackoverflow.com/questions/15199026/comparing-two-structs-using – Marcelo de Aguiar Sep 13 '15 at 00:44
0

In the following code, we create and implement an implicit boolean operator that handles one approach to determining if a struct has been initialized (a struct is never null, as they are value types. However their members can be null).

struct Foo
{
    float x;
    public static implicit operator bool(Foo x)
    {
        return !x.Equals(default(Foo));
    }
}

Note: this will return a false negative when x == 0. Keeping this in mind, you can creatively code a workaround depending on your specific needs.

The simplest work around would be to create a dummy class (eg nullable) which you initialize in the constructor of the struct, and ensure to always initialize said struct through the use of a constructor:

public class nullable{}
struct Foo
{
    nullable n;
    float x;
    public Foo(float x)
    {
        this.x = x;
        this.n = new nullable();
    }
    public static implicit operator bool(Foo x)
    {
        return !x.Equals(default(Foo));
    }
}

Usage:

if(mFoo)
{
    //Do something
}
Andy
  • 23
  • 7