Skip to main content
gamedev.net
🔒 Locked

[.net] enum problem

Started by microb Jun 27, 2007 at 2:38 PM 5 replies 1.3k views
Original Post
microb
microb
Hello, I'm new to C# and I'm encoutering a problem using enums. ------------- START OF CODE ---------------------------------------------- (File: E3DCore.cs) namespace E3D { public enum E3D_INIT_FLAGS : uint { E3D_INIT_FULLSCREEN = 0x00000010 }; public enum E3D_ERROR_TYPES : int { E3D_NO_ERROR = 0, E3D_FAILURE = -1; }; } // EOF ------------- END OF CODE ---------------------------------------------- If if try something like: ------------- START OF CODE ---------------------------------------------- if (flags & E3D_INIT_FLAGS.E3D_FULLSCREEN) pp.Windowed = false; ------------- END OF CODE ---------------------------------------------- I obtain: Error 1 Operator '&' cannot be applied to operands of type 'uint' and 'E3D.E3D_INIT_FLAGS' C:\Documents and Settings\microb.AZATHOTH\My Documents\Visual Studio 2005\Projects\E3D\E3D\Renderer\E3DRenderer.cs 72 17 E3D Can someone explain why? Thanks in advance, microb
mpipe
mpipe
Isn't the & operator used for bits?
microb
microb
In C++ (where I come from), I'm doing

if (flags & SGE_INIT_HIGHPRIORITY)
...

between to unsigned integers.

If you look at my enum, it has the 'uint' type.

Why E3D_INIT_FLAGS.E3D_INIT_FULLSCREEN isn't viewed as
an uint?

Thanks for your help,
microb
jpetrie
jpetrie
flags is an int. E3D_INIT_FLAGS.E3D_FULLSCREEN is a E3D_INIT_FLAGS. operator& cannot be applied to those types. You must cast one. C# is more strict concerning naughtiness between types than C++.

After you fix that error, you'll note that C# disallows non-boolean expressions in if() statements, and (x & y) is non-boolean. You'll need to do:
if(((E3D_INIT_FLAGS)flags & E3D_INIT_FLAGS.E3D_FULLSCREEN) != 0)  ...
microb
microb
Thank you for your answer, jpetrie. I've modified my code and it works.

Another question:

What is the exact meaning of 'uint' in:

public enum E3D_INIT_FLAGS : uint
{
}

Thanks,
microb
Nypyren
Nypyren
You might also want to put the [Flags] attribute on your enum, which lets a few things know to treat the enum as flags (I think mainly this is useful for converting to and from a string).
savagemonitor
savagemonitor
Quote:
Original post by microb
Thank you for your answer, jpetrie. I've modified my code and it works.

Another question:

What is the exact meaning of 'uint' in:

public enum E3D_INIT_FLAGS : uint
{
}

Thanks,
microb


By default in C# enums are of type int. What your statement is doing is changing that enum's type to uint instead of the standard int.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.