By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,192 Members | 1,779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,192 IT Pros & Developers. It's quick & easy.

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

P: n/a
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_LogicalDi sk")
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
Nov 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Untested . .

Dim ui As UInt64 = Convert.ToUInt64(500)

Dim int64 = Convert.ToInt64(ui)

Regards - OHM

Wolfgang Kaml wrote:
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_LogicalDi sk") 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
Nov 20 '05 #2

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

"Wolfgang Kaml" <ms@no-spam.kaml.com> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
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_LogicalDi sk")
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

Nov 20 '05 #3

P: n/a
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
uiDiskSpace = CType(objDisk("FreeSpace"), System.UInt64) touiDiskSpace = CType(objDisk("FreeSpace"), Long)

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

"Stephen Martin" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:eh**************@TK2MSFTNGP12.phx.gbl...
Unless you're expecting to query a disk with more than 9 billion gigabytes
of free space just declare uiDiskSpace as Long.

"Wolfgang Kaml" <ms@no-spam.kaml.com> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
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_LogicalDi sk") 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


Nov 20 '05 #4

P: n/a
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

"One Handed Man [ OHM# ]" <OneHandedMan{at}BTInternet{dot}com> wrote in
message news:O5**************@TK2MSFTNGP09.phx.gbl...
Untested . .

Dim ui As UInt64 = Convert.ToUInt64(500)

Dim int64 = Convert.ToInt64(ui)

Regards - OHM

Wolfgang Kaml wrote:
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_LogicalDi sk") 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

Nov 20 '05 #5

P: n/a
* "Wolfgang Kaml" <ms@no-spam.kaml.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

P: n/a
Glad I could help you.

Regards - OHM
Wolfgang Kaml wrote:
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

"One Handed Man [ OHM# ]" <OneHandedMan{at}BTInternet{dot}com> wrote
in message news:O5**************@TK2MSFTNGP09.phx.gbl...
Untested . .

Dim ui As UInt64 = Convert.ToUInt64(500)

Dim int64 = Convert.ToInt64(ui)

Regards - OHM

Wolfgang Kaml wrote:
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_LogicalDi sk") 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


Regards - OHM# OneHandedMan{at}BTInternet{dot}com
Nov 20 '05 #7

P: n/a
My apologies, I see that I should have shown the necessary change to that
line as well.

uiDiskSpace = Convert.ToInt64((objDisk("FreeSpace")))
"Wolfgang Kaml" <ms@no-spam.kaml.com> wrote in message
news:e6**************@TK2MSFTNGP11.phx.gbl...
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
uiDiskSpace = CType(objDisk("FreeSpace"), System.UInt64) touiDiskSpace = CType(objDisk("FreeSpace"), Long)
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

"Stephen Martin" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:eh**************@TK2MSFTNGP12.phx.gbl...
Unless you're expecting to query a disk with more than 9 billion gigabytes of free space just declare uiDiskSpace as Long.

"Wolfgang Kaml" <ms@no-spam.kaml.com> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
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_LogicalDi sk") 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



Nov 20 '05 #8

P: n/a
Hi,

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

Dim l As Long = 100

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

Ken

-----------------------

"Wolfgang Kaml" <ms@no-spam.kaml.com> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
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_LogicalDi sk")
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

Nov 20 '05 #9

P: n/a
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
---------------
"Wolfgang Kaml" <ms@no-spam.kaml.com> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
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_LogicalDi sk")
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

Nov 20 '05 #10

P: n/a
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

--------------------

"Wolfgang Kaml" <ms@no-spam.kaml.com> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
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_LogicalDi sk")
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

Nov 20 '05 #11

P: n/a
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.

Nov 20 '05 #12

P: n/a
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.

Nov 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.