# How to convert this VB code to C#? [Answered]RSS

## 3 replies

Last post Oct 07, 2019 05:47 AM by Yongqing Yu

• Peter Cong

Member

342 Points

2136 Posts

### How to convert this VB code to C#?

Oct 04, 2019 06:17 PM|Peter Cong|LINK

I have the following VB codes:

Dim u as Integer

Dim a as Integer = &H2  //need help here

.........

u = some integer number

Dim D as Boolean = True

D = CBool(a And u)   //need help here

If Not D then

return true

else

return false

end if

,,

can anybody help me to convert these codes to C#?

Thanks

• mgebhard

All-Star

43721 Points

18706 Posts

### Re: How to convert this VB code to C#?

Oct 04, 2019 07:06 PM|mgebhard|LINK

Looks like homework...

`Dim a as Integer = &H2  //need help here`

You could simply run the code through the Visual Studio debugger.  Anyway, the &H means Hex and 2 in Hex is same as 2 decimal.

`int a = 2;`

Or in Hex notation

`int a = 0x2;`

This is binary logic.

`D = CBool(a And u)   //need help here`

The idea is to determine if the variable u has bit 2 set.

The C# code is below.

```int a = 0x02;
int u = 0x0A;

bool result = (a & u) > 0;
Console.WriteLine(result);```

The result is true because bit two is set in u.

• Peter Cong

Member

342 Points

2136 Posts

### Re: How to convert this VB code to C#?

Oct 04, 2019 10:17 PM|Peter Cong|LINK

#### mgebhard

Looks like homework...

`Dim a as Integer = &H2  //need help here`

You could simply run the code through the Visual Studio debugger.  Anyway, the &H means Hex and 2 in Hex is same as 2 decimal.

`int a = 2;`

Or in Hex notation

`int a = 0x2;`

This is binary logic.

`D = CBool(a And u)   //need help here`

The idea is to determine if the variable u has bit 2 set.

The C# code is below.

```int a = 0x02;
int u = 0x0A;

bool result = (a & u) > 0;
Console.WriteLine(result);```

The result is true because bit two is set in u.

Thanks a lot for your quick help, I will try it next Monday and let you then,

Have a great weekend,

• Yongqing Yu

Contributor

2760 Points

784 Posts

### Re: How to convert this VB code to C#?

Oct 07, 2019 05:47 AM|Yongqing Yu|LINK

Hi Peter,

According to your VB code, you can convert these code to c# as follows:

```int u;
int a = 0x2;
//...
u = 12;//some integer number
bool D = true;
D = Convert.ToBoolean(a & u);
if(!D)
{
return true;
}
else
{
return false;
}```

Best Regards,

YongQing.

MSDN Community Support

Please remember to click "Mark as Answer" the responses that resolved your issue.

If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.