Bit Flags/Enums?

I want to write a method where I pass in a bunch of bit flags to decide what the method does, kind of like an OpenFile function where you would write something in C/C+C# like:

OpenFile(filename, READ | WRITE)

In the code for the method I would test to see if the flags value has the various bits set.

In my particular example I want to conditionally build up a block of JSON from properties in the class depending on the presence of the various flags. I looked at Enums but I can’t use them as if is not possible to set their value to align with powers of two. I looked at Consts but they are doubles, Is there a neat way so that I get some good looking code to achieve this in Xojo? I am wondering if I should be using or’d flags or may an array?

They aren’t, actually. The IDE refers to them as Doubles, but in reality the value of the constant is used verbatim. I use Integer constants all the time, and they work just fine when Or’ing them into a bitmask.

[quote=22834:@Carl Clarke]
In my particular example I want to conditionally build up a block of JSON from properties in the class depending on the presence of the various flags. I looked at Enums but I can’t use them as if is not possible to set their value to align with powers of two[/quote]

Why not ?
You can set the value each item in an enum represents
When you define it just put = in each line

[quote=22844:@Norman Palardy]Why not ?
You can set the value each item in an enum represents
When you define it just put = in each line[/quote]

Brilliant! - sorry it was just not obvious to me that you type the name and the assignment in the declaration cell. I went Googling and got even more confused with various pieces of misinformation. I was left with the impression that Enum values were just sequential magic values (like 1, 2, 3, 4, 5…) managed by Xojo.

Now I have coded one method that takes a configuration object and a bunch of flags to say which configuration properties are to be set

api.SetConfiguration(cfg, Data.Configuration.PropertyFlag.Name or Data.Configuration.PropertyFlag.ProxyPort)

and have an extension method that takes away the ugliness of the casts behind the scenes

Function IsFlagSet(Extends val as Data.Configuration.PropertyFlag, flag as Data.Configuration.PropertyFlag) As Boolean
  dim b as boolean = false // yeah I know but old habits die hard and a some safety critical work with radiation...
  
  if (Uint32(val) and Uint32(flag)) <> 0 then
    b = true
  end
  
  return b
End Function

and have a method that creates the JSONItem (called by the SetConfiguration method above)

Function ToJSONItem(flags as Data.Configuration.PropertyFlag) As JSONItem
  dim item as new JSONItem
  
  if flags.IsFlagSet(PropertyFlag.ProxyPort) then
    item.Value("proxyport") = self.ProxyPort.ToString()
  end
  
  if flags.IsFlagSet(PropertyFlag.Name) then
    item.Value("name") = self.Name
  end
  
  return item
End Function

Neat!! Thanks Norman (and Andrew for clarifying Consts )