|
-
Jun 30th, 2004, 07:26 AM
#1
Thread Starter
Addicted Member
4 Parallel Arrays
How do I create 4 parallel arrays using this table:
VB Code:
' Declare 4 parallel arrays for zones from this table:
' Weight Zone A Zone B Zone C Zone D
' 1 1.00 1.50 1.65 1.85
' 3 1.58 2.00 2.40 3.05
' 5 1.71 2.52 3.10 4.00
' 10 2.04 3.12 4.00 5.01
' >10 2.52 3.75 5.10 7.25
-
Jun 30th, 2004, 07:46 AM
#2
Assuming those values are CONSTANT, you can hard code them into your program. If you plan to change these values during you runtime then delete the red part of this code...
VB Code:
Dim table(,) As Single = New Single(4, 4) {[color=red]{1, 3, 5, 10, 10}, {!2nd column here!}, {!3rd column here!}, {!4th column here!}, {!5th column here!}[/color]}
I can't be bothered typing the values in but you get the point from the above code.
Experiment with the coordinates to make sure they are where they are where they should be, you may have to type the values in in row groups instead of column groups like above. I can't remember when I last initialised an array like this
Last edited by wossname; Jun 30th, 2004 at 07:54 AM.
I don't live here any more.
-
Jun 30th, 2004, 08:04 AM
#3
Thread Starter
Addicted Member
Is this not correct:
VB Code:
Dim dblCosts(4, 3) As Double
dblCosts(0, 0) = 1.0
dblCosts(0, 1) = 1.5
dblCosts(0, 2) = 1.65
dblCosts(0, 3) = 1.85
dblCosts(1, 0) = 1.58
dblCosts(1, 1) = 2.0
dblCosts(1, 2) = 2.4
dblCosts(1, 3) = 3.05
dblCosts(2, 0) = 1.71
dblCosts(2, 1) = 2.52
dblCosts(2, 2) = 3.1
dblCosts(2, 3) = 4.0
dblCosts(3, 0) = 2.04
dblCosts(3, 1) = 3.12
dblCosts(3, 2) = 4.0
dblCosts(3, 3) = 5.01
dblCosts(4, 0) = 2.52
dblCosts(4, 1) = 3.75
dblCosts(4, 2) = 5.1
dblCosts(4, 3) = 7.25
-
Jun 30th, 2004, 08:09 AM
#4
Retired VBF Adm1nistrator
That's basically what wossname posted
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jun 30th, 2004, 08:12 AM
#5
Thread Starter
Addicted Member
This might help:
1) I have 5 Radio buttons for Weight
1
3
5
10
>10
2) I have 4 Radio buttons for Zones
Zone A
Zone B
Zone C
Zone D
So users will select a radio button for Weight and one for the Zone and depending on which ones they select will determine the data pulled from the arrays.
Ex: Radio button 1 and Zone B selected
then MessageBox.Show("("The shipping rate is " & strValue, "Cost to Ship")
Where strValue will be the value from the arrays(which I also don't know how to set up a statement for) Please help!
-
Jun 30th, 2004, 08:13 AM
#6
Thread Starter
Addicted Member
Originally posted by plenderj
That's basically what wossname posted
Does it matter which way you populate it??
Is this still 4 parallel arrays or a 2D array??
-
Jun 30th, 2004, 09:10 AM
#7
There is no such thing (to my knowledge) as "parralel arrays" in VB.net. Not unless you write a class to do that yourself.
This is a 2 dimensional array.
dblCosts(4) would be a 1 dimensional array
dblCosts(4,4,4) would be a 3 dimensional array
dblCosts(4,4,4,4) would be a 4 dimensional array
I don't live here any more.
-
Jun 30th, 2004, 11:58 AM
#8
Thread Starter
Addicted Member
Thats what I was wondeting but my instructor said I needed to decalre 4 parallel arrays for zones. What the hell does she mean by that??
-
Jun 30th, 2004, 12:56 PM
#9
Originally posted by twisted
Thats what I was wondeting but my instructor said I needed to decalre 4 parallel arrays for zones. What the hell does she mean by that??
Why don't you ask her? That's what teachers are for.
IMO, you can't teach programming. You either teach yourself proper coding or someone teaches you their own style. Tutors rarely get time to actually DO any programming themselves, in the same way that Physics teachers seldom build lasers and aeroplanes. There is no susbstitute for getting stuck into a good programming book and learning stuff your own way. That's how I did it, and I reckon I'm not bad.
I don't live here any more.
-
Jun 30th, 2004, 01:13 PM
#10
Frenzied Member
My guess is she means 4 separate arrays for the zones, where the values at any particular index match the correct line in weight.
i.e.,
VB Code:
(1.0, 1.58, 1.71, 2.04, 2.52)
(1.50, 2.00, 2.52, 3.12, 3.75)
' etc
The values in the first index (index 0) are the values for weight 1, values in the second column are the values for weight 3, etc.
Then you could do something like loop through the arrays and get all the values for index(0), index(1), etc.
-
Jun 30th, 2004, 03:16 PM
#11
Fanatic Member
VB Code:
Dim zoneA ( 0 to 4 ) as whatever
Dim zoneB ( 0 to 4 ) as whatever
Dim zoneC ( 0 to 4 ) as whatever
Dim zoneD ( 0 to 4 ) as whatever
zoneA(0) = 1.00
zoneB(0) =1.5
zoneC(0) = 1.65
zoneD(0) = 1.85
...
That is probably what she meant by parallel arrays.
-
Jun 30th, 2004, 04:08 PM
#12
PowerPoster
Hi,
As mentioned in other posts, Parallel Arrays is NOT a term which has any technical meaning in VB.NET. Therefore we have to assume your instructor is using the term as an ordinary english expression. If she said "Four parallel arrays" for the zones from the table, then she probably means four single dimension arrays, as salvelinus posted but where each element (in this case row) number refers to the item Weight. (If you assume that element (0) referred to the weight 1 - which would be a more efficient use of the arrays - then she would have asked for five parallel arrays)Therefore several elements will be empty. For example in
VB Code:
Dim arrA(10), arrB(10), arrC(10), arrD(10) as Double
elements 0, 2, 4, 6, 7, 8, 9 of all the arrays will be zero
arrA(1) will contain 1.00
arrA(3) ,, ,, 1.58
arrB(1) ,, ,, 1.50
arrB(3) ,, ,, 2.00
etc.
Last edited by taxes; Jun 30th, 2004 at 04:13 PM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jun 30th, 2004, 05:45 PM
#13
Frenzied Member
I wasn't using the Weight column as an array, or the numbers in Weight as an index. The original question mentioned 4 arrays for the zone data. If you included Weight as an array, there would be 5. But nothing in the question says weight should be in the parallel array (true, not a VB term, but it sounds familiar from VB 5 schoolwork).
Taxes, I don't get why you have a 10 element array. I only see 5 elements in each array.
-
Jun 30th, 2004, 06:11 PM
#14
Thread Starter
Addicted Member
Originally posted by salvelinus
My guess is she means 4 separate arrays for the zones, where the values at any particular index match the correct line in weight.
i.e.,
VB Code:
(1.0, 1.58, 1.71, 2.04, 2.52)
(1.50, 2.00, 2.52, 3.12, 3.75)
' etc
The values in the first index (index 0) are the values for weight 1, values in the second column are the values for weight 3, etc.
Then you could do something like loop through the arrays and get all the values for index(0), index(1), etc.
This is what she wants I asked her. Thanks!
Then I can loop through the arrays..HAHA do you know the kinda VB newb I am. You make it sound so easy. I will definitely try to write a statement to do so though. Think I should use a For...Next statement??
-
Jun 30th, 2004, 06:49 PM
#15
PowerPoster
Originally posted by salvelinus
Taxes, I don't get why you have a 10 element array. I only see 5 elements in each array.
I would have thought that keeping the Weight figure equal to the index of the array was the logical approach . That would make locating the required values much easier, e.g. if you want the values for Weight 5:
Pseudo Code
Dim sTemp As String
Dim iCount as Integer=5
sTemp=str(arrA(iCount)) & " " & str(arrB(iCount))& " " & str(arrC(iCount)) & " " & str(arrD(iCount))
I also assumed that the number of Weight figures might be increased.
BUT it seems from twisted's last post that you are right and I am wrong. I was looking at it with commercial practicability in mind
and, bearing in mind twisted's previous threads, was trying to anticipate future questions on the subject.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jun 30th, 2004, 08:59 PM
#16
Frenzied Member
Yes, that would be logical, but only if the Weight value increased in regular increments. If the weight increases by an irregular number, you couldn't correlate the weight value with the index.
-
Jul 1st, 2004, 08:42 AM
#17
Thread Starter
Addicted Member
Originally posted by salvelinus
My guess is she means 4 separate arrays for the zones, where the values at any particular index match the correct line in weight.
i.e.,
VB Code:
(1.0, 1.58, 1.71, 2.04, 2.52)
(1.50, 2.00, 2.52, 3.12, 3.75)
' etc
The values in the first index (index 0) are the values for weight 1, values in the second column are the values for weight 3, etc.
Then you could do something like loop through the arrays and get all the values for index(0), index(1), etc.
How do I set it up this way?
Dim dblCosts() As Double = (1.0, 1.58, 1.71, 2.04, 2.52)
Like this?? Or this:
VB Code:
Dim table(,) As Single = New Single(3, 4) {{1.0, 1.58, 1.71, 2.04, 2.52}, {1.5, 2.0, 2.52, 3.12, 3.75}, {1.65, 2.4, 3.1, 4.0, 5.1}, {1.85, 3.05, 4.0, 5.01, 7.25}}
Last edited by twisted; Jul 1st, 2004 at 08:57 AM.
Twisted
-
Jul 1st, 2004, 09:01 AM
#18
Thread Starter
Addicted Member
How would I pull info from my arrays using two radio buttons??
If rad1.Checked And rad2.Checked Then
strValue = "1.00"
MessageBox.Show("The shipping value is " &strValue, "Cost")
This would be a lot of if then statements...
Any other way this can be done.
User select a Weight (5 radio buttons) and selects a Zone ( 4 radio buttons) and then it should be the value in the table
IM LOST!!
-
Jul 1st, 2004, 09:02 AM
#19
Frenzied Member
If she wants 4 parallel arrays, dim 4 separate arrays like in your first example. She's not asking for a multidimensional array.
I don't see how you could select more than 4 separate values with 2 radio buttons (both checked, neither checked, first only checked, second only checked, and this certainly wouldn't be intuitive to a user).
I'd use two comboxes, one to select the weight, one to select the zone. Using radio buttons, you'd have to have one for each row and one for each column.
Last edited by salvelinus; Jul 1st, 2004 at 09:07 AM.
-
Jul 1st, 2004, 09:06 AM
#20
Thread Starter
Addicted Member
Thanks!
VB Code:
Private Sub btnRate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRate.Click
Dim intRow, intColumn As Integer
Dim dblArray1() As Double = {1.0, 1.58, 1.71, 2.04, 2.52}
Dim dblArray2() As Double = {1.5, 2.0, 2.52, 3.12, 3.75}
Dim dblArray3() As Double = {1.65, 2.4, 3.1, 4.0, 5.1}
Dim dblArray4() As Double = {1.85, 3.05, 4.0, 5.01, 7.25}
Dim strValue As String
MessageBox.Show("The shipping rate is " & strValue, "Cost to Ship")
End Sub
End Class
Last edited by twisted; Jul 1st, 2004 at 09:11 AM.
Twisted
-
Jul 1st, 2004, 10:00 AM
#21
PowerPoster
Originally posted by twisted
How would I pull info from my arrays using two radio buttons??
If rad1.Checked And rad2.Checked Then
strValue = "1.00"
MessageBox.Show("The shipping value is " &strValue, "Cost")
This would be a lot of if then statements...
Any other way this can be done.
User select a Weight (5 radio buttons) and selects a Zone ( 4 radio buttons) and then it should be the value in the table
IM LOST!!
Hi, what is your instructor actually asking for? Two buttons or nine?
How does she want the choice of weight and zone to be made - through input into a text box or by selection through a button, listview or combo?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 1st, 2004, 10:58 AM
#22
Thread Starter
Addicted Member
-
Jul 1st, 2004, 04:19 PM
#23
Frenzied Member
OK, you've got two group boxes. One, Weight has five radio buttons, and the other, Zone, has 4. Then you have a button that shows a message. And you have to have 4 parallel arrays to hold the Zone data.
For example:
Call your Weight radio buttons rbWeight0, rbWeight1, etc. Call your Zone radio buttons rbZone0, rbZone1, etc, and the arrays myArray0, myArray1, and so on. You could use other names, this just helps to make it clear what value you want later.
You can initialize your arrays in a couple places. You could declare them form level variables and initialize them in the Form_Load event. You could declare and initialize them in the Button1_Click event. You could initialize a/o declare them in a separate function that returns the arrays. Ideally this data would be in a database, but that's not the assignment.
The differences between declaring them at the form level or in an event or sub/function relate to performance issues. Form level variables will take up memory and are, potentially, a weak spot in your code (although they're certainly necessary and appropriate in some cases). But, you only deal with creating and initializing them once.
Using an event/sub/function to declare and initialize them may save on memory, but will result in more processing overhead, because they have to be declared, initialized, and disposed of each time. For this app, the difference is almost nonexistent, but if you were dealing with a large app, lots of db stuff, it might be an issue. Memory isn't supposed to be as big an issue in .Net, but doesn't hurt to code as efficiently as possible.
So anyway, somewhere you declare and initialize the arrays. Then, when the button is clicked, check which Weight button is checked and which Zone button is checked (and check to make sure at least one in each group is checked, or set one in each to checked as default). Then you can retrieve the correct array value.
Then, to check which value to retrieve, use code like:
VB Code:
If rbWeight2.Checked Then
If rbZone2.Checked then
Messagebox.Show(myArray(2, 2))
End If
If rbZone3.Checked Then
Mesagebox.Show(myarray(2, 3))
'etc
End If
There's more efficient ways to check, that's just to give you an idea.
[edit]
Now that I've switched browsers from IE to FireFox (Mozilla), why is the reply box so much smaller?
And why can't I get Airhead by Thomas Dolby out of my head? It's in my head for days anytime I play it.
Last edited by salvelinus; Jul 1st, 2004 at 06:01 PM.
-
Jul 2nd, 2004, 09:49 AM
#24
Thread Starter
Addicted Member
You are the man! Thanks for putting me on the right track.
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
|