Results 1 to 6 of 6

Thread: adding totals?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    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
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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

    Thread Starter
    New Member
    Join Date
    Jan 2012
    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 2004
    Location
    Manchester
    Posts
    266

    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 02:46 PM.

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

    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
  •  



Click Here to Expand Forum to Full Width