473,473 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Need explanation

3 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. Public Class frm12DaysOfChristmas
  2.     Dim itemName(12) As String
  3.     Dim itemCost(12) As Double
  4.  
  5.  
  6.     Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
  7.         Dim InputDay As Integer
  8.         Dim CostOfDay As Double = 0
  9.         Dim TotalCost As Double = 0
  10.         InputDay = CInt(mtxtDayNumber.Text)
  11.         lstResults.Items.Clear()
  12.         If (CInt(InputDay) <= 0) Or (CInt(InputDay) >= 13) Then
  13.             MsgBox("Please enter a number from 1 to 12")
  14.             mtxtDayNumber.Clear()
  15.             mtxtDayNumber.Focus()
  16.         Else
  17.             For dayNum As Integer = 1 To InputDay
  18.                 lstResults.Items.Add(itemName(dayNum))
  19.                 CostOfDay = dayNum * itemCost(dayNum) + CostOfDay
  20.             Next
  21.             lstResults.Items.Add(FormatCurrency(CostOfDay))
  22.             CostOfDay = 0
  23.             'Grand Total
  24.             For iCount As Integer = 1 To 12
  25.                 For dayNum As Integer = 1 To iCount
  26.                     CostOfDay = dayNum * itemCost(dayNum) + CostOfDay
  27.                 Next
  28.             Next
  29.             lstResults.Items.Add("Total Cost " & (FormatCurrency(CostOfDay)))
  30.         End If
  31.     End Sub
  32.     Private Sub frm12DaysOfChristmas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  33.         itemName(1) = "1 Partridge in a pear tree"
  34.         itemName(2) = "2 Turtle doves"
  35.         itemName(3) = "3 French hens"
  36.         itemName(4) = "4 Calling birds"
  37.         itemName(5) = "5 Golden rings"
  38.         itemName(6) = "6 Geese a laying"
  39.         itemName(7) = "7 Swans a swimming"
  40.         itemName(8) = "8 Maids a milking"
  41.         itemName(9) = "9 Ladies dancing"
  42.         itemName(10) = "10 Lords a leaping"
  43.         itemName(11) = "11 Pipers piping"
  44.         itemName(12) = "12 Drummers drumming"
  45.  
  46.         itemCost(1) = 164.99
  47.         itemCost(2) = (20.0)
  48.         itemCost(3) = (15.0)
  49.         itemCost(4) = (149.99)
  50.         itemCost(5) = (79.0)
  51.         itemCost(6) = (60.0)
  52.         itemCost(7) = (600.0)
  53.         itemCost(8) = (5.85)
  54.         itemCost(9) = (528.8)
  55.         itemCost(10) = (428.51)
  56.         itemCost(11) = (201.22)
  57.         itemCost(12) = (199.82)
  58.     End Sub
  59. End Class
Dec 9 '09 #1
5 1886
hd82
3 New Member
anyone out their to offer help
Dec 10 '09 #2
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.
Dec 10 '09 #3
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:
Expand|Select|Wrap|Line Numbers
  1. For counter As Integer = 1 to 10
  2.   'do stuff
  3. Next
This loop declares a variable "counter" as an Integer and sets it to 1.
It's a short hand version of:
Expand|Select|Wrap|Line Numbers
  1. Dim counter As Integer = 1
  2. For counter To 12
  3.   'do stuff
  4. Next
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. Dim myArrayOfTwelveNumbers(12) As String= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"}
  2. 'The above array has been declared to hold 13 strings that contain text for numbers 0 - 12
  3.  
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:
Expand|Select|Wrap|Line Numbers
  1. Dim myArrayOfTwelveNumbers(12) As String= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"}
  2. For counter As Integer = 1 To 12
  3.   Console.WriteLine(myArrayOfTwelveNumbers(counter))
  4. Next
  5.  
This will print:
Expand|Select|Wrap|Line Numbers
  1. one
  2. two
  3. three
  4. four
  5. five
  6. six
  7. seven
  8. eight
  9. nine
  10. ten
  11. eleven
  12. 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:
Expand|Select|Wrap|Line Numbers
  1. Dim myArrayOfTwelveNumbers(12) As String= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"}
  2. For counter As Integer = 4 To 6
  3.   Console.WriteLine(myArrayOfTwelveNumbers(counter))
  4. Next
  5.  
It's pretty simple....but I still hate them.

-Frinny
Dec 10 '09 #4
hd82
3 New Member
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.
Dec 12 '09 #5
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
Dec 14 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
2
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...
17
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...
4
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...
18
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 ...
12
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:...
4
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 =...
4
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...
1
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...
1
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...
0
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...
0
jinu1996
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...
1
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...
0
agi2029
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,...
1
isladogs
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.