First i would like to say hi i am new here and new to programming. im currently takeing an intor to progrmming class useing vb 2008 express edition. so basicly im a newbe. now as for my question. i had to write a program for class as a lab assignment The 12 days of christmas. i was able to write part of it but couldnt quite get it runnin rite. finaly my teacher sat down real fast and fixed my mistakes so it runs fine and i handed it in. the problem im havein is that the professor didnr explain to me wat i did wrong or why wat she did works so i have no idea whats what in the program. it basicly comes down to i dont understand Loops, For Next, or Arrays. if someone could explain to me wats goin on in the program and why it all works i think it would help me understand things finally. heres the code, runs perfectly and does exactly wat it is supposed to. -
Public Class frm12DaysOfChristmas
-
Dim itemName(12) As String
-
Dim itemCost(12) As Double
-
-
-
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
-
Dim InputDay As Integer
-
Dim CostOfDay As Double = 0
-
Dim TotalCost As Double = 0
-
InputDay = CInt(mtxtDayNumber.Text)
-
lstResults.Items.Clear()
-
If (CInt(InputDay) <= 0) Or (CInt(InputDay) >= 13) Then
-
MsgBox("Please enter a number from 1 to 12")
-
mtxtDayNumber.Clear()
-
mtxtDayNumber.Focus()
-
Else
-
For dayNum As Integer = 1 To InputDay
-
lstResults.Items.Add(itemName(dayNum))
-
CostOfDay = dayNum * itemCost(dayNum) + CostOfDay
-
Next
-
lstResults.Items.Add(FormatCurrency(CostOfDay))
-
CostOfDay = 0
-
'Grand Total
-
For iCount As Integer = 1 To 12
-
For dayNum As Integer = 1 To iCount
-
CostOfDay = dayNum * itemCost(dayNum) + CostOfDay
-
Next
-
Next
-
lstResults.Items.Add("Total Cost " & (FormatCurrency(CostOfDay)))
-
End If
-
End Sub
-
Private Sub frm12DaysOfChristmas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
itemName(1) = "1 Partridge in a pear tree"
-
itemName(2) = "2 Turtle doves"
-
itemName(3) = "3 French hens"
-
itemName(4) = "4 Calling birds"
-
itemName(5) = "5 Golden rings"
-
itemName(6) = "6 Geese a laying"
-
itemName(7) = "7 Swans a swimming"
-
itemName(8) = "8 Maids a milking"
-
itemName(9) = "9 Ladies dancing"
-
itemName(10) = "10 Lords a leaping"
-
itemName(11) = "11 Pipers piping"
-
itemName(12) = "12 Drummers drumming"
-
-
itemCost(1) = 164.99
-
itemCost(2) = (20.0)
-
itemCost(3) = (15.0)
-
itemCost(4) = (149.99)
-
itemCost(5) = (79.0)
-
itemCost(6) = (60.0)
-
itemCost(7) = (600.0)
-
itemCost(8) = (5.85)
-
itemCost(9) = (528.8)
-
itemCost(10) = (428.51)
-
itemCost(11) = (201.22)
-
itemCost(12) = (199.82)
-
End Sub
-
End Class
5 1886
anyone out their to offer help
PRR 750
Recognized Expert Contributor
Can you explain what your program does? Then maybe it will be easier to say something. Its difficult to read code and "get" the whole picture.
Frinavale 9,735
Recognized Expert Moderator Expert
I seriously dislike VB.NET for loops.
C# loops are so much nicer (the same loop syntax for C++, Java, JavaScript and other languages too)
Let's ignore your program for the time being and take a look at the following loop: -
For counter As Integer = 1 to 10
-
'do stuff
-
Next
This loop declares a variable "counter" as an Integer and sets it to 1.
It's a short hand version of: -
Dim counter As Integer = 1
-
For counter To 12
-
'do stuff
-
Next
-
The for loop will loop from 1 to 12 in this case and then end.
You can access what loop iteration you're at using the "counter" variable.
Say you had the following array of Strings: -
Dim myArrayOfTwelveNumbers(12) As String= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"}
-
'The above array has been declared to hold 13 strings that contain text for numbers 0 - 12
-
You can loop through this array and print each the text stored in each element by accessing the element at "counter" for each loop iteration: -
Dim myArrayOfTwelveNumbers(12) As String= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"}
-
For counter As Integer = 1 To 12
-
Console.WriteLine(myArrayOfTwelveNumbers(counter))
-
Next
-
This will print: -
one
-
two
-
three
-
four
-
five
-
six
-
seven
-
eight
-
nine
-
ten
-
eleven
-
twelve
Note that "zero" is not printed. That is because "zero" exists at the array index 0 and you are looping from 1 to 12.
If you only wanted to print "four","five","six" you'd start the loop at element 4 instead: -
Dim myArrayOfTwelveNumbers(12) As String= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"}
-
For counter As Integer = 4 To 6
-
Console.WriteLine(myArrayOfTwelveNumbers(counter))
-
Next
-
It's pretty simple....but I still hate them.
-Frinny
to prr, the prgram is supposed to request an integer from 1 through 12 and then list the gifts for that day along with that days cost. on the nth day, the n gifts are 1 partridge in a pear tree, 2 turtle doves,... n of the nth item. the prgram should also give the total cost of all twelve days.
PRR 750
Recognized Expert Contributor
I guess Frinavale has answered pretty much most of your queries. Adding to her ans, you can look into: Convert to int Switch statement Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Carolyn Gill |
last post by:
I have already created an asp login/database for a learning/quiz section on
a small site. There will be multiple quizzes through the site and what I
need now would be help: tutorials or advice that...
|
by: Susan Bricker |
last post by:
Greetings. Before I begin, I have been stuck on this problem for about a
5 days, now. I have tried and just seem to be not getting anywhere. I
know that the explanation is lengthy, but I am a...
|
by: Hazz |
last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender'
line of code need to be there in this event handler?
Isn't cboFont passed via the managed heap, not the stack, into this...
|
by: usl2222 |
last post by:
Hi folks,
I appreciate any assistance in the following problem:
I have a form with a bunch of dynamic controls on it. All the controls
are dynamically generated on a server, including all...
|
by: Susan Rice |
last post by:
I'm comparing characters via
return(str1 - str2);
and I'm having problems with 8-bit characters being treated as signed
instead of unsigned integers. The disassembly is using
movsx ...
| |
by: jacob navia |
last post by:
Hi
I am writing this tutorial stuff again in the holidays and I came across
this problem:
The "width" field in printf is a minimum width. Printf will not truncate
a field. for instance:...
|
by: dismantle |
last post by:
Hi all,
this is my 3rd week in studying VB codes and i came across with this codes
from a online tutorial about classes.
Public Function MiddleInitial() As String
MiddleInitial =...
|
by: adam_kroger |
last post by:
BRIEF EXPLANATION:
I have 6 TextBoxes named LIS1, LIS2, LIS3, ... LIS6. I want to be
able to reference them using a For/Next loop and either read ot write
to them. In VBA I would use something...
|
by: vikjohn |
last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified...
|
by: javabeginner123 |
last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |