how to compare a Long with a UInt64 (or convert fromLong to UInt64)?

W

Wolfgang Kaml

Dear all,

I am using the following code to retrieve the size of a certain file and the
available (free) space on the disk. The problem is, that I get the size of
the file returned as a Long and the size of free disk space as UInt64.
Apparently, there are no function to convert a Long to a UInt64 or to
compare the two (I'm not expecting to convert the UInt64 to a Long
necessarily, but it should work the other way round, right?).

Short story: How to compare a Long with a UInt64?

Long story, please read the code below to see the problem described in the
very last comment statement
'------ snip - snip -------
Dim objDiskClass As System.Management.ManagementClass
Dim objDisks As System.Management.ManagementObjectCollection
Dim objDisk As System.Management.ManagementObject
Dim lFileSize As Long
Dim uiDiskSpace As System.UInt64

Dim objFileInfo = New FileInfo("MyFile.txt")
lFileSize = objFileInfo.Length

objDiskClass = New System.Management.ManagementClass("Win32_LogicalDisk")
objDisks = objDiskClass.GetInstances()

For Each objDisk In objDisks
If CStr(objDisk("Name")) = "D:" Then
uiDiskSpace = CType(objDisk("FreeSpace"), System.UInt64)
Exit For
End If
Next objDisk

'now I got the File Size as Long in lFileSize
'and the Free Disk Space as UInt64 in uiDiskSpace
'??? How can I compare the two ???
'------ snip - snip -------


Thank you for all of your help and have some great holidays too!
Wolfgang
 
O

One Handed Man [ OHM# ]

Untested . .

Dim ui As UInt64 = Convert.ToUInt64(500)

Dim int64 = Convert.ToInt64(ui)

Regards - OHM





Wolfgang said:
Dear all,

I am using the following code to retrieve the size of a certain file
and the available (free) space on the disk. The problem is, that I
get the size of the file returned as a Long and the size of free disk
space as UInt64. Apparently, there are no function to convert a Long
to a UInt64 or to compare the two (I'm not expecting to convert the
UInt64 to a Long necessarily, but it should work the other way round,
right?).

Short story: How to compare a Long with a UInt64?

Long story, please read the code below to see the problem described
in the very last comment statement
'------ snip - snip -------
Dim objDiskClass As System.Management.ManagementClass
Dim objDisks As System.Management.ManagementObjectCollection
Dim objDisk As System.Management.ManagementObject
Dim lFileSize As Long
Dim uiDiskSpace As System.UInt64

Dim objFileInfo = New FileInfo("MyFile.txt")
lFileSize = objFileInfo.Length

objDiskClass = New
System.Management.ManagementClass("Win32_LogicalDisk") objDisks =
objDiskClass.GetInstances()

For Each objDisk In objDisks
If CStr(objDisk("Name")) = "D:" Then
uiDiskSpace = CType(objDisk("FreeSpace"), System.UInt64)
Exit For
End If
Next objDisk

'now I got the File Size as Long in lFileSize
'and the Free Disk Space as UInt64 in uiDiskSpace
'??? How can I compare the two ???
'------ snip - snip -------


Thank you for all of your help and have some great holidays too!
Wolfgang

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
S

Stephen Martin

Unless you're expecting to query a disk with more than 9 billion gigabytes
of free space just declare uiDiskSpace as Long.
 
W

Wolfgang Kaml

OK, so now you have to tell me if you tested that or actually have done
that, because I'm getting an Exception when changing the line from
And assigning the UInt64 to a Long won't work either, because at that point
I get a
"Value of type 'System.UInt64' cannot be converted to 'Long'.

PLEASE show me of how you want to accomplish your proposed solution.

Thanks,
Wolfgang
 
W

Wolfgang Kaml

Thanks so much for your help!!!

I ended up converting the UInt64 to a Long, which is not without risk, but
talking about disk space, I should be OK for another dozen years.
lDiskSpace = Convert.ToDecimal(uiDiskSpace)

By converting the Long to a UInt64 I ran into the next problem, that the
operand '<' is not defined for UInt64.

I'm not sure if there would have been a way around that.

I'm just not sure, of why they used UInt64 for Diskspace. A Long should have
worked for a while longer, no?

Wolfgang
 
H

Herfried K. Wagner [MVP]

* "Wolfgang Kaml said:
I am using the following code to retrieve the size of a certain file and the
available (free) space on the disk. The problem is, that I get the size of
the file returned as a Long and the size of free disk space as UInt64.
Apparently, there are no function to convert a Long to a UInt64 or to
compare the two (I'm not expecting to convert the UInt64 to a Long
necessarily, but it should work the other way round, right?).

Short story: How to compare a Long with a UInt64?

What to do if the value of the UInt64 is bigger than a long? There is
no built-in way to compare UInt64 values -- you may want to convert the
value to a Long (if possible) and then do the comparison.
 
O

One Handed Man [ OHM# ]

Glad I could help you.

Regards - OHM


Wolfgang said:
Thanks so much for your help!!!

I ended up converting the UInt64 to a Long, which is not without
risk, but talking about disk space, I should be OK for another dozen
years. lDiskSpace = Convert.ToDecimal(uiDiskSpace)

By converting the Long to a UInt64 I ran into the next problem, that
the operand '<' is not defined for UInt64.

I'm not sure if there would have been a way around that.

I'm just not sure, of why they used UInt64 for Diskspace. A Long
should have worked for a while longer, no?

Wolfgang

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
S

Stephen Martin

My apologies, I see that I should have shown the necessary change to that
line as well.

uiDiskSpace = Convert.ToInt64((objDisk("FreeSpace")))
 
K

Ken Tucker [MVP]

Hi,

Dim u As UInt64 = UInt64.Parse("100")

Dim l As Long = 100

Me.Text = u.Equals(UInt64.Parse(l.ToString))

Ken
 
K

Ken Tucker [MVP]

Hi,

Try this.

Dim u As UInt64 = UInt64.Parse("100")

Dim l As Long = 100

if u.Equals(UInt64.Parse(l.ToString)) then
' They are equal
end if

Ken
 
K

Ken Tucker [MVP]

Hi,


Dim uTemp As UInt64
Dim lTemp As Long

lTemp = 100

uTemp = UInt64.Parse("100")

If uTemp.Equals(lTemp.ToString) Then

'They are equal

End If

Ken
 
Y

Yan-Hong Huang[MSFT]

Hi,

A small revision:
Convert.ToUInt64

:)

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Y

Yan-Hong Huang[MSFT]

Hi,

I agree with you on it.

I think we could use UInt64.Parse(long.tostring()) to convert a long to
UInt64 first, then use UInt64 to compare them.

Wolfgang, does that answer your question?

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top