Results 1 to 4 of 4

Thread: Trying to understand how to write code

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    2

    Trying to understand how to write code

    I have Visual Basic 2010 and also bought a book to study and practice problems. I know how to add controls and assign properties but get confused with the coding part. Can someone help me understand this somewhat?

    Here is one problem I am working on-

    Write a program to make change for an amount of money from 0-99 cents input by the user. the output of the program should show the number of coins from each denomination used to make change.

    Now I know what controls i need on the windows form but I am sorta lost on the coding to make this program work.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Trying to understand how to write code

    VB.NET is event driven, which means that, in your code, you handle events and then execute code. The first thing to consider is WHEN you want to do something. That will tell you what event you need to handle. In your case, you most likely want to do something when the user clicks a Button, so you need to handle the Click event of your Button. Double-click the Button in the designer to generate a Click event handler. The event handler is a method that will be executed when the user clicks the Button. You can put your code in that method.

    The next order of business is WHAT you want to happen. Before you even consider writing any code, you should first pick up a pen and paper and write down the steps you need to carry out to achieve your aim. Until you have a set of steps that you could hand to someone else and they could perform the operation manually, don't even think about code.

    I'm talking elementary steps here too; not broad strokes. You can start with the broad strokes but then you need to break each step down until they are as simple as possible. These steps will have to eventually be translated into code and each step should equate to no more than one or two lines of code.

    Once you have the steps and you can run through them using pen and paper to get the correct results, the next step is to translate the steps into pseudo-code. That is something that looks something like code but is a bit more human-friendly and doesn't conform to the rules of any particular programming language. Only when you're happy with the pseudo-code should you consider writing any real code in VB. By that stage, most of the code should pretty much write itself, with just a bit of help needed on the details.

    Remember that programming is not about creating solutions to problems. It's about implementing solutions. You should have already solved the problem before you come to write the code. Calculating change is not a programming problem. Someone with basic maths skills should be able to do it. Solve the problem first, articulate the steps required to enact that solution and THEN write code to implement it.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: Trying to understand how to write code

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         ListBox1.Items.Clear()
    5.         Dim change As Decimal
    6.         If Decimal.TryParse(TextBox1.Text, change) AndAlso change > 0 AndAlso change <= 0.99 Then
    7.             Dim half As Integer = Math.Abs(CInt(change >= 0.5))
    8.             If half > 0 Then change -= CDec(half * 0.5)
    9.             Dim quarter As Integer = Math.Abs(CInt(change >= 0.25))
    10.             If quarter > 0 Then change -= CDec(quarter * 0.25)
    11.             Dim dime As Integer = CInt(CInt(change * 100) \ 10)
    12.             If dime > 0 Then change = CDec(change Mod (dime * 0.1))
    13.             Dim nickel As Integer = CInt(CInt(change * 100) \ 5)
    14.             If nickel > 0 Then change = CDec(change Mod (nickel * 0.05))
    15.             Dim penny As Integer = CInt(change * 100)
    16.  
    17.             If half > 0 Then ListBox1.Items.Add("1 half")
    18.             If quarter > 0 Then ListBox1.Items.Add("1 quarter")
    19.             If dime > 0 Then ListBox1.Items.Add(dime.ToString & " dime(s)")
    20.             If nickel > 0 Then ListBox1.Items.Add(nickel.ToString & " nickel(s)")
    21.             If penny > 0 Then ListBox1.Items.Add(penny.ToString & " penny(s)")
    22.  
    23.         End If
    24.     End Sub
    25. End Class

    edit: it's a start but not perfect. i just noticed there can never be more than 1 nickel
    Last edited by .paul.; Feb 21st, 2011 at 01:50 AM.

  4. #4
    New Member CavitePride™'s Avatar
    Join Date
    Feb 2011
    Location
    Cavite Philippines
    Posts
    9

    Re: Trying to understand how to write code

    youtube and google would be your greatest tutor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width