locked
Program is closing unexpectedly RRS feed

  • Question

  • I simple have a single form in my application with two labels, label1 and label2. I just have the below code on the form load event.

            ' Getting Ram Information
    
            Dim TotalRam As String
            TotalRam = My.Computer.Info.TotalPhysicalMemory
    
            Dim Bytes As Integer
            Bytes = 1073741824
    
            Dim CalculationOfRam As Integer
            CalculationOfRam = CInt(TotalRam)
    
            Dim RamInGB As Integer
            RamInGB = CalculationOfRam / Bytes
    
            label1.Text = RamInGB & " Gigabytes"
    
    
    
            ' Getting OS Information
    
            label2.Text = My.Computer.Info.OSVersion

    The above code was working well a week ago, but now this is not working on all the machine in my office and after running the program it is closing unexpectedly.

    Please help me guys, what might be going wrong with it ? :(

    • Changed type Paul Ishak Sunday, August 25, 2013 11:32 AM is a question
    Sunday, August 25, 2013 11:28 AM

Answers

  • Hi

    May be that some of the Types are wrong. I would strongly suggest you incluse Option Strict On at the very top of your code to help prevent this sort of error.

    Try this out:

            ' Getting Ram Information
    
            Dim TotalRam As ULong = My.Computer.Info.TotalPhysicalMemory
    
            Dim Bytes As Integer = 1073741824
    
            Dim CalculationOfRam As ULong = TotalRam
    
            Dim RamInGB As Double = CalculationOfRam / Bytes
    
            Label1.Text = RamInGB.ToString & " Gigabytes"
    
            ' Getting OS Information
    
            Label2.Text = My.Computer.Info.OSVersion


    Regards Les, Livingston, Scotland



    Sunday, August 25, 2013 11:47 AM
  • I get an overflow here:

     CalculationOfRam = CInt(TotalRam)

    My.Computer.Info.TotalPhysicalMemory returns a ULong

    Try this:

    Option Strict On
    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ' Getting Ram Information
            Dim TotalRam As ULong
            TotalRam = My.Computer.Info.TotalPhysicalMemory
    
            Dim Bytes As ULong
            Bytes = 1073741824
    
            Dim CalculationOfRam As ULong
            CalculationOfRam = TotalRam
    
            Dim RamInGB As Double
            RamInGB = CDbl(CalculationOfRam / Bytes)
    
            Label1.Text = RamInGB.ToString("N4") & " Gigabytes"
            ' Getting OS Information
            Label2.Text = My.Computer.Info.OSVersion
        End Sub
    End Class
    
    

    Tuesday, August 27, 2013 7:37 PM

All replies

  • Hi

    May be that some of the Types are wrong. I would strongly suggest you incluse Option Strict On at the very top of your code to help prevent this sort of error.

    Try this out:

            ' Getting Ram Information
    
            Dim TotalRam As ULong = My.Computer.Info.TotalPhysicalMemory
    
            Dim Bytes As Integer = 1073741824
    
            Dim CalculationOfRam As ULong = TotalRam
    
            Dim RamInGB As Double = CalculationOfRam / Bytes
    
            Label1.Text = RamInGB.ToString & " Gigabytes"
    
            ' Getting OS Information
    
            Label2.Text = My.Computer.Info.OSVersion


    Regards Les, Livingston, Scotland



    Sunday, August 25, 2013 11:47 AM
  • I get an overflow here:

     CalculationOfRam = CInt(TotalRam)

    My.Computer.Info.TotalPhysicalMemory returns a ULong

    Try this:

    Option Strict On
    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ' Getting Ram Information
            Dim TotalRam As ULong
            TotalRam = My.Computer.Info.TotalPhysicalMemory
    
            Dim Bytes As ULong
            Bytes = 1073741824
    
            Dim CalculationOfRam As ULong
            CalculationOfRam = TotalRam
    
            Dim RamInGB As Double
            RamInGB = CDbl(CalculationOfRam / Bytes)
    
            Label1.Text = RamInGB.ToString("N4") & " Gigabytes"
            ' Getting OS Information
            Label2.Text = My.Computer.Info.OSVersion
        End Sub
    End Class
    
    

    Tuesday, August 27, 2013 7:37 PM