locked
How to convert this VB code to C#? RRS feed

  • Question

  • User-1651604128 posted

    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

    Friday, October 4, 2019 6:17 PM

Answers

  • User-1651604128 posted

    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,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 10:17 PM
  • User665608656 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2019 5:47 AM

All replies

  • User475983607 posted

    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.

    Friday, October 4, 2019 7:06 PM
  • User-1651604128 posted

    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,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 10:17 PM
  • User665608656 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2019 5:47 AM