Friday, May 6, 2011

Methods 1 - Text Analyzer

 Public Class Form1
  
   Dim nwordcount As Integer
  
   Dim ncharcount As Integer
  
   Dim nspacecount As Integer
  
   Dim nsentencecount As Integer
  
   Public Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
  
     Dim strMaterial As String = txtMaterial.Text
  
     ncharcount = GetCharacterCount(strMaterial)
  
     nsentencecount = GetSentenceCount(strMaterial)
  
     nspacecount = GetSpaceCount(strMaterial)
  
     nwordcount = GetWordCount(strMaterial)
  
     RefreshTextboxes()
  
   End Sub
  
   Private Function GetCharacterCount(ByVal str As String) As Integer
  
     Return str.Length
  
   End Function
  
   Private Function GetSentenceCount(ByVal str As String) As Integer
  
     Dim counter As Integer = 0
  
     For Each ending As String In str
  
       If ending.Contains(".") Or ending.Contains("?") Or ending.Contains("!") Then
  
         counter += 1
  
       End If
  
     Next
  
     Return counter
  
   End Function
  
   Private Function GetSpaceCount(ByRef str As String) As Integer
  
     Dim counter As Integer = 0
  
     For Each Space As String In str
  
       If Space.Contains(" ") Then
  
         counter += 1
  
       End If
  
     Next
  
     Return counter
  
   End Function
  
   Private Function GetWordCount(ByRef str As String) As Integer
  
     Dim counter As Integer = System.Text.RegularExpressions.Regex.Matches(str, "\S+").Count
  
     Return counter
  
   End Function
  
   Public Sub RefreshTextboxes()
  
     txtCharacters.Text = ncharcount
  
     txtSentences.Text = nsentencecount
  
     txtSpaces.Text = nspacecount
  
     txtWords.Text = nwordcount
  
   End Sub
  
 End Class
  

Methods 2 - Yahtzee

 Public Class Form1
  
   'Dim variables used in different Methods
  
   Dim lblDice1, lblDice2, lblDice3, lblDice4, lblDice5 As New Label
  
   Dim WithEvents butRoll As New Button
  
   Dim nYatzee, nFourOfAKind, nThreeOfAKind As New Integer
  
   Dim lblYatzee, lblFourOfAKind, lblThreeOfAKind As New TextBox
  
   Dim rnd As New Random
  
   Private Sub AddDice(ByRef lbl As Label, ByVal x As Integer, ByVal y As Integer)
  
     'Method for adding the differe labels to display the numbers rolled
  
     lbl.Text = 0
  
     lbl.Location = New Point(x, y)
  
     lbl.Font = New Drawing.Font("Microsoft Sans Serif", 28.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
  
     lbl.Height = 40
  
     lbl.Width = 40
  
     Me.Controls.Add(lbl)
  
   End Sub
  
   Private Sub AddOutput(ByRef lbl As TextBox, ByVal outcome As String, ByVal x As Integer, ByVal y As Integer)
  
     'Method of adding the output from the roll into the label
  
     lbl.Text = outcome
  
     lbl.Location = New Point(x, y)
  
     lbl.Width = 150
  
     Me.Controls.Add(lbl)
  
   End Sub
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Adding the labels to display the dice
  
     AddDice(lblDice1, 10, 20)
  
     AddDice(lblDice2, 70, 20)
  
     AddDice(lblDice3, 130, 20)
  
     AddDice(lblDice4, 190, 20)
  
     AddDice(lblDice5, 250, 20)
  
     'Adding the actual numbers that have been rolled into the dice labels
  
     AddOutput(lblYatzee, "Yahtzee: 0", 20, 140)
  
     AddOutput(lblFourOfAKind, "Four Of A Kind: 0", 20, 180)
  
     AddOutput(lblThreeOfAKind, "Three Of A Kind: 0", 20, 220)
  
     'Creating the Roll button
  
     butRoll.Text = "Roll"
  
     butRoll.Location = New Point(100, 90)
  
     'Adding the roll button and output lables
  
     Me.Controls.Add(butRoll)
  
     Me.Controls.Add(lblFourOfAKind)
  
     Me.Controls.Add(lblThreeOfAKind)
  
   End Sub
  
   Private Sub roll(ByRef lbl As Label)
  
     'Method telling how to roll the dice
  
     lbl.Text = rnd.Next(1, 7)
  
   End Sub
  
   Private Sub RollDice() Handles butRoll.Click
  
     'Rolling the Dice
  
     roll(lblDice1)
  
     roll(lblDice2)
  
     roll(lblDice3)
  
     roll(lblDice3)
  
     roll(lblDice4)
  
     roll(lblDice5)
  
     'Getting the stats from rolling the dice
  
     stats()
  
     'Updating the textboxes as to what was rolled
  
     UpdateTextboxes()
  
   End Sub
  
   Private Sub stats()
  
     'Creating an array to keep track of what was rolled and deciding if a three of a kind, four of a kind, or yahtzee
  
     Dim arrNumbers() As Integer = {0, 0, 0, 0, 0, 0}
  
     For Each lbl As Label In Me.Controls.OfType(Of Label)()
  
       arrNumbers(lbl.Text - 1) += 1
  
     Next
  
     For Each i As Integer In arrNumbers
  
       If i = 5 Then
  
         nYatzee += 1
  
       ElseIf i = 4 Then
  
         nFourOfAKind += 1
  
       ElseIf i = 3 Then
  
         nThreeOfAKind += 1
  
       End If
  
     Next
  
   End Sub
  
   Private Sub UpdateTextboxes()
  
     'Method stating to update the text boxes after each roll
  
     lblYatzee.Text = "Yatzees: " & nYatzee
  
     lblFourOfAKind.Text = "Four Of A Kind: " & nFourOfAKind
  
     lblThreeOfAKind.Text = "Three Of A Kind: " & nThreeOfAKind
  
   End Sub
  
 End Class
  

Subquery Practice

 use AdventureWorks
  
 select PC.FirstName, PC.LastName, PC.EmailAddress, HE.Title
  
 from Person.Contact PC
  
 inner join HumanResources.Employee HE
  
 on PC.ContactID = HE.ContactID
  
 where HE.EmployeeID in
  
      (
  
      select SS.SalesPersonID
  
      from Sales.SalesPerson SS
  
      )
  
 select SP.salespersonID, PC.EmailAddress
  
 from Sales.SalesPerson SP
  
 inner join HumanResources.Employee HRE
  
 on SP.SalesPersonID = HRE.EmployeeID
  
 inner join Person.Contact PC
  
 on HRE.ContactID = PC.ContactID
  
 where SP.SalesPersonID in 
  
      (
  
      select SOH.SalesPersonID
  
      from Sales.SalesOrderHeader SOH
  
      where SOH.TotalDue > '150000'
  
      )
  
 select PP.productID, (PP.ListPrice - PP.StandardCost) MarkUp
  
 from Production.Product PP
  
 where PP.ProductID not in
  
      (
  
      select SOD.ProductID
  
      from Sales.SalesOrderDetail SOD
  
      )
  
 order by MarkUp desc
  
 use AdventureWorks
  
 declare @top_sales_person int
  
 set @top_sales_person = 
  
      (
  
      select top 1 SP.SalesPersonID
  
      from Sales.SalesPerson SP
  
      order by SP.SalesLastYear desc
  
      )
  
 declare @customers int
  
 set @customers =
  
      (
  
      select COUNT (distinct customerid)
  
      from Sales.SalesOrderHeader SOH
  
      where SOH.SalesPersonID = @top_sales_person
  
      )
  
 declare @total_sales int
  
 set @total_sales =
  
      (select COUNT (distinct SOH.SalesOrderID)
  
      from Sales.SalesOrderHeader SOH
  
      where SOH.SalesPersonID = @top_sales_person
  
      )
  
 select PC.FirstName, PC.LastName, DATEDIFF ( year, HRE.HireDate, GETDATE() ) Tenure, @customers Customers 
  
 , @total_sales Total_Sales
  
 from Person.Contact PC
  
 inner join HumanResources.Employee HRE
  
 on PC.ContactID = HRE.ContactID
  
 inner join Sales.SalesPerson SP
  
 on HRE.EmployeeID = SP.SalesPersonID
  
 where SP.SalesPersonID = @top_sales_person  

Monday, May 2, 2011

Programming Final

Calculator

 Public Class Form1
  
   Dim number1 As Decimal
  
   Dim number2 As Decimal
  
   Dim use As Integer
  
   Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
  
     txtDisplay.Text = txtDisplay.Text & btn1.Text
  
   End Sub
  
   Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
  
     txtDisplay.Text = txtDisplay.Text & btn2.Text
  
   End Sub
  
   Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
  
     txtDisplay.Text = txtDisplay.Text & btn3.Text
  
   End Sub
  
   Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
  
     txtDisplay.Text = txtDisplay.Text & btn4.Text
  
   End Sub
  
   Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
  
     txtDisplay.Text = txtDisplay.Text & btn5.Text
  
   End Sub
  
   Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
  
     txtDisplay.Text = txtDisplay.Text & btn6.Text
  
   End Sub
  
   Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
  
     txtDisplay.Text = txtDisplay.Text & btn7.Text
  
   End Sub
  
   Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
  
     txtDisplay.Text = txtDisplay.Text & btn8.Text
  
   End Sub
  
   Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
  
     txtDisplay.Text = txtDisplay.Text & btn9.Text
  
   End Sub
  
   Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  
     txtDisplay.Text = " "
  
   End Sub
  
   Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  
     number1 = number1 + Val(txtDisplay.Text)
  
     use = 1
  
     txtDisplay.Clear()
  
   End Sub
  
   Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
  
     number1 = number1 + Val(txtDisplay.Text)
  
     use = 2
  
     txtDisplay.Clear()
  
   End Sub
  
   Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
  
     number1 = number1 + Val(txtDisplay.Text)
  
     use = 3
  
     txtDisplay.Clear()
  
   End Sub
  
   Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
  
     number1 = number1 + Val(txtDisplay.Text)
  
     use = 4
  
     txtDisplay.Clear()
  
   End Sub
  
   Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
  
     number2 = number2 + Val(txtDisplay.Text)
  
     If use = 1 Then
  
       txtDisplay.Text = number1 + number2
  
     ElseIf use = 2 Then
  
       txtDisplay.Text = number1 - number2
  
     ElseIf use = 3 Then
  
       txtDisplay.Text = number1 * number2
  
     ElseIf use = 4 Then
  
       txtDisplay.Text = number1 / number2
  
     End If
  
     number1 = 0
  
     number2 = 0
  
     use = 0
  
   End Sub
  
   Private Sub btnDeciamal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeciamal.Click
  
     txtDisplay.Text = txtDisplay.Text & btnDeciamal.Text
  
   End Sub
  
 End Class
  

Wednesday, April 13, 2011

If and Case Statements

declare @num int
set @num =4
declare @salespersonid int

select @num

if @num = 3
begin
 set @salespersonid = (select top 1 Sales.SalesPerson.SalesPersonID from Sales.SalesPerson)
end
else if @num = 4
begin
 select 'boo...no 3'
end
else
begin
 select 'nun of the above'
end

use AdventureWorks
select FirstName, LastName,
case emailpromotion
 when 1 then 'Email Promo'
 when 2 then 'Text Promo'
 else 'Leave Me Alone'
end promo
from person.contact

Wednesday, April 6, 2011

Practice #5

1. Adding Days of The Week
 Public Class Form1
  
   Dim week(0 To 6) As String
  
   Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
  
     week(0) = "Monday"
  
     week(1) = "Tuesday"
  
     week(2) = "Wednesday"
  
     week(3) = "Thursday"
  
     week(4) = "Friday"
  
     week(5) = "Saturday"
  
     week(6) = "Sunday"
  
     For Each day As String In week
  
       MsgBox(day)
  
     Next
  
   End Sub
  
 End Class  

2. Adding Users
 Public Class Form1
  
   Dim Users As New Hashtable
  
   Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  
     Users.Add("FirstName", txtFirstName.Text)
  
     Users.Add("LastName", txtLastName.Text)
  
     Users.Add("EMail", txtEMail.Text)
  
     txtEMail.Clear()
  
     txtFirstName.Clear()
  
     txtLastName.Clear()
  
   End Sub
  
   Private Sub btnFirstName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirstName.Click
  
     MsgBox(Users.Item("FirstName"))
  
   End Sub
  
   Private Sub bntLastName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntLastName.Click
  
     MsgBox(Users.Item("LastName"))
  
   End Sub
  
   Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmail.Click
  
     MsgBox(Users.Item("EMail"))
  
   End Sub
  
 End Class  

3. Printer Queue
 Public Class Form
  
   Dim joblist As New Queue
  
   Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  
     Dim jobs As New Hashtable
  
     jobs.Add("Job Title", txtTitle.Text)
  
     jobs.Add("Page Quantity", NumericUpDown1.Value)
  
     joblist.Enqueue(jobs)
  
     For Each h As Hashtable In joblist
  
       lstJobs.Items.Add("(" & h.Item("Page Quantity") & ")" & h.Item("Job Title"))
  
     Next
  
     jobs.Clear()
  
     joblist.Clear()
  
     txtTitle.Clear()
  
     NumericUpDown1.Value = 1
  
   End Sub
  
   Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  
     lstJobs.Items.Clear()
  
   End Sub
  
 End Class
  

Monday, February 28, 2011

Practice Numer 4

#1 - Adding items to a list box with a loop
 Public Class Form1
  
   Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
  
     Dim amount As Integer = txtValue.Text
  
     For n As Integer = 1 To amount
  
       ListBox1.Items.Add(n & "items")
  
     Next
  
   End Sub
  
 End Class  

#2 - How many years will it take to save up to a certain amount of money
 Public Class Form1
  
   Private Sub btnYears_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYears.Click
  
     Dim payment As Integer = txtPayment.Text
  
     Dim counter As Integer = 1
  
     Dim amount As Integer
  
     Dim goal As Integer = 10000
  
     Dim years As Integer
  
     Do While amount < goal
  
       counter += 1
  
       amount = payment * counter
  
     Loop
  
     years = counter / 12
  
     MessageBox.Show(years & " Years")
  
   End Sub
  
 End Class  

#3 - Create a Times table using a loop within a loop
 Public Class Form1
  
   Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
  
     Dim y As Integer
  
     Dim x As Integer
  
     For x = 1 To 10
  
       For y = 1 To 10
  
         Dim btn As New Button
  
         btn.Location = New Point(35 * x, 35 * y)
  
         btn.Height = 35
  
         btn.Width = 35
  
         Me.Controls.Add(btn)
  
         btn.Text = (x * y)
  
       Next
  
     Next
  
   End Sub
  
 End Class