VB.NET Data Types and Variables PDF
Document Details
Parmar Chetan B.
Tags
Summary
This document provides a brief overview of fundamental concepts in VB.NET programming. It covers data types, including their sizes and ranges, and introduces variables and their naming conventions. The document also touches on the concept of constants and operations in VB.NET. It's designed to provide a starting point for learners.
Full Transcript
VB.NET VB.NET Parmar Chetan B. Data Types: Data Type Size Byte 8 bit signed integer 0 to 255 Boolean 16 bit True or False Char 16 bit Unicode character 0 to 65535 Date 64 bit...
VB.NET VB.NET Parmar Chetan B. Data Types: Data Type Size Byte 8 bit signed integer 0 to 255 Boolean 16 bit True or False Char 16 bit Unicode character 0 to 65535 Date 64 bit January 1, 0001 to 128 bit +/-79,228,162,514,264,337,593,543,950,335 with no decimal Decimal point; +/-7.9228162514264337593543950335 with 28 place to right of decimal. Double 64 bit floating-point -1.79769313486231E+308 to -4.94065645841247E-324 for number negative values to 4.94065645842247E-324 to 1.79769313486231E+308 for positive values. Integer Int16 - 16 bit signed integer Int32 - 32 bit signed integer -2,147,483,648 to 2,147,483,647 Int64 - 64 bit signed integer Long 64 bit -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Object 32 bit Any type can be stored in a variable of type Object Short 16 bit -32,768 to 32,767 Single 32 bit floating-point -3.402823 * 103 to -1.401298 * 10 45 for negative values number 1.401298 * 10 -45 to 3.402823 * 10 38for positive values String 16 bit 0 to approximately 2 billion characters VB.NET Parmar Chetan B. Variables: Variables in VB.NET are used to store values and also they have a datatype and a unique name. Naming Convention: – Variables in VB.NET should start with an alphabet or a letter and should not contain any special characters like %,&,!,#,@ or $. The variable should not exceed 255 characters. Valid Name Invalid Name My_Car My.Car this year 1NewBoy Long_Name_Can_beUSE He&HisFather *& is not acceptable VB.NET Parmar Chetan B. Declaring Variables Dim {Variable Name} As {Data Type} Example: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim password As String Dim yourName As String Dim firstnum As Integer Dim secondnum As Integer Dim total As Integer Dim doDate As Date End Sub VB.NET Parmar Chetan B. Assigning Values to Variables Variable=Expression Example: – firstNumber=100 – secondNumber=firstNumber-99 – userName="Chetan Parmar" – userpass.Text = password – Label1.Visible = True – Command1.Visible = false – Label4.Caption = textbox1.Text – ThirdNumber = Val(usennum1.Text) – total = firstNumber + secondNumber+ThirdNumber VB.NET Parmar Chetan B. Constants: Constants are different from variables in the sense that their values do not change during the running of the program. Syntax: – [Private | Public | Friend | Protected Friend ] Const {Constant Name} As {Data Type} = {Value} Example: – Public Const PI As Double = 3.14159 VB.NET Parmar Chetan B. Operators: Arithmetic Operators Concatenation Operators Relational Operators Logical Operators Bit Shift Operators Compound assignment VB.NET Parmar Chetan B. Arithmetic Operators Operator Description ^ Exponentiation Operator * Multiplication Operator / Division Operator \ Integer division Operator Mod Modulus Operator + Unary and Binary Addition Operator - Unary and Binary Negation Operator VB.NET Parmar Chetan B. Concatenation Operators Operator Description Exponentiation Operator + & Multiplication Operator VB.NET Parmar Chetan B. Relational Operators Operator Description < Less than Greater than >= Greater than or equal to = Equal to Not equals VB.NET Parmar Chetan B. Logical Operators Operator Description And Both Conditions must be true Or Either one condition is true One condition can be true, but Xor not both Not None should be true VB.NET Parmar Chetan B. Bit Shift Operators Operator Description > Right shift operator VB.NET Parmar Chetan B. Compound assignment The += compound operator is one of these shorthand operators. Other compound operators are: -=, *=, \=, /=, &=, ^= VB.NET Parmar Chetan B. Option Statements Option Explicit - Set to On or Off. On is the default. Requires declaration of all variables before they are used (this is the default). Option Compare - Set to Binary or Text. This specifies if strings are compared using binary or text comparison operations. Option Strict - Set to On or Off. Off is the default. In that case, you must use explicit conversion functions, like CLng Option Infer- Set to On or Off. On is the default. It is used in VB.NET to determine the data type of a local variable declared without an as clause on the basis of the value assigned to the variable at compile time. VB.NET Parmar Chetan B. Comment: By using ' ' This is a comment beginning at the left edge of the screen. text1.Text = "Hi! Chetan…" ' This is an inline comment. VB.NET Parmar Chetan B. Type Conversion: Implicit Conversion (Widening Conversion) Explicit Conversion (Narrowing Conversion) VB.NET Parmar Chetan B. Implicit Conversion Byte -> Short -> Integer -> Long -> Decimal -> Single -> Double Example: Sub Main() Dim num1 As Integer = 100 Dim num2 As Integer = 75 Dim total As Long total = num1 + num2 Console.WriteLine("Total is : " & total) Console.ReadLine() End Sub VB.NET Parmar Chetan B. Explicit Conversion Sub Main() Dim num As Integer Dim marks As Decimal = 34.75 num = CInt(marks) Console.WriteLine("Converted value is: " & num) Console.ReadLine() End Sub VB.NET Parmar Chetan B. Explicit Conversion Function Description CBool to convert to Bool data type CByte to convert to Byte data type CChar to convert to Char data type CDate to convert to Date type CDbl to convert to Double data type CDec to convert to Decimal data type CInt to convert to Integer data type CLng to convert to Long data type CObj to convert to Object type CShort to convert to Short data type CSng to convert to Single data type CStr to convert to String data type CUInt to convert to UInteger data type CULng to convert to ULong data type CUShort to convert to UShort data type VB.NET Parmar Chetan B. CType function for conversion Sub Main() Dim text As String = "25.56" Dim per As Double per = CType(text, Double) + 1.14 Console.WriteLine("Integer value is: " & per) Console.ReadLine() End Sub VB.NET Parmar Chetan B. Decision statements: If Then Statement If Then Else Statement Nested If Then Else Statement Select Case VB.NET Parmar Chetan B. If Then Statement If [Condition] Then [Statements] End If Example: Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Val(TextBox1.Text) > 25 Then TextBox2.Text = "Eligible“ End If End Sub VB.NET Parmar Chetan B. If Then Else Statement Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Val(TextBox1.Text) >= 0 Then MsgBox("Positive") Else MsgBox("Negative") End If End Sub VB.NET Parmar Chetan B. Nested If Then Else Statement Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Val(TextBox1.Text) >= 40 Then If Val(TextBox1.Text) >= 60 Then MsgBox("You have FIRST Class") Else MsgBox("You have SECOND Class") End If Else MsgBox("You have PASS Class ") End If End Sub VB.NET Parmar Chetan B. Select Case Statement Syntax: Select Case Expression Case Expression1 Statement1 Case Expression2 Statement2 Case Expressionn Statementn... Case Else Statement End Select VB.NET Parmar Chetan B. Select Case Statement Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim c As String c = TextBox1.Text Select c Case "Red" MsgBox("Color is Red") Case "Green" MsgBox("Color is Green") Case "Blue" MsgBox("Color is Blue") Case Else MsgBox("Enter correct choice") End Select End Sub VB.NET Parmar Chetan B. Loop statements: While Wend Statement Do While Loop Statement Do Loop While Statement Do Loop Until Statement For Next Loop Statement For Each …. Next Loop Statement With …. End with Loop Statement VB.NET Parmar Chetan B. While Wend Statement Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim n As Integer n=1 While n