WIA Shared Samples - Translating VB to C#

105 Views Asked by At

This Sample is supposed to return the number of Root-Level Items for Transfer of any given Device (here).

Dim d 'As Device
Dim itm 'As Item
Dim i 'As Integer

i = 0
Set d = CommonDialog1.ShowSelectDevice
For Each itm In d.Items
    Dim f
    f = itm.Properties("Item Flags")
    If (f And ImageItemFlag) = ImageItemFlag Then
       i = i + 1
    End If
Next

MsgBox "Selected device has " & i & " top-level images"

What troubles me is the following line:

If (f And ImageItemFlag) = ImageItemFlag Then

My Translation to C# is:

using static WIA.WiaItemFlag; // enum


Device device;
CommonDialog dialog = new CommonDialog();
int i = 0;

device = dialog.ShowSelectDevice();

foreach (Item item in device.Items)
{
    var property = item.Properties["Item Flags"];
    // &-Operator can't be applied to Types 'Property' and WiaItemFlag
    if ((property & ImageItemFlag).Equals(ImageItemFlag)) 
        i++;
}


//return i;

The Documentation right here tells us that we can use the &-Operator indeed, but it doesn't work in practice.

How do you translate the If-Condition? I tried several times but the error is that one cannot use the '&'-Operator on Property and WiaItemFlag.

Help would be much appreciated.

1

There are 1 best solutions below

9
On

Depending on the types of property and ImageItemFlag you may need to cast them as int.

using static WIA.WiaItemFlag; // enum


Device device;
CommonDialog dialog = new CommonDialog();
int i = 0;

device = dialog.ShowSelectDevice();

foreach (Item item in device.Items)
{
    var property = item.Properties["Item Flags"];
    // &-Operator can't be applied to Types 'Property' and WiaItemFlag
    if (((int)property & (int)ImageItemFlag).Equals(ImageItemFlag)) 
        i++;
}