|
-
Jun 7th, 2006, 07:09 PM
#1
Thread Starter
Member
[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:
Public Function get_text(firstword As String, secondword As String)
Dim extractedtext As String
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = InStr(1, HTML, firstword)
b = a + Len(firstword)
c = InStr(b, HTML, secondword)
extractedtext = Mid$(HTML, b, c - b)
extractedtext = Replace(extractedtext, "\r", "")
extractedtext = Replace(extractedtext, "\t", "")
extractedtext = Replace(extractedtext, "\n", "")
extractedtext = Replace(extractedtext, ",", "")
Label2.Caption = Int(extractedtext)
End Function
Private Sub Command2_Click()
Dim returned_text As Integer
Dim intmoney As Integer
returned_text = get_text("money:", "Turn")
intmoney = Int(returned_text)
Label1.Caption = intmoney
End Sub
Last edited by pilot_kof; Jun 7th, 2006 at 07:12 PM.
-
Jun 7th, 2006, 07:11 PM
#2
PowerPoster
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"
-
Jun 7th, 2006, 07:13 PM
#3
Thread Starter
Member
Re: basic function question
 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
-
Jun 7th, 2006, 07:14 PM
#4
PowerPoster
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.
-
Jun 7th, 2006, 07:18 PM
#5
New Member
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:
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
-
Jun 7th, 2006, 07:32 PM
#6
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:
Public Function get_text(firstword As String, secondword As String) As Integer
'... your code ...
get_text = 1 '(Return a value)
End Function
Last edited by jcis; Jun 7th, 2006 at 07:35 PM.
-
Jun 7th, 2006, 07:34 PM
#7
Thread Starter
Member
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:
Public Function get_text(firstword As String, secondword As String)
Dim extractedtext As String
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = InStr(1, HTML, firstword)
b = a + Len(firstword)
c = InStr(b, HTML, secondword)
extractedtext = Mid$(HTML, b, c - b)
Label2.Caption = Int(extractedtext)
get_text = extractedtext
End Function
Private Sub Command2_Click()
Dim returned_text As String
Dim intmoney As Integer
returned_text = get_text("Money:", "Turn")
[B]intmoney = CInt(returned_text)[/B]
Label1.Caption = intmoney
End Sub
Last edited by pilot_kof; Jun 7th, 2006 at 07:41 PM.
-
Jun 7th, 2006, 07:38 PM
#8
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|