VB.Net operators and converters not the same as c#.. WHY???

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,016
Location
Cambridge, UK
Programming Experience
5-10
Ok, so i've been playing around with some of the more obscure functions in the win32 api, and i have to say that I am startingto lose heart with vb.net. Bearing in mind that both languages are supposed to be equivalent but the 'same' routine can produce such drasticaly different results when comapred to c# and becuase i have run the same function s from both managed c++ and unmanaged c++ unfortunately the c# version of the code returns the correct value!!!! Here it is. I would appreciate anyone telling me why on earth these two functions should return different values. It's late, maybe i've missed something obvious if i have then I will be a happy man. Try it if you dare....

c# version

VB.NET:
public static int MulDiv(int number, int numerator, int denominator)
        {
            float num = (number * numerator) / denominator;
            return (int)Math.Round((double)num);
        }

vb.net version


VB.NET:
Public Shared Function MulDiv(ByVal number As Integer, ByVal numerator As Integer, ByVal denominator As Integer) As Integer
        Dim num As Single = ((number * numerator) / denominator)
        Return CInt(Math.Round(CDbl(num)))
    End Function

On my machine they return two different values for a win32 color, doesn't matter what the latter two values are. How could this be????

Another example trying to mimic the RGB macro from c++....

VB.NET:
public static int RGB(int r, int g, int b)
        {
            return ((((byte)r) | (((byte)g) << 8)) | (((byte)b) << 0x10));
        }


c# version performs perfectly, whereas the vb.net version....


VB.NET:
 Public Shared Function RGB(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As Integer
        Return (((CByte(r)) Or ((CByte(g)) << 8)) Or ((CByte(b)) << &H10))
    End Function


Doesn't!!! Same Signature, same conversion... different result. If anyone could explain this to me i'd be grateful. I don't think i've missed anything. I just dont see why it shouldn't work.

The only thing I can think of is that c# allows a bitwise Or as well as a logical Or
 
Somethings wrong with your conversion, correct translation of the RGB function in post 2 here.

For the other you can debug it, break down each operation and conversion with exact values to see where there is a difference and possibly why. For example is there a difference between VB.Net and C# Math.Round(Double) function?
 
Should the Math.Round function not be the same in both C# and VB.Net seeing as it's a framework function? Interesting thing is that the MulDiv function in Kernal32.dll also returns a different value. It's fairly arithmatic I just can't see why the return values would differ. I have played around with it and it just won't do it. Very frustrating..
 
Yes, I think they should be the same, but I haven't downloaded C# Express to actually verify it. I also think * and / should be the same. So it seems it boils down to CDbl(SingleNum) compared to (double)SingleNum. Can you verify that this is where it differs, and only here?
 
For one thing, Int() is NOT the same as CInt().

The Int() function truncates the decimal number no matter what the value is after the decimal point. So for example, Int(45.89) returns 45.

On the other hand, CInt() uses banker's rounding to round out the number to the nearest integer, so CInt(45.89) will return 46.

Note: Banker's rounding rounds up to the nearest EVEN number if the pivot digit is a 5, so 4.5 and 3.5 will both round out to 4.
 
Back
Top