Results 1 to 6 of 6

Thread: adding totals?

  1. #1
    New Member
    Join Date
    Jan 12
    Posts
    2

    adding totals?

    hi im new to programming, started only a few days ago, so i don't know most of the definitions or jargon!

    but iv got a a asisignment where i have to a daily total and add it to the weekly total if that makes sense?

    basically

    weekTotal = 0 + dailyTotal

    now i know thats not the proper way beacause it resets each time. what i wana do is

    weekTotal = weekTotal + dailyTotal

    but i dont know how to do it! i read something online about using += to do it but when i do i get end of statement expected.
    what do i do?

  2. #2
    Fanatic Member ident's Avatar
    Join Date
    Mar 09
    Location
    Cambridge
    Posts
    1,020

    Re: adding totals?

    Code:
    weekTotal = weekTotal + dailyTotal
    would work same as below. You must be doing something else. Please post your code.

    Code:
    weekTotal += dailyTotal

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,834

    Re: adding totals?

    I would imagine that this is a scope issue. If you are declaring 'weekTotal' inside a method then it only exists inside that method, so it's value defaults to zero each time. If you want it to maintain its value between method calls then it must exist outside the method, i.e. you must declare it outside the method.

  4. #4
    New Member
    Join Date
    Jan 12
    Posts
    2

    Re: adding totals?

    Quote Originally Posted by jmcilhinney View Post
    I would imagine that this is a scope issue. If you are declaring 'weekTotal' inside a method then it only exists inside that method, so it's value defaults to zero each time. If you want it to maintain its value between method calls then it must exist outside the method, i.e. you must declare it outside the method.
    erm do you mean a global variable? if so how do i go about setting it up? iv only started rogramming recently as part of my ict class, and had a few lessons so fr, so tryin to do a crash course!

  5. #5
    Hyperactive Member
    Join Date
    Sep 04
    Location
    Manchester
    Posts
    265

    Re: adding totals?

    Unless I have grossly misunderstood, you are pretty much there. Try this on for size.

    http://pastebin.com/7F0U41mF

    (If you need to enter values through text boxes on a form. Try something like this.)

    http://pastebin.com/Q51kCawj
    Last edited by intraman; Jan 31st, 2012 at 01:46 PM.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,834

    Re: adding totals?

    Quote Originally Posted by moynzzee View Post
    erm do you mean a global variable? if so how do i go about setting it up? iv only started rogramming recently as part of my ict class, and had a few lessons so fr, so tryin to do a crash course!
    No I don't mean a global variable. I mean just what I said. A variable declared inside a method (Sub, Function or the Get or Set of a property) is a "local variable" while anything declared outside is a "member variable", also known as a "field". Any properties, methods, events, non-local variables, etc, that you declare within a type, e.g. a form, are all known as members. E.g.
    vb.net Code:
    1. Public Class SomeType
    2.  
    3.     Private memberVariable As Object
    4.  
    5.     Public Sub SomeMethod()
    6.         Dim localVariable As Object
    7.     End Sub
    8.  
    9. End Class

Posting Permissions

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