-
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
-
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
-
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
-
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...
Which causes...
To be...
The rest of your code is just adding 0 to sum.