Number of days

  • 14 years ago
    Hi,
    I have this function:
    Public Function NewReleasesTime(ByVal Time As String) As Integer
            Select Case Time
                Case "One Week"
                    Return 7
                Case "Two Weeks"
                    Return 14
                Case "Three Weeks"
                    Return 21
                Case "One Month"
                    Return 30
                Case "Two Months"
                    Return 60
                Case "Three Months"
                    Return 90
                Case "One Year"
                    Return 365
                Case "Two Years"
                    Return 730
                Case "Three Years"
                    Return 1095
            End Select
    End Function
    
    There is no method in VB 2005 that when is got the word “One Week” it return the number 7 (that means seven days)?

  • 14 years ago
    Uf I guess it is not necessary to process data like this. But I don't see kontext in which this crappy function should be needed :)

    Anyway, I think (hope), that there is no function like this :)... but if you prefer that way, you should make it more efficient... for example you can make one string like this with fixed atom length, where atom is part of that string containing e.g. "Three Years" and rest of atom is filled by some special char

    like this:

    function GRT(name as string) as int
    TMP = "$monday---$tuesday---$wensday---$friday----etc"
    and you only once run Instr(TMP,name)
    -- you will get e.g. 0 for monday, 10 for tuesday, 20 for ...
    you divide that with 10 - and you get PTR
    and you have array DayInWeek of int {1,2,3,4,5,6,0}
    where PTR applied on diw array will return you day in week...
    it is quicker I quess - only one string search, not n string comparings.
    Also Array.IndexOf(ArrayOfStrings,Needle) should be faster than select case on strings.















  • 14 years ago

    I don't know of any function to do what your asking but I had an idea that will work.  The thing you need to solve is really just converting a word to it's value.  For instance if we can covert ten to 10 then we could figure out the number of days by multiplying by the appropriate value.  10 weeks we multiply by 7; 10 months we multiply by 30.

    I tinkered with creating a function that could convert a word to it's actual numeric value.  So for instance "ten thousand seven hundred and forty-one" would be appropriatly converted to 10741.  I'll post the code below.  Hopefully it's self explanitory but basically you pass in a string that represents a number like the one I just mentioned and it returns the real value.  Now all you need to do is create another function (which should be pretty easy) that converts the words "month" "week" etc. to the appropriate multiplier.  You can the parse your string of "three weeks" pass "three" to my function and multiply the result by the result of your function to be created.  Make sense?  This should allow you to create a very robust function without limits.  Hope this helped.  The function should work but if you have any problems let me know.

      'I didn't handle overflow errors so any number over the max integer
       'will throw an error.  This should normally be dealt with.
       Private Function ConvertWordToNumber(ByVal num As String) As Integer
          Dim value As Integer = 0
    
          'Convert to all lower case and remove hyphens and spaces.
          'I think that's all that needs to be done here.
          num = num.ToLower
          num = num.Replace(" "c, "")
          num = num.Replace("-"c, "")
    
          Do While num.Length > 0
             Dim length = num.Length
             If num.StartsWith("twenty") Then
                value += 20
                num = num.Remove(0, 6)
             ElseIf num.StartsWith("thirty") Then
                value += 30
                num = num.Remove(0, 6)
             ElseIf num.StartsWith("forty") Then
                value += 40
                num = num.Remove(0, 5)
             ElseIf num.StartsWith("fifty") Then
                value += 50
                num = num.Remove(0, 5)
             ElseIf num.StartsWith("sixty") Then
                value += 60
                num = num.Remove(0, 5)
             ElseIf num.StartsWith("seventy") Then
                value += 70
                num = num.Remove(0, 7)
             ElseIf num.StartsWith("eighty") Then
                value += 80
                num = num.Remove(0, 6)
             ElseIf num.StartsWith("nintey") Then
                value += 90
                num = num.Remove(0, 6)
             ElseIf num.StartsWith("ten") Then
                value += 10
                num = num.Remove(0, 3)
             ElseIf num.StartsWith("eleven") Then
                value += 11
                num = num.Remove(0, 6)
             ElseIf num.StartsWith("twelve") Then
                value += 12
                num = num.Remove(0, 6)
             ElseIf num.StartsWith("thirteen") Then
                value += 13
                num = num.Remove(0, 8)
             ElseIf num.StartsWith("fourteen") Then
                value += 14
                num = num.Remove(0, 8)
             ElseIf num.StartsWith("fifteen") Then
                value += 15
                num = num.Remove(0, 7)
             ElseIf num.StartsWith("sixteen") Then
                value += 16
                num = num.Remove(0, 7)
             ElseIf num.StartsWith("seventeen") Then
                value += 17
                num = num.Remove(0, 9)
             ElseIf num.StartsWith("eighteen") Then
                value += 18
                num = num.Remove(0, 8)
             ElseIf num.StartsWith("nineteen") Then
                value += 19
                num = num.Remove(0, 8)
             ElseIf num.StartsWith("zero") Then
                num = num.Remove(0, 4)
             ElseIf num.StartsWith("one") Then
                value += 1
                num = num.Remove(0, 3)
             ElseIf num.StartsWith("two") Then
                value += 2
                num = num.Remove(0, 3)
             ElseIf num.StartsWith("three") Then
                value += 3
                num = num.Remove(0, 5)
             ElseIf num.StartsWith("four") Then
                value += 4
                num = num.Remove(0, 4)
             ElseIf num.StartsWith("five") Then
                value += 5
                num = num.Remove(0, 5)
             ElseIf num.StartsWith("six") Then
                value += 6
                num = num.Remove(0, 3)
             ElseIf num.StartsWith("seven") Then
                value += 7
                num = num.Remove(0, 5)
             ElseIf num.StartsWith("eight") Then
                value += 8
                num = num.Remove(0, 5)
             ElseIf num.StartsWith("nine") Then
                value += 9
                num = num.Remove(0, 4)
             ElseIf num.StartsWith("hundred") Then
                value *= 100
                num = num.Remove(0, 7)
             ElseIf num.StartsWith("thousand") Then
                value *= 1000
                num = num.Remove(0, 8)
                Return value + ConvertWordToNumber(num)
             ElseIf num.StartsWith("million") Then
                value *= 1000000
                num = num.Remove(0, 7)
                Return value + ConvertWordToNumber(num)
             ElseIf num.StartsWith("billion") Then
                value *= 1000000000
                num = num.Remove(0, 7)
                Return value + ConvertWordToNumber(num)
             ElseIf num.StartsWith("and") Then
                num = num.Remove(0, 3)
             End If
    
             'Length of our variable didn't change so we know 
             'nothing was stripped from it.
             'This would result in an infinite loop so we need to exit
             'An error should probably be raised.
             If length = num.Length Then Exit Do
          Loop
    
          Return value
       End Function
  • 14 years ago

    Hi TwoFaced,

     

    I will let you know how this function is working and the MyTextBox class two. Thanks a lot.

  • 14 years ago

    Ya, sorry for my misunderstand - twofaceds func does give sense to question also solution.
    :)


Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler