I am a new student in the area with very little programming experience. We are working with arrays in our vb.Net class and I am coompletely lost. Our program has to look up part numbers in order to improve customer service. The user has to enter the part number and brand and look up the corresponding Auto number. They get to choose from 3 brands which can be selected by radio button. We can either store the part numbers in a 2-dimensional array or in an array of a structure. We have to use the part number to lookuup and display the part number. can anyone provide any assistance as to where i should start?
cassie_sanford 1 Junior Poster in Training
timothybard 12 Posting Whiz
Well, I guess the first part would be how to define an array. To define an array, you use parentheses when you define the array of a variable. For example, instead of Dim num as Integer, to define an array of integers, you use Dim num(9) as Integer. This will work for any type of variable or class. The value within the parentheses is largest index value of the array. In the example I gave, the max index is 9, which means you can store 10 values in the array because the index starts at 0.
Next you need to be able to store a value in the array. To do this, you simple type out the array name, put in the index where you want to store the value, and then store a value it is as you normally would. For example, num(2) = 5 will store 5 in the array at the places where the index is 2.
Lastly, to retrieve a value, simply refer to the array with the index of the value you want. For example, test = num(2) will store the number 5 in the variable test since 5 is in num(2).
A helpful feature in arrays is that you can loop through arrays to check the values at each index:
For counter = 0 to Ubound(num())
debug.print(num(counter))
Next
The above code will display the contents of the array in the debug window. The UBound() function returns the largest index of the array.
Two dimensional arrays are similar to one dimensional arrays. The only difference is that there are two indices. For example, Dim num(9,9) will create a 10 x 10 array. To reference any particular value, just refer to it using its two indicies; for example, num(0,6) = 5.
You can go here for more information:
http://msdn.microsoft.com/en-us/library/wak0wfyt(VS.80).aspx
cassie_sanford 1 Junior Poster in Training
Ok, I think i'm kind of understanding it. Since the user has to enter the part number and brand to look up corresponding part numbers for this shop, does this mean that i would need a (8,3) array?
They have the corresponding part numbers for the Auto Center in one column and 3 other brands with selections for them to choose from.
timothybard 12 Posting Whiz
With an (8,3) array (nine "rows" by 3 "columns"), I would imagine that you have 8 items and that each item has three fields (Auto Center, Brand 1, Brand 2 and Brand 4)
Please note that my use of "rows" and "columns" above is purely to visualize the array. There are no set rows or columns in arrays.
cassie_sanford 1 Junior Poster in Training
I'm having a hard time trying to declare the right variables for my program.
Structure partSale
Dim typeString As String
Dim brandString As String
Dim brandTypeDecimal As Decimal
End Structure
Private ???????? As PartSale
Private ???????????
Private partNumber ( , ) As String = {{MR43T(STRING), RBL85 (STRING), 14K225(STRING)}} AND SO ON....
Private SelectedButtonString As String
there is no set number of part lookups, so im having problems makieng up a withMe statement...can anyone help?
timothybard 12 Posting Whiz
I'm not sure what you are trying to do. You have a structure set up, but then you have an array of strings. How are the strings supposed to represent and how does the PartSale fit into everything?
cassie_sanford 1 Junior Poster in Training
That's what i'm having trouble with...the vlaues that i'm stroring are like MR43T....plus i'm having trouble making sense of what i'm supposed to be doing. Since the user has to enter the part number and base it upon the the autoshop's list, here's what I came up with...i know its not right, but i need some kind of direction....
'Declare structure and module-level variables
Structure PartsSale
Dim partString As String
Dim brandString As String
Dim partNumberString As String
End Structure
Private autoPartSale(30) As PartsSale
Private numberAutoPartInteger As Integer
Private partNumberString(,) As String = _
{{"MR43T", "RBL8", "14K22"}, {"R43", "RJ6", "14K24"}, {"R43N", "RN4", "14K30"}, {"R46N", "RN8", "14K32"}, {"R46TS", "RBL17Y", "14K33"}, {"R46TX", "RBL12-6", "14K35"}, {"S46", "J11", "14K38"}, {"SR46E", "XEJ8", "14K40"}, {"47L", "H12", "14K44"}}
Private selectedButtonString As String
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
'Terminate the project.
Dim responseDialogResult As System.Windows.Forms.DialogResult
responseDialogResult = MessageBox.Show("Print the report?", "Terminate the Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If responseDialogResult = System.Windows.Forms.DialogResult.Yes Then
printButton_Click(sender, e)
End If
End Sub
Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printButton.Click
'Print the report using Print Preview.
With Me
.PrintPreviewDialog1.Document = .PrintDocument1
.PrintPreviewDialog1.ShowDialog()
End With
End Sub
Private Sub brandARadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brandARadioButton.CheckedChanged
'Save the name of the selected radio button.
'This procedure is executed each time any radio button is selected.
selectedButtonString = CType(sender, RadioButton).Name
End Sub
Private Sub brandCRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brandCRadioButton.CheckedChanged
'Save the name of the selected radio button.
'This procedure is executed each time any radio button is selected.
selectedButtonString = CType(sender, RadioButton).Name
End Sub
Private Sub brandXRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brandXRadioButton.CheckedChanged
'Save the name of the selected radio button.
'This procedure is executed each time any radio button is selected.
selectedButtonString = CType(sender, RadioButton).Name
End Sub
Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
'Clear the price.
'Select first radio button.
brandARadioButton.Select()
'Clear the part number selection.
partnumTextBox.SelectedText = -1
correspondingPartNumberTextBox.Clear()
End Sub
Private Sub findPartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles findPartButton.Click
'Look up the part using the brand and type
Dim rowString, columnString As String
Dim foundPartNumberString As String
With Me
'Allow only 30 transactions.
If numberAutoPartInteger < 30 Then
columnString = .partnumTextBox.SelectedText
If columnString <> -1 Then
'Part selection made, determine brand selected.
Select Case selectedButtonString
Case "brandARadioButton"
rowString = 0
autoPartSale(numberAutoPartInteger).brandString = " Brand A"
Case "brandCRadioButton"
rowString = 1
autoPartSale(numberAutoPartInteger).brandString = "Brand C"
Case "brandXRadioButton"
rowString = 2
autoPartSale(numberAutoPartInteger).brandString = "Brand X"
Case Else
'No selection made; use brand a.
rowString = 0
autoPartSale(numberAutoPartInteger).brandString = "Brand A"
End Select
'Retrieve part number of selection.
foundPartNumberString = partNumberString(rowString, columnString)
correspondingPartNumberTextBox.Text = foundPartNumberString.ToString()
'Save the selections.
autoPartSale(numberAutoPartInteger).partString = .partnumTextBox.Text
autoPartSale(numberAutoPartInteger).partNumberString = foundPartNumberString
numberAutoPartInteger += 1
Else
MessageBox.Show("Select the part.", "Selection Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show("Sorry, only 30 transactions allowed.")
End If
End With
End Sub
End Class
cassie_sanford 1 Junior Poster in Training
As earlier, i'm having a harad time picking the right names for my variables...that's probably what's throwing both of us off
timothybard 12 Posting Whiz
After copying your code, creating a form and creating the controls that the code refers to, I had to change the following lines
columnString = .partnumTextBox.SelectedText
to
columnString = .partnumTextBox.SelectedIndex
partnumTextBox.SelectedText = -1
to
partnumTextBox.SelectedIndex = -1
foundPartNumberString = partNumberString(rowString, columnString)
to
foundPartNumberString = partNumberString(columnString, rowString)
Everything seemed to work okay after that. On my form, partNumTextBox is a combobox and have 9 items to correspond to the partNumberString.
cassie_sanford 1 Junior Poster in Training
Okay i see where you're coming from now...as far as printing my slections out, i got this error saying that warning me about this particular line of code:
'Right align the corresponding part number.
formattedPartString = individualPartSale.partNumberString.To String()
'Measure the string in this font.
fontSizeF = e.Graphics........
It was mainly about the formattedPartString generating my selections in print preview.
timothybard 12 Posting Whiz
Where in the code is that line? I don't see a individualPartSale object reference in the code.
cassie_sanford 1 Junior Poster in Training
Dim printFont As New Font("Arial", 12)
Dim headingFont As New Font("Arial", 14, FontStyle.Bold)
Dim lineHeightSingle As Single = printFont.GetHeight + 2
Dim column1HorizontalLocationSingle As Single = e.MarginBounds.Left
Dim verticalPrintLocationSingle As Single = e.MarginBounds.Top
Dim column2HorizontalLocationSingle As Single = 300
Dim column3HorizontalLocationSingle As Single
Dim printLineString As String
Dim fontSizeF As New SizeF
Dim formattedPartString As String
'Set up and display heading lines.
printLineString = "Orange County Auto Center"
e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, column2HorizontalLocationSingle, verticalPrintLocationSingle)
printLineString = "by Cassie Sanford"
verticalPrintLocationSingle += lineHeightSingle
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, column2HorizontalLocationSingle, verticalPrintLocationSingle)
verticalPrintLocationSingle += lineHeightSingle * 2
'Loop through the transaction.
For Each individualPartSale As PartsSale In autoPartSale
'Dont print if blank.
If individualPartSale.brandString <> " " Then
'Set up a line.
'Brand.
e.Graphics.DrawString(individualPartSale.brandString, printFont, Brushes.Black, column1HorizontalLocationSingle, verticalPrintLocationSingle)
'Part Number
e.Graphics.DrawString(individualPartSale.partString, printFont, Brushes.Black, column2HorizontalLocationSingle, verticalPrintLocationSingle)
'Right align the corresponding part number.
formattedPartString = individualPartSale.partNumberString.ToString(" ")
'Measure string in this font.
fontSizeF = e.Graphics.MeasureString(formattedPartString, printFont)
'Subtract width of string from column position
column3HorizontalLocationSingle = 550 - fontSizeF.Width
e.Graphics.DrawString(formattedPartString, printFont, Brushes.Black, column3HorizontalLocationSingle, verticalPrintLocationSingle)
'Increment the Y position fot the next line; double space.
verticalPrintLocationSingle += lineHeightSingle * 2
End If
Next
End Sub
End Class
timothybard 12 Posting Whiz
Which event is that in?
cassie_sanford 1 Junior Poster in Training
in printing the form
timothybard 12 Posting Whiz
Can you tell me which object and which event the code is in? Since you are using code such as e.MarginBounds.Left, I need to know which object and which event the code is in. What is the name of the Sub?
cassie_sanford 1 Junior Poster in Training
Public Class Form1
'Declare structure and module-level variables
Structure PartsSale
Dim partString As String
Dim brandString As String
Dim partNumberString As String
End Structure
Private autoPartSale(30) As PartsSale
Private numberAutoPartInteger As Integer
Private partNumberString(,) As String = _
{{"MR43T", "RBL8", "14K22"}, {"R43", "RJ6", "14K24"}, {"R43N", "RN4", "14K30"}, {"R46N", "RN8", "14K32"}, {"R46TS", "RBL17Y", "14K33"}, {"R46TX", "RBL12-6", "14K35"}, {"S46", "J11", "14K38"}, {"SR46E", "XEJ8", "14K40"}, {"47L", "H12", "14K44"}}
Private selectedButtonString As String
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
'Terminate the project.
Dim responseDialogResult As System.Windows.Forms.DialogResult
responseDialogResult = MessageBox.Show("Print the report?", "Terminate the Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If responseDialogResult = System.Windows.Forms.DialogResult.Yes Then
printButton_Click(sender, e)
End If
End Sub
Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printButton.Click
'Print the report using Print Preview.
With Me
.PrintPreviewDialog1.Document = .PrintDocument1
.PrintPreviewDialog1.ShowDialog()
End With
End Sub
Private Sub brandARadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brandARadioButton.CheckedChanged
'Save the name of the selected radio button.
'This procedure is executed each time any radio button is selected.
selectedButtonString = CType(sender, RadioButton).Name
End Sub
Private Sub brandCRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brandCRadioButton.CheckedChanged
'Save the name of the selected radio button.
'This procedure is executed each time any radio button is selected.
selectedButtonString = CType(sender, RadioButton).Name
End Sub
Private Sub brandXRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brandXRadioButton.CheckedChanged
'Save the name of the selected radio button.
'This procedure is executed each time any radio button is selected.
selectedButtonString = CType(sender, RadioButton).Name
End Sub
Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
'Clear the price.
'Select first radio button.
brandARadioButton.Select()
'Clear the part number selection.
partNumComboBox.SelectedIndex = -1
correspondingPartNumberTextBox.Clear()
End Sub
Private Sub findPartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles findPartButton.Click
'Look up the part using the brand and type
Dim rowString, columnString As String
Dim foundPartNumberString As String
With Me
'Allow only 30 transactions.
If numberAutoPartInteger < 30 Then
columnString = partNumComboBox.SelectedIndex
If columnString <> -1 Then
'Part selection made, determine brand selected.
Select Case selectedButtonString
Case "brandARadioButton"
rowString = 0
autoPartSale(numberAutoPartInteger).brandString = " Brand A"
Case "brandCRadioButton"
rowString = 1
autoPartSale(numberAutoPartInteger).brandString = "Brand C"
Case "brandXRadioButton"
rowString = 2
autoPartSale(numberAutoPartInteger).brandString = "Brand X"
Case Else
'No selection made; use brand a.
rowString = 0
autoPartSale(numberAutoPartInteger).brandString = "Brand A"
End Select
'Retrieve part number of selection.
foundPartNumberString = partNumberString(columnString, rowString)
correspondingPartNumberTextBox.Text = foundPartNumberString.ToString()
'Save the selections.
autoPartSale(numberAutoPartInteger).partString = .partNumComboBox.Text
autoPartSale(numberAutoPartInteger).partNumberString = foundPartNumberString
numberAutoPartInteger += 1
Else
MessageBox.Show("Select the part.", "Selection Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show("Sorry, only 30 transactions allowed.")
End If
End With
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Handle print and print previews.
Dim printFont As New Font("Arial", 12)
Dim headingFont As New Font("Arial", 14, FontStyle.Bold)
Dim lineHeightSingle As Single = printFont.GetHeight + 2
Dim column1HorizontalLocationSingle As Single = e.MarginBounds.Left
Dim verticalPrintLocationSingle As Single = e.MarginBounds.Top
Dim column2HorizontalLocationSingle As Single = 300
Dim column3HorizontalLocationSingle As Single
Dim printLineString As String
Dim fontSizeF As New SizeF
Dim formattedPartString As String
'Set up and display heading lines.
printLineString = "Orange County Auto Center"
e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, column2HorizontalLocationSingle, verticalPrintLocationSingle)
printLineString = "by Cassie Sanford"
verticalPrintLocationSingle += lineHeightSingle
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, column2HorizontalLocationSingle, verticalPrintLocationSingle)
verticalPrintLocationSingle += lineHeightSingle * 2
'Loop through the transaction.
For Each individualPartSale As PartsSale In autoPartSale
'Dont print if blank.
If individualPartSale.brandString <> " " Then
'Set up a line.
'Brand.
e.Graphics.DrawString(individualPartSale.brandString, printFont, Brushes.Black, column1HorizontalLocationSingle, verticalPrintLocationSingle)
'Part Number
e.Graphics.DrawString(individualPartSale.partString, printFont, Brushes.Black, column2HorizontalLocationSingle, verticalPrintLocationSingle)
'Right align the corresponding part number.
formattedPartString = individualPartSale.partNumberString.ToString(" ")
'Measure string in this font.
fontSizeF = e.Graphics.MeasureString(formattedPartString, printFont)
'Subtract width of string from column position
column3HorizontalLocationSingle = 550 - fontSizeF.Width
e.Graphics.DrawString(formattedPartString, printFont, Brushes.Black, column3HorizontalLocationSingle, verticalPrintLocationSingle)
'Increment the Y position fot the next line; double space.
verticalPrintLocationSingle += lineHeightSingle * 2
End If
Next
End Sub
End Class
timothybard 12 Posting Whiz
Okay, that helps a lot.
Replace this line:
formattedPartString = individualPartSale.partNumberString.ToString(" ")
with this:
formattedPartString = individualPartSale.partNumberString
I don't know what you were attempting to do with .toString (" "), but toString is not a property or method of strings, which was giving you the problem.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.