Results 1 to 4 of 4
  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Chelmsford UK

    Re: Trying to understand how to write code

    try this:

    vb Code:
    1. Public Class Form1
    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)
    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)")
    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 12:50 AM.

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

    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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.