|
-
Jan 17th, 2015, 06:58 PM
#1
Thread Starter
New Member
Search a 2D array for a value <=
I am working on an application that calculates a shipping price based on the amount ordered. I have a 2D array (or so I believe) that I am searching, however it only yields any results when I search for an exact number. I would like to set it so when I search a value between 1 and 10 I get a $15 for shipping. Right now I have to enter 1 to receive the $15 shipping, 11 for $10, and so on. Below is what I have so far.
Code:
Public Class frmMain
Private strItems(,) As String = {{"1", "15"},
{"11", "10"},
{"51", "5"},
{"101", "0"}}
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtordered_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtOrdered.KeyPress
' allows the text box to accept numbers and the Backspace key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub txtordered_TextChanged(sender As Object, e As EventArgs) Handles txtOrdered.TextChanged
lblShipping.Text = String.Empty
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim strSearchForId As String
Dim intRow As Integer
strSearchForId = txtOrdered.Text
Do Until intRow > strItems.GetUpperBound(0) OrElse
strSearchForId = strItems(intRow, 0)
intRow += 1
Loop
If intRow <= strItems.GetUpperBound(0) Then
Dim intPrice As Integer
Integer.TryParse(strItems(intRow, 1), intPrice)
lblShipping.Text = intPrice.ToString("C0")
End If
End Sub
End Class
I have tried playing with the columns and rows to no avail. Some assistance would be greatly appreciated.
I am using a textbox to enter in my search value and a label to display the shipping cost.
-
Jan 17th, 2015, 07:07 PM
#2
Re: Search a 2D array for a value <=
You do have a 2D array, but you almost certainly would be better off without one. So, why do you have a 2D array? Is there some requirement that you have one?
One issue is that the values are clearly numbers, and you are already fighting with that. For one thing, since the array is strings, you always have to convert to integer for comparisons, which is inefficient. For another, you are using a Textbox for entry and have to add that code to restrict it to numbers. That isn't really safe, though, because copy and paste will bypass what you have done, since the KeyPress event won't fire. Better would be to use a NumericUpDown control that already only allows numbers, and returns a number (though a Decimal) from the .Value property.
A 2D array is ok if you will never be changing the size. If you are changing the size, you would probably be better off making a small class or structure with two members, then creating a sorted list of those objects. The two members would be something like Threshold and Value. Another option would be to use a SortedDictionary, which would be a bit easier. What you'd have to do is iterate through the keys, and as soon as you find one that is higher than the value you are seeking, you'd take the value associated with that key.
My usual boring signature: Nothing
 
-
Jan 17th, 2015, 07:26 PM
#3
Thread Starter
New Member
Re: Search a 2D array for a value <=
I have been hearing that a lot, that 2D arrays are becoming less and less used. Its for my advanced CIS class. The book touches on multidimensional array but does not cover one quite like this. Thank you for the lead however I will try and see what I can do.
-
Jan 17th, 2015, 08:56 PM
#4
Re: Search a 2D array for a value <=
There are drawbacks to multi-D arrays, especially these days, but somewhat in earlier times. The main problem is that they are really touch to change the size on. VB only allowed resizing the final dimension of the array. If you wanted to resize any other dimension you had to do it yourself by creating a new array of the new size, and copying over data from the old array to the new array as you saw fit. Not totally horrible, but not so easy, either.
Now, with the introduction of generics in 2005, you have a bunch of collections that are quick, easy, and quite efficient, to resize, such as the List(of T). However, the List(of T) is only one dimension and that's all you can have. It is so quick, easy, and efficient, though, that it is often easier to create some custom class or structure and just maintain a List of those. Furthermore, there are lots of cases where that is the right thing to do, because the 2D array doesn't really capture the concept you are trying to capture. A 2D array makes a lot of sense if you have a table, though the Datatable might be better for such a thing. In this case, you don't really have a 2D array so much as you have a set of item pairs. The pairs consist of a threshold and a value. If X gets above the threshold, then use the value associated with it. Conceptually, that sounds more like an object that has the threshold and a value, to me, rather than a 2D grid of values.
The SortedDictionary would just be a shortcut to the same result. Normally, dictionaries are using the key to look up the value, but there is no reason not to use them as a key associated with a value, and if they are sorted, they'd serve the purpose you need. After all, you need that threshold to be sorted, so sorted keys would do just as well.
My usual boring signature: Nothing
 
-
Jan 17th, 2015, 09:12 PM
#5
Thread Starter
New Member
Re: Search a 2D array for a value <=
Got it figured out tho. I hope I don't have to use these but I got it working.
Code:
Private items As String(,) = {{"1", "15"},
{"11", "10"},
{"51", "5"},
{"101", "0"}}
Private Sub Amount_TxtBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles Amount_TxtBox.TextChanged
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub GetShipping_But_Click(sender As System.Object, e As System.EventArgs) Handles CalcTotal_But.Click
Dim itemCost As Integer
Dim shippingCost As Integer
If Not Integer.TryParse(Me.Amount_TxtBox.Text, itemCost) Then
MsgBox("Please enter a valid amount!", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error in item cost")
Exit Sub
End If
shippingCost = GetShipping(itemCost)
Me.ItemShipping_Lbl.Text = shippingCost.ToString()
Me.TotalCost_TxtBox.Text = CalcTotal(itemCost, shippingCost).ToString()
End Sub
Private Function GetShipping(ByVal itemAmount As Integer) As Integer
Dim amount As Integer = 0
Integer.TryParse(Me.Amount_TxtBox.Text, amount)
For item As Integer = 0 To (items.GetUpperBound(0)) Step +1
If (items.GetLength(0)) <= (item + 1) Then
Return CInt(items(items.GetLength(0) - 1, 1))
End If
If amount >= CInt(items(item, 0)) And amount < CInt(items(item + 1, 0)) Then
Return CInt(items(item, 1))
End If
Next
Return 0
End Function
Private Function CalcTotal(ByVal itemCost As Integer, ByVal shippingCost As Integer) As Integer
Return itemCost + shippingCost
End Function
End Class
Had to make classes. But still kept the 2D array so this should be acceptable.
Thank you for all the information, I really can't learn enough.
-
Jan 17th, 2015, 11:52 PM
#6
Re: Search a 2D array for a value <=
Nobody can. There is always more to learn for all of us.
My usual boring signature: Nothing
 
-
Jan 18th, 2015, 12:13 AM
#7
Thread Starter
New Member
Re: Search a 2D array for a value <=
I think that's exactly the pure essence of why I enjoy it.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|