Question integers value from user?

Chuckles56

New member
Joined
Jun 1, 2011
Messages
1
Programming Experience
1-3
Hi,
I'm new to console apps,and is there an way you can set an integers value by an value typed by the user?
And is there an way you can save the value?
 
Top:
Dim intInput as Integer
Try
Console.writeline("Please enter an integer: ")
intInput = Cint(Console.Readline)
Catch Ex As Exception
Console.Writeline("Please enter an integer value! Back to top..")
intInput = Nothing
Goto Top
End Try
'now intInput should have your integer value
 
quick demo

[edit] ss7thirty - didnt see your post, was writing my while you posted your i guess.



Hi,
I'm new to console apps,and is there an way you can set an integers value by an value typed by the user?
And is there an way you can save the value?

Your question seems a bit vague, how are you trying to save the value?

Heres a quick sample i just wrote, this shows how to save something to your settings, and a textfile on the computer. Quick Demo. Otherwise explain how you are trying to save this integer and expand your question.

Note to have the settings work you must add it into your settings either via the app.config file or via the settings tab in project properties found via the solution explorer. How to: Add or Remove Application Settings

Imports System.IO

Module Module1
    Dim fileLocation As String = "C:\Testing\Test.txt"

    Sub Main()
        ''
        ''STORED TEMPORARY IN MEMORY
        ''

        Dim intInMem As Integer = 0

        Console.WriteLine("Enter a number: ")
        'note if a number is not entered there will be a conversion error.
        ''EDIT using method mentioned by JohnH
        If Not Integer.TryParse(Console.ReadLine(), intInMem) Then
            Console.WriteLine("Error. Must Be integer...")
        End If

        Console.WriteLine("You Entered {0}.", intInMem)

        ''
        ''USING A FILE
        ''
        Try
            If Not readFromFile() Then
                Dim intToSave As Integer = 0

                Console.WriteLine("Please enter a number to save to a file: ")
                ''EDIT
                If Not Integer.TryParse(Console.ReadLine(), intToSave) Then
                    Console.WriteLine("Error. Must Be integer...")
                End If

                File.WriteAllText(fileLocation, intToSave.ToString())
                Console.WriteLine("File Saved.")
            End If
        Catch ex As Exception
            Console.WriteLine("An Unexpected Error occurred trying to read/write to file. :: {0}", ex.Message)
        End Try


        ''
        ''USING SETTINGS
        ''

        'add to your settings of the appliation
        '   a setting of Integer Type
        '   named: SettingsSavedNumber
        Console.WriteLine("This setting should be stored {0} in the directory with [MANUFACTUR]/[PROGRAMNAME]/[VERSION#?]/[user.config]", Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))

        'program will not run if the Setting does not exist, be sure to add if testing
        Console.WriteLine("Currently saved in settings {0}", My.Settings.SettingsSavedNumber)
        Console.WriteLine("Enter new number: ")

        If Not Integer.TryParse(Console.ReadLine(), My.Settings.SettingsSavedNumber) Then
            Console.WriteLine("Error. Must Be integer...")
        End If
        My.Settings.Save()

        Console.WriteLine("Settings have been saved. Press any key to exit.")
        Console.ReadLine()
    End Sub

    Function readFromFile() As Boolean
        If File.Exists(fileLocation) Then
            Dim fileContents As String = File.ReadAllText(fileLocation)

            Console.WriteLine("{0} was found in the saved file.", fileContents)
            Return True
        Else
            Console.WriteLine("No previous data was saved. File not found at: {0}.", fileLocation)
            Return False
        End If
    End Function
End Module
 
Last edited:
You should use Integer.TryParse when attempting to convert input from user that is supposed to be an integer number. Try-CInt-Catch adds unneccesary exception handling to this.
Dim input As Integer
If Integer.TryParse(Console.ReadLine, input) Then
    'input variable contains valid integer.
Else
    'user didn't write a valid integer.
End If
 
You should use Integer.TryParse when attempting to convert input from user that is supposed to be an integer number. Try-CInt-Catch adds unneccesary exception handling to this.

Thanks for the suggestion, (edited my previous post with the tryparse) i didn't think of using that. I'm assuming other data types might have that method too, Sounds useful. I'll keep that in mind.
 
Last edited:
Back
Top