none
How to save a decimal variable to a text file and then restore it in vb? RRS feed

  • Question

  • Saving the decimal value to a text file is okay, but restore it from the file is a problem because CDEC doesn't work on strings (you can't convert a string to a decimal value). I saw somewhere that "serialization" can be used to accomplish this, but I don't know nothin' about serialization. Can anyone offer a SIMPLE explanation of serialization? Or, tell me another way. (Actually, I thought of one "rough" and inexact approach - convert the decimal values (representing HOURS or parts thereof) to integer SECONDS to store it, then convert it from integer seconds to decimal hours when restoring. But that IS slightly "inexact", and why make things that complicated if you don't have to.)


    Robert Homes

    Sunday, October 7, 2018 1:32 AM

Answers

  • (you can't convert a string to a decimal value). 

    Yes you can. Study this example:

    Dim D As Decimal = 123.45
    Dim str As String = D.ToString
    
    Dim D2 As Decimal
    Dim b As Boolean = Decimal.TryParse(str, D2)
    
    If b Then
        Console.WriteLine("{0} {1} {2}", D, str, D2)
    Else Console.WriteLine("Conversion of {0} to Decimal failed!", str)
    End If
    

    - Wayne

    • Marked as answer by Robert Homes Sunday, October 7, 2018 12:15 PM
    Sunday, October 7, 2018 2:00 AM

All replies

  • Here is a very simple example of serializing 

    <Serializable()>
    Public Class Demo
        Public Property DecimalValue() As Decimal
    End Class

    .

    Public Class DemoContainer
        Public Property FileName() As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Demo.bin")
        Public Property Item() As Demo
        Public Sub Save(pDemo As List(Of Demo))
            Dim bf As New BinaryFormatter
            Try
    
                File.Delete(FileName)
    
                Using fs As New FileStream(FileName, FileMode.Create)
    
                    For Each person As Demo In pDemo
                        bf.Serialize(fs, person)
                    Next
    
                    fs.Close()
    
                End Using
            Catch ex As Exception
                ' decide how to handle
            Finally
                If Not (bf Is Nothing) Then
                    bf = Nothing
                End If
            End Try
    
        End Sub
    
        Public Function Load() As List(Of Demo)
            Dim bf As New BinaryFormatter
            Dim item As Object
            Dim demp As Demo
    
            Dim theList = New List(Of Demo)
    
            Dim fs As New FileStream(FileName, FileMode.OpenOrCreate)
    
            Try
                Do
                    item = bf.Deserialize(fs)
    
                    If item.GetType Is GetType(Demo) Then
                        demp = CType(item, Demo)
                        theList.Add(demp)
                    End If
    
                Loop While fs.Position < fs.Length - 1
    
            Finally
                fs.Close()
                If Not (bf Is Nothing) Then
                    bf = Nothing
                End If
            End Try
    
            Return theList
    
        End Function
    
    End Class

    Usage

    Dim ops = New DemoContainer()
    ops.Save(New List(Of Demo)() From {New Demo() With {.DecimalValue = 12.23D}, New Demo() With {.DecimalValue = 1.3D}})
    Dim results = ops.Load()
    Console.WriteLine("Items")
    For Each item As Demo In results
        Console.WriteLine(item.DecimalValue)
    Next
    Console.WriteLine("One item")
    Console.WriteLine(results.FirstOrDefault().DecimalValue)

    Results to show up in the IDE Output window

    Items
    12.23
    1.3
    One item
    12.23


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, October 7, 2018 1:56 AM
    Moderator
  • (you can't convert a string to a decimal value). 

    Yes you can. Study this example:

    Dim D As Decimal = 123.45
    Dim str As String = D.ToString
    
    Dim D2 As Decimal
    Dim b As Boolean = Decimal.TryParse(str, D2)
    
    If b Then
        Console.WriteLine("{0} {1} {2}", D, str, D2)
    Else Console.WriteLine("Conversion of {0} to Decimal failed!", str)
    End If
    

    - Wayne

    • Marked as answer by Robert Homes Sunday, October 7, 2018 12:15 PM
    Sunday, October 7, 2018 2:00 AM
  • Yes, it worked! Thanks Wayne. I need to get acquainted with that there PARSE stuff. Never dealt with it before, but it seems to be very handy.

    Robert Homes


    Sunday, October 7, 2018 12:15 PM