4 Replies - 781 Views - Last Post: 07 February 2009 - 11:10 AM Rate Topic: -----

#1 Pvk258  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-February 09

Need help with visual basic question

Posted 07 February 2009 - 09:36 AM

I need help with this following array question.

Create an array-based project to track and report on the number of kilometers travelled by a team of salespeople.Salespeople have ID numbers ranging from 1 to 12, and there are 12 salespeople in total.Separately, each salesperson travels between various towns each day, accumulating kilometers on their cars.The application should input the kilometers travelled by particular salespeople,and provide a lookup facility to find how many kilometers have been travelled by a particular salesperson.
This is what I have done so far.
Public Class KilometerForm

Dim SalesKm(11, 1) As Integer '12rows 2 columns
Dim SalesID As Integer = 1
Dim KmTravelled As Integer

Private Sub SubmitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubmitButton.Click
'Verify sales id as number
If Not Integer.TryParse(SalesIDTextBox.Text, SalesID) Then
'Not a number
Exit Sub
Else
SalesKm(SalesID - 1, 0) = SalesID
End If
If Not Integer.TryParse(KilometersTextBox.Text, KmTravelled) Then
Exit Sub
End If
End Sub


Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close()
End Sub

End Class


When I run it nothing happens and there is no errors,I would be grateful if someone told me what to do or give me the remaining code on the array part.Once I have that done I will be able to do the rest of the question myself.I am not asking anyone to do the full question for me just the array part.

This post has been edited by Pvk258: 07 February 2009 - 09:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with visual basic question

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4436
  • View blog
  • Posts: 12,307
  • Joined: 18-April 07

Re: Need help with visual basic question

Posted 07 February 2009 - 10:31 AM

First of all, no need for a 2 dimensional array. One dimensional will be fine. It is going to be an array which will have 12 slots (0 through 11) and each slot will contain the kilometers traveled.

Dim SalesKm(11) as Integer

' Now lets say salesmen 10 racked up 34 km...

SalesKm(SalesID - 1) +=  34



In the code above we are saying take the SalesID of the salesmen, subtract 1 to find their position in the array (since we start at zero) and then add 34 to the value.

There is a reason the question is saying the SalesID is 1 through 12, so you can use that as the subscript of your array.

Then when you go to look up that sales person you can use the salesID simply...

MessageBox.Show("Sales person " & SalesID.ToString() & " racked up " & SalesKm(SalesID - 1).ToString() & " km")



Now if we go back to your code we see that if we collect the SalesID, and all that is fine, we go onto collecting the KmTravelled and once we have both we use the SalesID to access the appropriate subscript of the array and add KmTravelled to its current value.

Hope that makes sense. :)

"At DIC we be km traveling code ninjas... 300,000 km and counting! My odometer has rolled over 3 times already!" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 General Adamus  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 110
  • Joined: 05-February 09

Re: Need help with visual basic question

Posted 07 February 2009 - 10:50 AM

You would need at least a combobox to select which salesperson you are incrementing so...

Private SalesKm(11)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	If IsNumeric(TextBox1.Text) Then
		SalesKm(ComboBox1.SelectedIndex) += CType(TextBox1.Text, Integer)
	End If
End Sub


Adamus
Was This Post Helpful? 1
  • +
  • -

#4 General Adamus  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 110
  • Joined: 05-February 09

Re: Need help with visual basic question

Posted 07 February 2009 - 11:03 AM

Adding to the above:

Public Class Form1
	Private SalesPeople() As String = New String() {"Adam", "John", "Jim", "Jane"}
	Private SalesKm(3) As Integer

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		If IsNumeric(TextBox1.Text) Then
			SalesKm(ComboBox1.SelectedIndex) += CType(TextBox1.Text, Integer)
		End If

		ListBox1.Items.Clear()
		For i As Integer = 0 To 3
			ListBox1.Items.Add(SalesPeople(i) & " = " & SalesKm(i))
		Next
	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		ComboBox1.DataSource = SalesPeople
	End Sub
End Class


Adamus
Was This Post Helpful? 1
  • +
  • -

#5 Pvk258  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-February 09

Re: Need help with visual basic question

Posted 07 February 2009 - 11:10 AM

The code works successful.
Thanks.

This post has been edited by Pvk258: 07 February 2009 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1