By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,646 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,646 IT Pros & Developers. It's quick & easy.

How to return value from invoked function?

P: n/a
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
Aug 6 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Terry Olsen" <to******@hotmail.comschrieb
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

Aug 6 '07 #2

P: n/a
Terry Olsen wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 6 '07 #3

P: n/a
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.

"Göran Andersson" <gu***@guffa.comwrote in message
news:O0**************@TK2MSFTNGP02.phx.gbl...
Terry Olsen wrote:
>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.

--
Göran Andersson
_____
http://www.guffa.com

Aug 7 '07 #4

P: n/a

"Armin Zingler" <az*******@freenet.dewrote in message
news:eS**************@TK2MSFTNGP04.phx.gbl...
"Terry Olsen" <to******@hotmail.comschrieb
>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 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.
Aug 7 '07 #5

P: n/a
Terry Olsen wrote:
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.
"Göran Andersson" <gu***@guffa.comwrote in message
news:O0**************@TK2MSFTNGP02.phx.gbl...
>Terry Olsen wrote:
>>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.

--
Göran Andersson
_____
http://www.guffa.com


--
Göran Andersson
_____
http://www.guffa.com
Aug 7 '07 #6

P: n/a
"Terry Olsen" <to******@hotmail.comschrieb
>
"Armin Zingler" <az*******@freenet.dewrote in message
news:eS**************@TK2MSFTNGP04.phx.gbl...
"Terry Olsen" <to******@hotmail.comschrieb
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 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?".
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".
You are ignoring the return value of the Invoke function. Return it to the
caller and you will get the correct value.
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.
see above
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.
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.
If you wanted to do it, just look at the return value of the
Invoke function.

Been there, done that. Doesn't work.
No, you ignore the return value of the Invoke function. :)
Armin

Aug 7 '07 #7

P: n/a
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....

"Armin Zingler" <az*******@freenet.dewrote in message
news:ug**************@TK2MSFTNGP05.phx.gbl...
"Terry Olsen" <to******@hotmail.comschrieb
>>
"Armin Zingler" <az*******@freenet.dewrote in message
news:eS**************@TK2MSFTNGP04.phx.gbl...
"Terry Olsen" <to******@hotmail.comschrieb
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 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?".
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".

You are ignoring the return value of the Invoke function. Return it to the
caller and you will get the correct value.
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.

see above
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.

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.
If you wanted to do it, just look at the return value of the
Invoke function.

Been there, done that. Doesn't work.

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

Aug 7 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.