-
Apr 12th, 2011, 12:54 AM
#1
Thread Starter
New Member
[RESOLVED] Creating an app using arrays
First, I'm sorry if this is in the wrong place: I'm new here
I needed to create an app using arrays. The goal of the app was to input a certain amount of points and display their level of contribution and any benefits they receive (for example: enter 500 points -> display "Gold level" and "Fieldhouse privileges, Parking at game, Football tickets"). I was supposed to use a loop and intLevel array to find the donor level that corresponds to the input amount, display the donor level using the strLevel array and using the strBen array display the benefits that apply to this donor.
What I was given:
Code:
Dim intLevel() As Integer = {50, 100, 200, 500, 1000}
Dim strLevel() As String = {"Contributor", "Laker Club", "Bronze", "Silver", "Gold"}
Dim strBen() As String = {" ", "f", "fp", "ft", "ftp"}
Dim i As Integer, inputAmt As Integer
What I did:
Code:
Dim intLevel() As Integer = {50, 100, 200, 500, 1000, 2000}
Dim strLevel() As String = {"Contributor", "Laker Club", "Bronze", "Silver", "Gold"}
Dim strBen() As String = {"None", "Fieldhouse Privileges", "Fieldhouse Privileges" & ControlChars.Newline & "Parking at game", "Fieldhouse Privileges" & ControlChars.NewLine & "Football tickets", "Fieldhouse Privileges" & ControlChars.Newline & "Parking at game" & ControlChars.NewLine & "Football tickets"}
Dim i As Integer, inputAmt As Integer
Integer.TryParse(txtContribution.Text, inputAmt)
Do Until i = strLevel.Length OrElse inputAmt < intLevel(i + 1)
i = i + 1
Loop
Do Until i = strBen.Length OrElse inputAmt < intLevel(i + 1)
i = i + 1
Loop
lblLevel.Text = strLevel(i)
lblBenefits.Text = strBen(i)
My app works, but my prof's comments on it were to get rid of the "+ 1" on the first "Do Until" loop, cross off the second Do Until loop and he just wrote "not quite" next to the bottom two lines.
So basically, I'm wondering how I would write code to search for the amount of points (without having to add the "2000" so it doesn't blow up on me) and matching it with strBen. To be honest, I don't understand why I am not getting points for this. The app works Anyways, if you have any input let me know! Thanks.
-
Apr 12th, 2011, 05:09 AM
#2
Re: Creating an app using arrays
Welcome to VBForums
Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum
-
Apr 12th, 2011, 07:48 AM
#3
Re: Creating an app using arrays
Originally Posted by pinkpostette
The goal of the app was to input a certain amount of points and display their level of contribution and any benefits they receive (for example: enter 500 points -> display "Gold level" and "Fieldhouse privileges, Parking at game, Football tickets").
In the arrays, it appears that Gold = 1000 and 500 = Silver. That would sound like "not quite" to me.
How about a For loop? It seems most appropriate here. Loop through until you find a match, then store the loop vaiable. You do not need to loop more than once. Then you can access multiple arrays with the same index if the data in the arrays is correct. If you have 50 points, then the index will be 0. you can then get strLevel(0) and strBen(0) without using another loop.
VB6 Library
If I helped you then please help me and rate my post!
If you solved your problem, then please mark the post resolved
-
Apr 12th, 2011, 08:07 AM
#4
Thread Starter
New Member
Re: Creating an app using arrays
Getting rid of the 2nd Do Until loop definitely works fine, thanks :P Didn't need that.
I still don't know why he scratched off the "+ 1" from the first one though... I couldn't get things to appear right without it.
And sorry, I realize I wrote the example out wrong in the first part, but it is supposed to be gold = 1000 and silver = 500
-
Apr 12th, 2011, 08:22 AM
#5
Re: Creating an app using arrays
Dim intLevel() As Integer = {50, 100, 200, 500, 1000}
That is not Classic VB code, which is the topic of this forum
-
Apr 12th, 2011, 08:32 AM
#6
Thread Starter
New Member
Re: Creating an app using arrays
Okay, I think I got it to work the way he wanted now (for the most part). This is what I did:
Code:
Dim intLevel() As Integer = {50, 100, 200, 500, 1000, 2000}
Dim strLevel() As String = {"Contributor", "Laker club", "Bronze", "Silver", "Gold"}
Dim strBen() As String = {"None", "Fieldhouse Privileges", "Fieldhouse Privileges" & ControlChars.NewLine & "Parking at game", "Fieldhouse Privileges" & ControlChars.NewLine & "Football Tickets", "Fieldhouse Privileges" & ControlChars.NewLine & "Parking at game" & ControlChars.NewLine & "Football Tickets"}
Dim i As Integer, inputAmt As Integer
Integer.TryParse(txtContribution.Text, inputAmt)
Do Until i = intLevel.Length OrElse inputAmt < intLevel(i)
i = i + 1
Loop
lblLevel.Text = strLevel(i - 1)
lblBenefits.Text = strBen(i - 1)
Still, in the beginning code he game us, he wanted strBen = {"","f","fp",...} so on and then for each letter to match up to a Benefit label (lblBenefit1, lblBenefit2, lblBenefit3) and then depending on which came up, display the appropriate benefits. What I tried was:
Code:
Dim intLevel() As Integer = {50, 100, 200, 500, 1000, 2000}
Dim strLevel() As String = {"Contributor", "Laker club", "Bronze", "Silver", "Gold"}
Dim strBen() As String = {" ", "f", "fp", "ft", "ftp"}
Dim i As Integer, inputAmt As Integer
Dim f As Integer, p As Integer, t As Integer
Integer.TryParse(txtContribution.Text, inputAmt)
Do Until i = intLevel.Length OrElse inputAmt < intLevel(i)
i = i + 1
Loop
lblLevel.Text = strLevel(i - 1)
If strBen.Contains("f") Then
lblBenefit1.Text = "Fieldhouse Privileges"
ElseIf strBen.Contains("fp") Then
lblBenefit1.Text = "Fieldhouse Privileges"
lblBenefit2.Text = "Parking at game"
ElseIf strBen.Contains("ft") Then
lblBenefit1.Text = "Fieldhouse Privileges"
lblBenefit3.Text = "Football tickets"
ElseIf strBen.Contains("ftp") Then
lblBenefit1.Text = "Fieldhouse Privileges"
lblBenefit2.Text = "Parking at game"
lblBenefit3.Text = "Football tickets"
Else
lblBenefit1.Text = "None"
End If
End Sub
But all that's doing is having "Fieldhouse privileges" appear with "ft" or whichever is chosen also show up.
-
Apr 12th, 2011, 08:32 AM
#7
Thread Starter
New Member
Re: Creating an app using arrays
Originally Posted by VBClassicRocks
That is not Classic VB code, which is the topic of this forum
I already got moved, I assumed this is where I was supposed to be Sorry.
-
Apr 12th, 2011, 08:49 AM
#8
Re: Creating an app using arrays
A couple points:
1) Since you don't check the return from TryParse, if an invalid value is entered into the textbox, you won't know about it. Instead, your variable will just contain 0, and the code will continue. If that is not the way you want it, then check the return and exit if TryParse returns false.
2) You are checking if the array contains those strings. Of course it does. You put those strings into the array. That whole portion is not what you want, but see the next one.
3) I agree with MarMan that this would make more sense as a For Next rather than a Do loop. Are you required to use a Do loop? If so, then i is the index into all the arrays (or i-1, I suppose, but see item 4). Therefore, if i = 0 then you want the Contributor level. If i = 1 then you want the next level, and so on. You could use a select case for that, or If Else, but what you are looking at is what is i. The values in the strBen array don't appear to make any difference at all. You could force them to make a difference, but it would be a waste of effort. The key is i, not the item in strBen.
4) If the user enters 25, then the loop will find that InputAmount < IntLevel(0), so the loop will not run even once. Then at the next line you will be indexing array position i-1, but i will be 0, so i-1 is -1. That will cause an exception, as there is no array index -1. You need to validate the input to make sure that it is never less than 50.
My usual boring signature: Nothing
-
Apr 12th, 2011, 05:15 PM
#9
Thread Starter
New Member
Re: [RESOLVED] Creating an app using arrays
Thanks everyone. Apparently the second time around he just wanted us to demo it, and it worked fine in the first place so I was all set.
Thank you again, though, I will use this help for future projects
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
|