Results 1 to 4 of 4

Thread: Sum in vb

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Sum in vb

    im new to vb and i want to sum my days and months and seems not working..i convert months back to days.suppose my code below works fine but i got no sum values at the end.please help

    Public Function test(length As Integer ) As String


    dim month As integer=0
    dim day As Integer=0
    dim sum as integer


    if length>31 Andalso length<367 then
    length=(month * 30.4375)
    month=(length)

    end if

    if length<31 Then
    day =cstr(length)
    end if

    sum= year+month+day

    return(sum)

    End Function

    thanks

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Sum in vb

    this is where you need to learn how to set breakpoints and step through the code...

    Let's say length (which is passed in) is 36 ...

    that means this is true...
    if length>31 Andalso length<367 then

    so this code runs... but month is set to 0
    length=(month * 30.4375)
    and anything times 0 is always 0... so now length is 0

    And now month is 0....
    month=(length)

    end if


    Length is now 0
    if length<31 Then

    And now so is Day...
    day =cstr(length) (fyi, CSTR isn't needed here... for any reason)
    end if

    we never set year, so it too is 0
    sum= year+month+day

    and 0 + 0 + 0 is still 0.
    return(sum)

    which is what you return.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Sum in vb

    ok..i edit the code.but my question is my sum below is working fine?as i said this code works fine and get me the right days..suppose i got 1 year= 365 and 1 months = 30 days..so i add the 365 + 30 =395. is my sum can have the code below where sum = months + days? or do i need to change another way round?...Is there any simplest way to do Sum in vb?

    Public Function test(length As Integer ) As String


    dim month As integer=0
    dim day As Integer=0
    dim sum as integer


    if length>31 Andalso length<367 then
    length=(month * 30.4375)
    month=(length)

    end if

    if length<31 Then
    day =(length)
    end if

    sum= month+day

    return(sum)

    End Function
    Last edited by Uttar; Nov 7th, 2012 at 05:28 PM.

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Sum in vb

    The problem pointed out with your code is here

    Code:
    length=(month * 30.4375)
    At this point month = 0 so...

    Code:
    length = 0 * 30.4375
    Which means...

    Code:
    length = 0
    Which causes...

    Code:
    month = length
    To be...

    Code:
    month = 0
    The rest of your code is just adding 0 to sum.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

Tags for this Thread

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