
Variable Type Conversions and Data Type Checking in Programming
Learn about converting variable types using methods and functions in programming. Explore the use of `Convert` class, functions like `IsNumeric`, and examples with `TextBox` and `InputBox`. Understand the `Try...Catch` statement for error handling in programming.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Introduction to Programming Lecture 4 Msury Mahunnah, Department of Informatics, Tallinn University of Technology
Converting Variable Types The methods of the Convert class that perform data-type conversions: ToBoolean, ToByte, ToChar, ToDateTime, ToDecimal, ToDouble, ToInt16, ToInt32, ToInt64, ToSByte, ToShort, ToSingle, ToString, ToUInt16, ToUInt32, ToUInt64 Example: Dim x$ = 21.03.2011", y As Date y = Convert.ToDateTime(x) y = #3/21/2011#
Converting Variable Types 2 Functions that perform data-type conversions: CBool, CByte, CChar, CDate, CDec, CDbl, CInt, CLng, CSByte, CShort, CSng, CStr, CUInt, CULng, CUShort Example: Dim x$ = 21.03.2011", y As Date y = CDate(x) y = #3/21/2011#
Functions for checking data type IsNumeric() Returns True if its argument is a number (Short, Integer, Long, Single, Double, Decimal). IsDate() Returns True if its argument is a valid date (or time). IsArray() Returns True if its argument is an array.
An example with IsNumeric (TextBox) Private Sub Button1_Click(ByVal sender As System.Object, ...) Dim x% If Not IsNumeric(TBox_Age.Text) Then MsgBox("A number is expected!") TBox_Age.Text = "" TBox_Age.Focus() Exit Sub End If x = Convert.ToInt32(TBox_Age.Text) floating-point number??? MsgBox("You are " & x & " years old!") End Sub Three objects on the form are needed: TextBox with a Name TBox_Age Button with a Name Button_1 Label with a Text Age
A example with IsNumeric (InputBox) Private Sub Button1_Click(ByVal sender As System.Object, ...) Dim strAge As String = "" Dim Age As Integer Do While Not IsNumeric(strAge) strAge = InputBox( Please enter your age") Loop Age = Convert.ToInt32(strAge) if strAge is a floating-point number? MsgBox("You are " & Age & " years old!") End Sub A object on the form is needed: Button with a Name Button_1
Try ... Catch Try ... Catch statement At first, execute sentence(s)1, but if the error appear, execute sentence(s)2 Try sentence(s)1 Catch [ex As Exception] sentence(s)2 [Finally sentence(s)3] End Try [ ] this part of structure is not necessary
Try ... Catch and Textbox Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Age% Try Age = Convert.ToInt32(TBox_Age.Text) MsgBox("You are " & Age & " years old!") Catch ex As Exception MsgBox(ex.Message) 'MsgBox("A number is expected!") Finally TBox_Age.Text = "" TBox_Age.Focus() End Try if an error appears go there End Sub
Try ... Catch and Inputbox Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strAge As String = "" Dim Age As Integer Do strAge = InputBox("Please enter your age") Try Age = Convert.ToInt32(strAge) if error here, go to there Exit Do Exit Do when previous sentence doesn t give error Catch ex As Exception MsgBox(ex.Message) End Try Loop MsgBox("You are " & Age & " years old!") End Sub