Aleksander Fimreite
Aleksander Fimreite

Reputation: 1449

VB - Convert long String-number to Integer

Alright, so I'm not exactly very oriented when it comes to functions available in VB. I have a string containing the current date and time and need to convert it to a integer so i can compare the time.

Dim my_str as String = "201308281110"
Dim my_int as Integer = Convert.ToInt32(my_str)

I cant do that with this string apparently. Because I think it is too long for the 32-bit integer. All the other convertions I have tried also fails. That including "ToInt64", "Int", "CInt"... So, any idea how to convert this longer string to an integer?

Upvotes: 3

Views: 11009

Answers (3)

Steven Doggart
Steven Doggart

Reputation: 43743

Normally I wouldn't do this. Several correct answers have already been given, but since you are still learning VB.NET, I feel like a more thorough answer would be helpful to you. So my answer will repeat some of what others have already said.

The Int64 and Decimal types are indeed big enough to to hold a number like that. I suspect that the reason why Convert.ToInt64 wasn't working for you is because you were trying to store the results in an Integer variable, like this:

Dim my_str as String = "201308281110"
Dim my_int as Integer = Convert.ToInt64(my_str)  ' Throws an OverflowException

The reason that fails is not because ToInt64 doesn't work. That part of the statement is actually working fine. The part that's failing is where you are assigning the my_int variable to the value. my_int is declared as an Integer. The Integer type in VB.NET is actually just a pseudonym for Int32. In other words, it's actually the same thing as this:

Dim my_str as String = "201308281110"
Dim my_int as Int32 = Convert.ToInt64(my_str)  ' Throws an OverflowException, just like the above example

To correct the problem, you need to change the type of the my_int variable to Int64 so that it will be big enough to hold the value being returned from the ToInt64 function.

Dim my_str as String = "201308281110"
Dim my_int as Int64 = Convert.ToInt64(my_str)  ' Works

In VB.NET, Long is the pseudonym for Int64, so in most cases, that's what you should use. If you are going to use Long, however, using the ToInt64 method is a little ugly. It would be easier to read to just use Long on both sides of the assignment, like this:

Dim my_str as String = "201308281110"
Dim my_int as Long = Long.Parse(my_str)  ' Works, just like above

That also makes the code marginally safer because it will still work even if the size of Long changes in the future (however unlikely that may be).

The Decimal type would also be large enough to hold the value, but it would be less efficient than using Long, so I wouldn't recommend it.

This begs the question, however, "Why are you doing this?" If you need to compare the value to another Long variable, it would make sense to do that, but then how did you get that other Long value? If you are converting both Long values from strings, then it doesn't make sense to do that. The string is already formatted in a way where it can be easily compared with other strings of the same format. For instance:

Dim dateTime1 As String = "201308281110"
Dim dateTime2 As String = "201308281850"
If dateTime1 > dateTime2 Then
    ' Doesn't get here
End If
If dateTime1 < dateTime2 Then
    ' Get's here
End If
If dateTime1 = dateTime2 Then
    ' Doesn't get here
End If

If, however, you need to parse the value to read its individual parts (e.g. date, time, year, month, hour), it makes more sense to convert the value to a DateTime value. Or, if you need to compare the string value to another value which is already stored in a DateTime variable, then, in that case, it also makes sense to convert the string to a DateTime value. In VB.NET, the pseudonym for DateTime is simply Date, so in most cases, you should just use that, like this:

Dim my_str as String = "201308281110"
Dim my_date as Date = Date.ParseExact(my_str, "yyyyMMddHHmm", System.Globalization.CultureInfo.InvariantCulture)
If my_date > Date.Now Then
    ' Do stuff
End If

Upvotes: 4

Tim Schmelter
Tim Schmelter

Reputation: 460048

But Long.Parse works as well as Convert.ToInt64 since it doesn't overflow(Int32.MaxValue is 2147483647):

Dim myLong1 = Long.Parse("201308281110")
Dim myLong2 = System.Convert.ToInt64("201308281110")

Demo

Note that Long is the same as Int64.

Upvotes: 0

sloth
sloth

Reputation: 101042

Why don't you just simply use Date? You can compare Dates with each other, so there's no need to use an integer for comparing.

Dim my_date as Date = DateTime.ParseExact(my_str, "yyyyMMddhhmm", System.Globalization.CultureInfo.InvariantCulture)

Upvotes: 5

Related Questions