Wednesday, February 9, 2011

Case Statement

■A case statement
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim strSnack As String
  
     Dim strDinner As String
  
     strSnack = TextBox1.Text
  
     Select Case strSnack
  
       Case "Had a Snack"
  
         strDinner = "Will not have"
  
       Case "Had a little"
  
         strDinner = "Will have a little"
  
       Case Else
  
         strDinner = "Will have"
  
     End Select
  
     MessageBox.Show(strDinner & " Dinner.")
  
   End Sub
  
 End Class  

If Statements

■An if statement that makes one decision
 Public Class Form1   
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
  
    'Declare variables    
  
    Dim dedGrade1, decGrade2, decBetterGrade As Decimal    
  
    'Convert text box to double    
  
    decGrade1 = CDec(txtSecondGrade.Text)    
  
    decGrade2 = CDec(txtSecondGrade.Text)    
  
    'Find results    
  
    If decGrade1 > decGrade2 Then     
  
      decBetterGrade = decGrade1    
  
    Else     
  
      decBetterGrade = decGrade2    
  
    End If    
  
    'Display results    
  
    txtBestGrade.Text = "Your best grade is " & decBetterGrade & "."   
  
   End Sub  
  
 End Class  

■An if statement where it's either or
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim decGPA As Decimal
  
     If decGPA < 3.5 Then
  
       MessageBox.Show("You did not make the dean's list")
  
     Else
  
       MessageBox.Show("You made the dean's list")
  
     End If
  
   End Sub
  
 End Class  

■An if statement where many decisions are made
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim decGrade As Decimal
  
     If decGrade >= 91 Then
  
       MessageBox.Show("You get an A")
  
     ElseIf decGrade >= 82 Then
  
       MessageBox.Show("You get a B")
  
     ElseIf dedGrade >= 70 Then
  
       MessageBox.Show("You get a C")
  
     ElseIf decGrade >= 60 Then
  
       MessageBox.Show("You get a D")
  
     Else
  
       MessageBox.Show("You get an F")
  
     End If
  
   End Sub
  
 End Class  

Monday, February 7, 2011

Practice #1 -Values and Formulas

1.)Morgan loves collecting small stuffed animals. She has 6 cows and 7 sheep. How many animals does she have in her collection?

  Public Class Form1   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    'Declare variables   
  
 Dim intCows, intSheep, intTotal As Integer    
  
 'Set values    
  
 intCows = 6   
  
 intSheep = 7   
  
 intTotal = C + S   
  
 'Display results    
  
 MessageBox.Show("Morgan has " & intTotal & " stuffed animals.")   
  
 End Sub  
  
 End Class  

2.)Diane bought 7 mega burgers. Each mega burger cost $4. How many dollars did she spend on the mega burgers?

 Public Class Form1   
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
  
  'Declare variables    
  
 Dim intMegaBurgers As Integer
  
 Dim decCost, decTotalPrice As Decimal
  
 'Set values    
  
 intMegaBurgers = 7    
  
 decCost = 4    
  
 decTotalPrice = intMegaBurgers * decCost   
  
 'Display results   
  
 MessageBox.Show("The Mega Burgers cost " & FormatCurrency(decTotalPrice))   
  
 End Sub 
  
 End Class  

3.)Ole has 15 apples and 12 oranges. How many pieces of fruit does he have?
-Hard code the initial facts
  Public Class Form1   
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables   
  
 Dim intApples, intOranges, intFruit As Integer    
  
 'Set values    
  
 intApples = 15    
  
 intOranges = 12    
  
 intFruit = intApples + intOranges    
  
 'Display results    
  
 MessageBox.Show("Ole has " & intfFruit & " pieces of fruit.")   
  
 End Sub  
  
 End Class  

4.)There are 33 horses in a field. 15 horses go into the barn. Then 7 of them come back out. How many horses are standing in the field?
-Hard code the initial values
  Public Class Form1   
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables    
  
 Dim intField, intBarn, intOut, intTotalHorses As Integer    
  
 'Set values    
  
 intField = 33    
  
 intBarn = 15    
  
 intOut = 7    
  
 intTotalHorses = intField - intBarn + intOut    
  
 'Display results    
  
 MessageBox.Show("There are " & intTotalHorses & " horses in the field.")   
  
 End Sub
  
 End Class  

5.)Inglebert has 15 apples and 3 times as many oranges. How many pieces of fruit does she have?
-Hard code the initial facts
 Private Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables    
  
 Dim intApples, intOranges, intPiecesFruit As Integer    
  
 'Set values    
  
 intApples = 15    
  
 intOranges = intApples * 3    
  
 intPiecesFruit = intApples + intOranges    
  
 'Display results    
  
 MessageBox.Show("Inglebert has " & intPiecesFruit & " pieces of fruit.")   
  
 End Sub  
  
 End Class   

6.)Don't make this table. This is just for your reference
English Geography Mathematics Science
Andrew 12 19 18 7
Brian 22 15 7 22
The table shows quiz marks out of 25 in each subject.

-How many marks did Brian totally obtain in Mathematics and Science?
 Private Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables    
  
 Dim intMath, intScience, intMarks As Integer    
  
 'Set values    
  
 intMarks = 7 'Marks in Mathmetics   
  
 intScience = 22 'Marks in Science   
  
 intMarks = M + S    
  
 'Display results    
  
 MessageBox.Show("Brian has " & intMarks & " marks in Mathematics and Science.")   
  
 End Sub 
  
 End Class   
-How many more marks does Andrew need for a perfect score in Mathematics?
 Private Class Form1
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables    
  
 Dim intMath, intScience, intMarks As Integer    
  
 'Set values    
  
 intMath = 18 'Brian's actual marks   
  
 intScience = 25 'total possible   
  
 intMarks = intScience - intMath    
  
 'Display results    
  
 MessageBox.Show("Brian needs " & intMarks & " marks for a perfect score in Mathematics.")   
  
 End Sub  
  
 End Class   
-What is Andrew's percentage for all of the quizzes together?
 Private Class Form1  
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables    
  
 Dim intEnglish, intGeography, intMath, intScience As Integer    
  
 Dim decMarks As Decimal    
  
 'Set values 
  
 intEnglish = 12 'marks in English  
  
 intGeography = 15 'marks in Geography 
  
 intMath = 18 'marks in Mathematics    
  
 intScience = 7 'marks in Science          
  
 decMarks = (intScience + intMath + intEnglish + intGeography) / 100    
  
 'Display results    
  
 MessageBox.Show("Brian has " & FormatPercent(decMarks) & " marks for all the quizzes together.")   
  
 End Sub  
  
 End Class  

Sunday, February 6, 2011

String Concatenation

String Concatenation
 Dim strHotel as String
  
 Dim strPartOne as String = "This is"
  
 Dim strPartTwo as String = "the best"
  
 strHotel= strPartOne & strPartTwo  

Convert Decimal to Integer

Convert Decimal to Integer
 Dim decLose as Decimal = 23
  
 Dim intControl as Integer
  
 intControl= CInt(decLose)  

Convert Integer to Decimal

Convert Integer to Decimal

 Dim intControl as Integer= 56
  
 Dim decLose as Decimal
  
 decLose= CDec(intControl)  

Wednesday, February 2, 2011

Convert a Number Variable into a String

Blog post: blog how to convert a number variable into a string
 Dim intNumbers as Integer

 Dim strNumber as String
  
 intNumbers= "24"
  
 strNumber = CStr(intNumbers)