locked
C# to VB Conversion RRS feed

  • Question

  • User-2097567671 posted

    Hi,

    I had to convert some C# code to VB and I am getting the following error:  "Value of type 'Double' cannot be converted to 'Long'"

    I am getting it on the following code:

    Private Shared Function SignVals(key As String, username As String, ikey As String, prefix As String, expire As Int64, current_time As DateTime) As String
    
                Dim ts As Int64 = DirectCast((current_time - New DateTime(1970, 1, 1)).TotalSeconds, Int64)
                expire = ts + expire
    
                Dim val As String = (Convert.ToString(username & Convert.ToString("|")) & ikey) + "|" + expire.ToString()
                Dim cookie As String = Convert.ToString(prefix & Convert.ToString("|")) & Encode64(val)
    
                Dim sig As String = HmacSign(key, cookie)
    
                Return Convert.ToString(cookie & Convert.ToString("|")) & sig
            End Function



    Does anyone know how to fix this?

    Thanks.

    Tuesday, October 22, 2013 1:13 PM

Answers

  • User281315223 posted

    You might try simply using the Convert.ToInt64 method as seen below and revising over your Convert.ToString() methods : 

    Private Shared Function SignVals(key As String, username As String, ikey As String, prefix As String, expire As Int64, current_time As DateTime) As String
    
            Dim ts As Int64 = Convert.ToInt64((current_time - New DateTime(1970, 1, 1)).TotalSeconds)
            expire = ts + expire
    
            Dim val As String = username & "|" & ikey & "|" & expire.ToString()
            Dim cookie As String = prefix & "|" & Encode64(val)
    
            Dim sig As String = HmacSign(key, cookie)
    
            Return cookie & "|" & sig
    End Function



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 22, 2013 1:40 PM

All replies

  • User-993404089 posted

    Did you try to use one of these?

     

    http://blog.arvixe.com/converting-c-to-vb-and-back-again-2/

    Tuesday, October 22, 2013 1:25 PM
  • User-2097567671 posted

    Yes, I used the one from Telerik.

    Tuesday, October 22, 2013 1:39 PM
  • User281315223 posted

    You might try simply using the Convert.ToInt64 method as seen below and revising over your Convert.ToString() methods : 

    Private Shared Function SignVals(key As String, username As String, ikey As String, prefix As String, expire As Int64, current_time As DateTime) As String
    
            Dim ts As Int64 = Convert.ToInt64((current_time - New DateTime(1970, 1, 1)).TotalSeconds)
            expire = ts + expire
    
            Dim val As String = username & "|" & ikey & "|" & expire.ToString()
            Dim cookie As String = prefix & "|" & Encode64(val)
    
            Dim sig As String = HmacSign(key, cookie)
    
            Return cookie & "|" & sig
    End Function



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 22, 2013 1:40 PM
  • User-2097567671 posted

    Perfect!  Thanks!

    Tuesday, October 22, 2013 1:50 PM