-1

I'm trying to fix two warnings. The first warning is an implicit conversion from long to string on the oInfo.Length. The second is Implicit conversion from string to double on 'strSize'. This is an old .NET 1 project I'm try to convert to 4.0. How do I fix these warnings while keeping the logic?

 Dim oInfo As New System.IO.FileInfo(Server.MapPath(strVirtualPath))
    Dim strSize As String = ""
    Try
        If oInfo.Exists Then
            strSize = **oInfo.Length**
            If **strSize** < 1048576 Then
                strSize = System.Math.Round(Convert.ToInt64(strSize) / 1024, 2) & " kb"
            Else
                strSize = System.Math.Round(Convert.ToInt64(strSize) / 1048576, 2) & " mb"
            End If
        End If
    Catch ex As Exception
derekjs67
  • 77
  • 1
  • 1
  • 9

3 Answers3

3

oInfo.Length refers to System.IO.FileInfo.Length, which is a long.

So, you can't play strSize one time as Long, other as String.

In fact, you even don't need to store oInfo.Length into another variable. It's populated in object creation (when FileInfo retrieves information from file). Doing that, you don't need to Convert the value to Long.

I would rewrite this code as:

Dim oInfo As New System.IO.FileInfo(Server.MapPath(strVirtualPath))
Dim strSize As String = ""

Try
    If oInfo.Exists Then
        If oInfo.Length < 1048576 Then
            strSize = System.Math.Round(oInfo.Length / 1024, 2) & " KB"
        Else
            strSize = System.Math.Round(oInfo.Length / 1024 / 1024, 2) & " MB"
        End If
    End If
Catch ex As Exception
Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
  • `...System.IO.FileInfo.Length, which is a double.` Nope... It's a Long (as shown in the page you linked). The reason to store the length in a variable is to avoid querying the file system twice – Basic Mar 07 '14 at 17:36
  • @Basic, about querying the sistem twice.. no, it's wrong.. the FileInfo properties are populated by querying the system when creating the object. After that, it stores the value in variable `oInfo`. i.e. if file size change between two calls of `Length`, the app won't know about that and will still inform the old value. – Andre Figueiredo Mar 07 '14 at 18:21
  • 1
    @AndréFigueiredo actually, we're both partially right.. It doesn't read from the disk if it's already initialised but there is some overhead... I've put what it actually does into my answer – Basic Mar 07 '14 at 20:54
1

Something like the following. Note that if it's a number it should be stored in a datatype designed for numbers. The only time a number should be stored in a string is for display purposes (eg comma separation, units, etc)

Dim FInfo As New System.IO.FileInfo(Server.MapPath(strVirtualPath))
Dim Result As String
Try
    If oInfo.Exists Then
        Dim FileSize = FInfo.Length
        If FileSize < 1048576 Then
            Result = System.Math.Round(FileSize / 1024, 2) & " kb"
        Else
            Result = System.Math.Round(FileSize / 1048576, 2) & " mb"
        End If
    End If
Catch ex As Exception
    ....

Note that this is still inadequate (unless you know you'll never get a file smaller than 1KB or larger than 1GB). A much better solution can be found here

Edit: In response to André Figueiredo... FileInfo.Length is defined as:

Public ReadOnly Property Length() As Long
    <SecuritySafeCritical()>
    Get
        If Me._dataInitialised = -1 Then
            MyBase.Refresh()
        End If
        If Me._dataInitialised <> 0 Then
            __Error.WinIOError(Me._dataInitialised, MyBase.DisplayPath)
        End If
        If(Me._data.fileAttributes And 16) <> 0 Then
            __Error.WinIOError(2, MyBase.DisplayPath)
        End If
        Return CLng(Me._data.fileSizeHigh) << 32 Or (CLng(Me._data.fileSizeLow) And CLng((CULng(-1))))
    End Get
End Property

The _dataInitialised seems to be set by the (native) base class so I can't see when it's set. One would hope it's on construction but that doesn't seem to be the case as it's checked in a number of places.

Of course, it's all moot since the OP seems to be using Option Strict = False which adds ~30% performance penalty due to all the type checking.

Community
  • 1
  • 1
Basic
  • 26,321
  • 24
  • 115
  • 201
0

Start here:

Dim strSize As Double
T McKeown
  • 12,971
  • 1
  • 25
  • 32