Is it possible to divide a UInt64 by another UInt64?

  • Thread starter Thread starter ThunderMusic
  • Start date Start date
T

ThunderMusic

Hi,
I have a problem, I have to do this (MyUInt64 / MyOtherUInt64). the IDE
tells me that the / operator has not been overloaded for UInt64. Is there a
way I can do what I need to do?

Thanks
 
Hi,
I have a problem, I have to do this (MyUInt64 / MyOtherUInt64). the IDE
tells me that the / operator has not been overloaded for UInt64. Is there a
way I can do what I need to do?

I assume you're working in VB.NET? C# can handle dividing the UInt64
numbers. However, UInt64 is not CLS compliant. VB.NET makes it much
more difficult to write non-CLS compliant code.
 
Patrick Steele said:
I assume you're working in VB.NET? C# can handle dividing the UInt64
numbers. However, UInt64 is not CLS compliant. VB.NET makes it much
more difficult to write non-CLS compliant code.

Yes I work in VB.NET. I found that by writing (MyUInt64.ToString /
MyOtherUInt64.ToString), the division is done correctly... But I'm pretty
sure there is a better solution.

Is there a reason why C# can do it and VB.NET cannot? I mean, it's supposed
to be the same framework, it's supposed to compile to the exact same thing,
right? Why can one do what the other cannot?

thanks
 
ThunderMusic said:
Hi,
I have a problem, I have to do this (MyUInt64 / MyOtherUInt64). the IDE
tells me that the / operator has not been overloaded for UInt64. Is there a
way I can do what I need to do?

Thanks

In VB.Net I assume? Unsigned Integers are not yet supported in VB.Net.
C# will allow you to work with unsigned Ints.

Options:
If you don't really need the values to be unsigned and signed Int64 will
work, use that instead. This is the preferred method.

If you still require the larger values that extra bit gives you, but you
don't have to use UInt64, then convert the values to Decimal format. Less
efficient than Int64, but a good alternative.

If you really need to keep them as UInt64's for whatever reason, and you
only have a few operations you need to do, then converting them temporarily
to Decimal and back is an option. For example:

Private Function UInt64Divide(ByVal Value1 As UInt64, ByVal Value2 As
UInt64) As UInt64
Dim a, b, c As Decimal

a = Convert.ToDecimal(Value1)
b = Convert.ToDecimal(Value2)
c = a \ b 'Note \ not / means Integer Division
Return Convert.ToUInt64(c)

End Function

If you really need them as UInt64's and have any reasonable amount of work
you need to do with them, then write a class in C# to handle all the things
you really need to do with them. Although take note that they are not
CLS-compliant. While you "can" work with them, it is not recommended,
especially if you expose any of them outside your class.

Gerald
 
Is there a reason why C# can do it and VB.NET cannot? I mean, it's supposed
to be the same framework, it's supposed to compile to the exact same thing,
right? Why can one do what the other cannot?

The goal of VB.NET was to be a simpler way to program in .NET. To that
end, the designers decided not to add support for non-CLS data types in
the language. However, I believe the next version of VB.NET will
support most (if not all) of the non-CLS data types (like unsigned
integers).
 
ThunderMusic said:
Yes I work in VB.NET. I found that by writing (MyUInt64.ToString /
MyOtherUInt64.ToString), the division is done correctly... But I'm pretty
sure there is a better solution.

I believe that the values are being cast to the first value that will
satisfy the / operator.
So if the String value is "123456" then those strings are probably being
cast to Integers.
If they are really greater than a standard Int64 value, by 1 Bit, then they
will be cast to Decimals.
Is there a reason why C# can do it and VB.NET cannot? I mean, it's supposed
to be the same framework, it's supposed to compile to the exact same thing,
right? Why can one do what the other cannot?

Communication and planning issues if I recall. Unsigned Integers are not CLS
compliant. Originally neither language was going to support it. Then they
were. Then they weren't. etc.
At one point, VB.Net had them in there I believe. Along with some BitWise
operators I liked, but that's another story.
The decision was made to pull them from the languages to keep them CLS
compliant.
They got pulled from VB, but never did from C#.
In the end, sometimes you just need that one extra bit. After much
discussion I'm sure, I believe they have decided to re-introduce them in VB
2005.

Gerald
 
Back
Top