How to return value from invoked function?

T

Terry Olsen

How do I get this to work? It always returns False, even though I can see
"This is True!" in the debug window. Do I have to invoke functions
differently than subs?

Private Delegate Function IsLvItemCheckedDelegate(ByVal ClientID As Integer)
As Boolean
Private Function IsLvItemChecked(ByVal ClientID As Integer) As Boolean
If lvServers.InvokeRequired = True Then
lvServers.Invoke(New IsLvItemCheckedDelegate(AddressOf IsLvItemChecked),
ClientID)
Else
For i As Integer = 0 To lvServers.CheckedItems.Count - 1
If CInt(lvServers.CheckedItems(i).SubItems(1).Text) = ClientID Then
Debug.WriteLine("This is true!")
Return True
End If
Next
Return False
End If
End Function
 
A

Armin Zingler

Terry Olsen said:
How do I get this to work? It always returns False, even though I
can see "This is True!" in the debug window. Do I have to invoke
functions differently than subs?

Private Delegate Function IsLvItemCheckedDelegate(ByVal ClientID As
Integer) As Boolean
Private Function IsLvItemChecked(ByVal ClientID As Integer) As
Boolean If lvServers.InvokeRequired = True Then
lvServers.Invoke(New IsLvItemCheckedDelegate(AddressOf
IsLvItemChecked), ClientID)
Else
For i As Integer = 0 To lvServers.CheckedItems.Count - 1
If CInt(lvServers.CheckedItems(i).SubItems(1).Text) = ClientID Then
Debug.WriteLine("This is true!")
Return True
End If
Next
Return False
End If
End Function


I don't completely understand the example. What returns false? The Debug
statement doesn't evaluate a function return value. It is based on an
expression, a comparsion comparing two integers (ClientID and Cint).
In other words, the debug output is not based on the function return value.

Using the delegate, you never evaluate the returned value, thus you can not
say whether it's True or False. If you wanted to do it, just look at the
return value of the Invoke function.


Armin
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Terry said:
How do I get this to work? It always returns False, even though I can see
"This is True!" in the debug window. Do I have to invoke functions
differently than subs?

Private Delegate Function IsLvItemCheckedDelegate(ByVal ClientID As Integer)
As Boolean
Private Function IsLvItemChecked(ByVal ClientID As Integer) As Boolean
If lvServers.InvokeRequired = True Then
lvServers.Invoke(New IsLvItemCheckedDelegate(AddressOf IsLvItemChecked),
ClientID)
Else
For i As Integer = 0 To lvServers.CheckedItems.Count - 1
If CInt(lvServers.CheckedItems(i).SubItems(1).Text) = ClientID Then
Debug.WriteLine("This is true!")
Return True
End If
Next
Return False
End If
End Function

The invoked function returns the value just fine, and the Invoke method
returns it to the calling code, but as you don't get the return value
from the Invoke method, the value is gone.

Get the return value, cast it to boolean and return it.
 
T

Terry Olsen

I must be missing something here. I AM returning the boolean values. Look at
the "Return True" and "Return False" code lines.

I call this code with :

Dim x as boolean=IsLvItemChecked(ClientID)

It always returns x=false, even though the ClientID and CInt(expression) are
equal (hence the "This is true!" in the debug window). Directly after that
Debug.Writeline statement, is coded "Return True". Yet I still get a False.
 
T

Terry Olsen

Armin Zingler said:
I don't completely understand the example. What returns false?

The function 'IsLvItemChecked' returns false.

Dim x as boolean=IsLvItemChecked(ClientID)

x is false every time, even when it should be true.
The Debug
statement doesn't evaluate a function return value. It is based on an
expression, a comparsion comparing two integers (ClientID and Cint).
In other words, the debug output is not based on the function return
value.

Yes, I know that. The debug statement is my troubleshooting tool. I know the
comparison of the two integers are equal, because it is writing "This is
true!" in the debug window! Get it? The next statement after the debug line
is the "Return True" line. So If I see "This is true!" in my debug window, I
should see x=True. That is not happening. The x variable is alway false,
even when it "Return's True".
Using the delegate, you never evaluate the returned value,

So what am I missing here? How do I evaluate the returned value? I thought I
was doing that in the calling code....Is there something else I should be
doing to get the returned value from the delegate? This routine works fine
when I call it from the same thread (because it's not invoking the
delegate). But when I call it from another thread, it always returns false.
thus you can not
say whether it's True or False.

Yes I can. The calling code told me so. The fact that "This is true!"
appears in the debug window tells me that it should have returned True when,
in fact, it returned False.
If you wanted to do it, just look at the return value of the Invoke
function.

Been there, done that. Doesn't work.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Terry said:
I must be missing something here. I AM returning the boolean values. Look at
the "Return True" and "Return False" code lines.

Yes, as I said, the invoked function returns the value just fine, but
the code that invokes the method just throws away the return value.

Look at the lvServers.Invoke line.
I call this code with :

Dim x as boolean=IsLvItemChecked(ClientID)

It always returns x=false, even though the ClientID and CInt(expression) are
equal (hence the "This is true!" in the debug window). Directly after that
Debug.Writeline statement, is coded "Return True". Yet I still get a False.

Yes, of course you do. The return value of the method is initialised to
zero (false) when the method starts. The code in the method just invokes
a method (which happens to be the same method) in another thread, it
never sets the return value. Therefore the initialised value is returned.
 
A

Armin Zingler

Terry Olsen said:
The function 'IsLvItemChecked' returns false.

Dim x as boolean=IsLvItemChecked(ClientID)

x is false every time, even when it should be true.

You didn't show where you check the return value of the function, that's why
I asked. In addition, in the subject you was referring to the return value
from an invoked function. In the code you posted, you ignore the return
value. That's why I asked "what returns false?".
Yes, I know that. The debug statement is my troubleshooting tool. I
know the comparison of the two integers are equal, because it is
writing "This is true!" in the debug window! Get it? The next
statement after the debug line is the "Return True" line. So If I
see "This is true!" in my debug window, I should see x=True. That is
not happening. The x variable is alway false, even when it "Return's
True".

You are ignoring the return value of the Invoke function. Return it to the
caller and you will get the correct value.
So what am I missing here? How do I evaluate the returned value? I
thought I was doing that in the calling code....Is there something
else I should be doing to get the returned value from the delegate?
This routine works fine when I call it from the same thread (because
it's not invoking the delegate). But when I call it from another
thread, it always returns false.

see above
Yes I can. The calling code told me so. The fact that "This is
true!" appears in the debug window tells me that it should have
returned True when, in fact, it returned False.

I was referring to your statement saying that the invoked function always
returns False. As you ignore the function return value, I stated that you
can not say whether it's True or False.
Been there, done that. Doesn't work.

No, you ignore the return value of the Invoke function. :)


Armin
 
T

Terry Olsen

Okay, lightbulb comes on....again. I missed the difference between the
"Invoked Function" and the "Invoke Function" :)

Thanks for setting me straight...both of you....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top