[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Converting VB.NET to C#

Posted on 2007-08-02
17
Medium Priority
?
1,188 Views
Last Modified: 2013-12-17
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???
0
Comment
Question by:gvector1
  • 6
  • 6
  • 2
  • +2
17 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 19617356
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 19617381
Try this:

using System.Drawing;

    public short LoWord(int value)
    {
        return (short)new Point(value).X;
    }

    public short HiWord(int value)
    {
        return (short)new Point(value).Y;
    }

Bob
0
 
LVL 23

Assisted Solution

by:Jens Fiederer
Jens Fiederer earned 1000 total points
ID: 19617393
Of course, since C has more boolean operators than VB does, a more sensible implementation would be:

            public short HiWord(ref int dw)
            {

                 return (short) (dw >> 16);  
            }
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:gvector1
ID: 19617405
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;
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 19617450
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.


0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 19617484
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.
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 19617495
Ignore me :)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19617689
VB.NET has the << and >> operators.

Bob
0
 

Author Comment

by:gvector1
ID: 19619302
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;
}
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 19620026
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);
}


0
 

Author Comment

by:gvector1
ID: 19620260
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?
0
 

Author Comment

by:gvector1
ID: 19620311
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.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 19620667
(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)



0
 

Author Comment

by:gvector1
ID: 19620847
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.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19621973
"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)?
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 19624162
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.
0
 

Author Comment

by:gvector1
ID: 19638929
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
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question