Using Flags In My Function as Parameter (OR usage?)

  • Thread starter Thread starter Alex Stevens
  • Start date Start date
A

Alex Stevens

Hi All,

I want to do something like the flags property in the old commondialog.

Where you used the OR to specifiy the options, I'd like to build the same
into my function call.
How do I do this.

For example:

Dim lng As Long
lng = 1 Or 2

How do I establish that lng contains 1 and 2?

What is this technique described as in vb.net, and where would I be able to
read up on it?

Thanks

Alex
 
Alex,
How do I establish that lng contains 1 and 2?

Use the And operator:

If CBool(lng And 1) Then

or

If (lng And 2) <> 0 Then


You can make the code more readable by using an Enum rather than
hardcoded values.



Mattias
 
Cracking......

I was using an enum, but as I wasn't sure about the terminology i was using
I just used integers to describe......

Is there anywhere I can read up on this????

For example, how would I enumerate the options / choices whatever they are.

So If I was executing a procedure using the enum's long value, how would
interate around the selections and use the value in my procedure?

Apologies for my stupidity......!

Alex
 
* "Alex Stevens said:
For example, how would I enumerate the options / choices whatever they are.

If you want to define an enumeration for flags, the different bits in
the constants must be set, for example (simplified):

Const1 = 1 (0000001)
Const2 = 2 (0000010)
Const3 = 4 (0000100)
Const4 = 8 (0001000)
Const5 = 16 (0010000)
.... ...

In VB.NET 2003, you can use bit shifting operators to do that:

\\\
<Flags()> _
Enum ErrCode
OkeyDokey = 1 << 0
BadThis = 1 << 1
BadThat = 1 << 2
BadWhat = 1 << 3
BadDude = 1 << 4
BadBoy = 1 << 5
BadToTheBone = 1 << 6
End Enum
///
So If I was executing a procedure using the enum's long value, how would
interate around the selections and use the value in my procedure?

I remember Mattias showed you how to determine if a bit is set.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
Am I able to use the FlagsAttribute when my enum is numbered as such????

<FlagsAttribute()> Public Enum CustomCCItemType As Long

Drawing = 1
PNumber = 2
ConsumablePNumber = 3
RepairSalvageDrawing = 4
ProcurementDS = 5
LOP = 6
WeightReport = 7
Modification = 8
Amendment = 9
MagneticTape = 10
DesignSpecification = 11
ToolDrawing = 12
TestRig = 13
TestRigDrawing = 14
StandardPart = 15

End Enum

The reason they are numbered is that they correspond to a reference key in a
table which I want to build a search SQL string for.
What I am reading is that a flags enum has to be numbered 1,2,4,8,16
etc......
In that case, how can I use this enum as a flag?

Alex
 
* "Alex Stevens said:
Am I able to use the FlagsAttribute when my enum is numbered as such????

<FlagsAttribute()> Public Enum CustomCCItemType As Long

Drawing = 1
PNumber = 2
ConsumablePNumber = 3
RepairSalvageDrawing = 4
ProcurementDS = 5
LOP = 6
WeightReport = 7
Modification = 8
Amendment = 9
MagneticTape = 10
DesignSpecification = 11
ToolDrawing = 12
TestRig = 13
TestRigDrawing = 14
StandardPart = 15

End Enum

Yes, you are able to do that but it doesn't make sense because the
numbers cannot be used as flags (there is more than 1 bit set in some
numbers, for example 3).
The reason they are numbered is that they correspond to a reference key in a
table which I want to build a search SQL string for.
What I am reading is that a flags enum has to be numbered 1,2,4,8,16
etc......
In that case, how can I use this enum as a flag?

In this case, you cannot use flags.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
Alex,
The reason they are numbered is that they correspond to a reference key in a
table which I want to build a search SQL string for.
I would have the Enum have values such as 1, 2, 4, 8, 16, as that is
required.

Then I would consider having a Hashtable that maps the above values to 1, 2,
3, 4, 5, 6 to do the lookup in SQL. (see below)

Remember the point of using Flags & Or operator is to support passing in two
CustomCCItemType values at the same time, in a signal value:

CustomCCItemType.Drawing Or CustomCCItemType.PNumber

However you will need to extract each CustomCCItemType individually to build
your SQL statement. (see below)

The problem as I see it is the way the Enum implements a "set of values" by
combining the bit values into a single integer, where as SQL implements a
"set of values" by combining individual values into a collection of multiple
integers. Hence your delima.

One way to map the Enum to SQL key:

I would have a hash table that is keyed by the CustomCCItemType
values themselves, the value would be the SQL Key.

' Note it is best to do this once, as a Shared variable of your class.
Dim mapping As New Hashtable()
Dim index As Integer
For Each value As CustomCCItemType In
[Enum].GetValues(GetType(CustomCCItemType))
index += 1
mapping.Add(value, index)
Next

If the SQL keys are not consecutive then the above will not work, I would
use individual mapping.Add statements for each Enum to Key pair, instead of
the loop.

mapping.Add(CustomCCItemType.Drawing, 1)
mapping.Add(CustomCCItemType.PNumber, 2)
mapping.Add(CustomCCItemType.ConsumablePNumber, 3)
...

The problem would seem to be 'extracting' each unique attribute out of the
CustomCCItemType parameter, I would use my hash table to map bit masks (the
keys) that I use to check the parameter.

Dim keys As New ArrayList()
For Each de As DictionaryEntry In mapping
Dim key As CustomCCItemType = CType(de.Key, CustomCCItemType)
If (input And key) = key Then
keys.Add(de.Value)
End If
Next

The keys ArrayList now contains a list of SQL keys to use for your SQL
statement. Rather then using the keys ArrayList you could use a
StringBuilder and directly build the SQL statement.

Hope this helps
Jay


Hope this helps
Jay
 
Back
Top