Results 1 to 8 of 8

Thread: [RESOLVED] basic function question

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    34

    Resolved [RESOLVED] basic function question

    alright well I have no idea why this isn't returning a result for me can anyone see possibly why? when I post the results to label2 inside of the function it posts the correct result when I do it inside the sub where I called the function the return is "0"
    VB Code:
    1. Public Function get_text(firstword As String, secondword As String)
    2. Dim extractedtext As String
    3. Dim a As Integer
    4. Dim b As Integer
    5. Dim c As Integer
    6.  
    7. a = InStr(1, HTML, firstword)
    8. b = a + Len(firstword)
    9. c = InStr(b, HTML, secondword)
    10. extractedtext = Mid$(HTML, b, c - b)
    11. extractedtext = Replace(extractedtext, "\r", "")
    12. extractedtext = Replace(extractedtext, "\t", "")
    13. extractedtext = Replace(extractedtext, "\n", "")
    14. extractedtext = Replace(extractedtext, ",", "")
    15. Label2.Caption = Int(extractedtext)
    16.  
    17. End Function
    18.  
    19. Private Sub Command2_Click()
    20. Dim returned_text As Integer
    21. Dim intmoney As Integer
    22. returned_text = get_text("money:", "Turn")
    23. intmoney = Int(returned_text)
    24. Label1.Caption = intmoney
    25. End Sub
    Last edited by pilot_kof; Jun 7th, 2006 at 07:12 PM.

  2. #2
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: basic function question

    Totally ignoring your question...that's one long piece of code to do a simple data grab from a string :-)

    And answering your question...when you write a function that returns a string or number, you NEED to put "functionname = string" as the last line...in this case, "Get_Text = ExtractedText"

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    34

    Re: basic function question

    Quote Originally Posted by smUX
    Totally ignoring your question...that's one long piece of code to do a simple data grab from a string :-)
    yeah I was thinking the \r \n \t and " " may have been giving me the issue but that obviously wasn't it i'll cut all that out once i get this thing working

  4. #4
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: basic function question

    Read above...I edited :-)

    Now lemme see if I can help you here...do me a favour and copy the area of text you are planning to parse the data from...about 10-20 characters before and after the data :-)
    Last edited by smUX; Jun 7th, 2006 at 07:17 PM.

  5. #5
    New Member
    Join Date
    May 2006
    Posts
    7

    Re: basic function question

    I assume you mean the 'SUB' statement :
    returned_text = get_text is returning
    is has a vaule of '0'.

    This would be correct. In order for the function to return anything to the caller the function needs to place the return value into its name like:

    get_text = "....."
    Or to steel from MSDN

    Function CalculateSquareRoot(NumberArg As Double) As Double
    If NumberArg < 0 Then ' Evaluate argument.
    Exit Function ' Exit to calling procedure.
    Else
    CalculateSquareRoot = Sqr(NumberArg) ' Return square root.
    End If
    End Function

    Note the 'CalculateSquareRoot = ' above.

    I hope it helps

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: basic function question

    1) Where is HTML variable declared?
    2) Public Functions are in modules (not forms), so you can't say Label2.Caption there, you have to say: Form1.Label2.Caption.
    3) To assign a value to a Function, at the end: FunctionName = Value, but also, the Function must be declared AS, the same way than variables, example:
    VB Code:
    1. Public Function get_text(firstword As String, secondword As String) As Integer
    2.    '... your code ...
    3.    get_text = 1 '(Return a value)
    4. End Function
    Last edited by jcis; Jun 7th, 2006 at 07:35 PM.

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    34

    Re: basic function question

    alright well here is the updated bit it gets all the way down to intmoney = CInt(returned_text) before it fails out on me returned_text is holding the correct value but I'm not sure why it stops right there any more ideas?
    HTML is delcared as a public variable at the start of the program didn't feel the urge to flood you guys but good thinkin

    VB Code:
    1. Public Function get_text(firstword As String, secondword As String)
    2. Dim extractedtext As String
    3. Dim a As Integer
    4. Dim b As Integer
    5. Dim c As Integer
    6.  
    7. a = InStr(1, HTML, firstword)
    8. b = a + Len(firstword)
    9. c = InStr(b, HTML, secondword)
    10. extractedtext = Mid$(HTML, b, c - b)
    11. Label2.Caption = Int(extractedtext)
    12. get_text = extractedtext
    13.  
    14. End Function
    15.  
    16. Private Sub Command2_Click()
    17. Dim returned_text As String
    18. Dim intmoney As Integer
    19. returned_text = get_text("Money:", "Turn")
    20. [B]intmoney = CInt(returned_text)[/B]
    21. Label1.Caption = intmoney
    22. End Sub
    Last edited by pilot_kof; Jun 7th, 2006 at 07:41 PM.

  8. #8
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: basic function question

    Just a guess here, but at the end of public function (outside of the ( ) that is...right at the end) put " as integer"

    then change "get_text = extractedtext" to "get_text = CInt(extractedtext)"...see if that makes any difference...and what *exactly* is extractedtext? is it just numbers, has it got commas, spaces etc? I don't use CInt() at all so don't even know what it does or how pedantic it is with regards to what it's given to work with :-)

    Of course, then change "returned_text = get_text("Gold:", "Turn")" to "intmoney = get_text("Gold:", "Turn")"

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