Link to home
Start Free TrialLog in
Avatar of gvector1
gvector1

asked on

Converting VB.NET to C#

This should be easy but I am having a hard time.  I have some code that started out in VB6 that I opened with Studio 2003 and was able to compile and run.  Now I am trying to make the necessary conversions to C# and am having some problems.  Namely with the following routine:

      Function HiWord(ByRef dw As Integer) As Short
            '*************************************************
            ' returns the HIWORD (device) of parameter lParam
            '*************************************************
            If dw And &H80000000 Then
                  HiWord = (dw \ 65535) - 1
            Else : HiWord = dw \ 65535
            End If
            
      End Function

I tried a little VB.NET to C# converter I found on the web and it converted to this code:

            public short HiWord(ref int dw)
            {
                  short functionReturnValue = 0;
                  //*************************************************
                  // returns the HIWORD (device) of parameter lParam
                  //*************************************************
                  if (dw && -2147483648)
                  {
                        functionReturnValue = (dw / 65535) - 1;
                  }
                  else
                  {
                        functionReturnValue = dw / 65535;
                  }
                  return functionReturnValue;

            }

When I try to compile, I get errors stating "Operator && cannot be used on operands of type int and int" on this line:
   if (dw && -2147483648)

I also get a message stating "Cannot implicitly convert int to short" on the following 2 lines:
   functionReturnValue = (dw / 65535) - 1;
   functionReturnValue = dw / 65535;

I know on the convert int to short message I can cast it to a short or does the method need to return an int?  I am not very familiar with VB6 or VB.NET so understanding before the conversion is a little confusing in spots.  Sort of like how can an if statement contain (int & int) in VB?  What would be your suggestion on these conversions???
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

boolean AND should be the "&" operator, not "&&"

Since functionReturnValue is short, you need to cast explicitly to tell the compiler you KNOW you might be throwing away significant digits.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gvector1
gvector1

ASKER

When I use the boolean & I get the error "Cannot convert int to bool" with this line:
     if (dw & -2147483648)

When I try to cast to a short, I still get the same error,  here is the code I tried:

     functionReturnValue = (short)(dw / 65535) - 1;
Not that I recommend your method (either mine last suggestion or Bob's make more sense), to explain what is going wrong:

1) Unlike C, C# does not interpret int 0 as false and anything else as true, so
if ((dw & -2147483648) != 0)
would compile


2)  (short) cast binds to the (dw / 65535) rather than the whole expression by precedence, so
 functionReturnValue = (short)(dw / 65535 - 1);
would compile.


I think the short should be infront of whichever value you are stating is a short.  Eg:

(dw / (short)65535) -1;

Although I'm not sure what your if statement is trying to achieve.  Seems to just say if dw (dont know what that is) divided by 65535... but then nothing to say what you're looking for.  Unless its if 65535 can be divided into dw.
Ignore me :)
VB.NET has the << and >> operators.

Bob
I don't fully understand what is trying to be done with the hiword and loword methods, but how could I use the same code in the loword routine:

public short LoWord(int dw)
{
      short functionReturnValue = 0;
      //*************************************************
      // returns the LOWORD (command) of parameter lParam
      //*************************************************
      if (dw & 32768)
      {
            functionReturnValue = -32768 | (dw & 32767);
      }
      else
      {
            functionReturnValue = dw & 65535;
      }
      return functionReturnValue;
}
A 32 bit word ("int") can be considered to be composed of two 16 bit words ("short").

The idea of HiWord and LoWord is to return the two 16 bit entities in the 32 bit source.

The HiWord (as I showed above) is retrieved by SHIFTING the 16 high order bits to the right (losing the 16 low order bits).

The LoWord Bob showed you takes advantage of an int Point being composed of two shorts (one for the x coordinate and one for the y coordinate).  If you don't want to load the Drawing library to do this operation, all you have to do is "mask" out the high order bits.....


public short LoWord(int dw)
{
      return  (short) (dw & 0xffff);
}


What is (int & int) supposed to return?

What is the purpose of the following code:

If dw And &H8000 Then
      LoWord = &H8000s Or (dw And &H7FFF)

is that to check if it is a 16 bit or 32 bit value?
I'm sorry, I figured out that it does a bitwise and operator, but what I really need to know is how do you know when coding whether you need to use bitwise operators such as this.  I have never really gotten too in depth with bitwise operations.  It is sort of like I probably would have never figured out that saying

return (short)(dw & 0xffff);

would return the value I would need.  I understand more now than I did yesterday, but still hazy on some things.
(int1 & int2) returns an int where each bit is 1 ONLY if the corresponding bit was 1 in BOTH int1 and int2.

For example,
   0x101 & 0x100
returns 0x100
(since only the FIRST bit was set (i.e., 1) in both operands)

dw, being an int is ALWAYS a 32 bit value, even if all those bits are zero.
"If dw And &H8000 Then"
checks whether the highest of the first 16 bits is set
(which would make the corresponding short a NEGATIVE number)



I understand.  What I don't understand is how does someone determine that using these bit values and bitwise operators will accomplish what is needed.  Is there a rule of thumb that helps to determine whether using bit values and bitwise operators will accomplish certain tasks.
"What I don't understand is how does someone determine that using these bit values and bitwise operators will accomplish what is needed.  Is there a rule of thumb that helps to determine whether using bit values and bitwise operators will accomplish certain tasks."

Well, it sounds kinda stupid...but you only have two choices here:

(1) It is DOCUMENTED somewhere...
     (a) MSDN for example
     (b) In the code itself via comments
     (c) In some kind of physical documentation (i.e. a manual)

(2) It is NOT documented and you are relying on previous code as an example of what to do.

Or are you asking how does one know which combination of bitwise operators and values to use with each data type (short, int32, int64, etc)?
The way I parse your last question about a rule of thumb to determine which operators to use to perform various unspecified tasks seems to cover a good percentage of all programming!

Get to know the operators and functions available in your programming environment, and you will develop a feel for how to program.
Thanks to everyone who assisted with this question.  It was extremely helpful.  I feel I should split the points in this question as Bob and jensfiederer both answered my question and gave some additional assistance.

Thanks again,
Kendal