Hi all, here is a code snippet I'm having trouble understanding.

public notinheritable class myClass

------Public Delegate Sub mySubDelegate()

------Public Sub mySub(ByVal Text As String)

-----------------If Me.InvokeRequired Then

------------------------Dim SI As New mySubDelegate(AddressOf mySub)

------------------------Me.Invoke(SI, New Object() {Text})

-----------------Else

------------------------ Text = "Something"

-----------------End If

------End Sub

End class

Assume that for the execution of mySub, that an invoke is required. So the IF condition should be satisfied. Why doesn't this code execute the ELSE section as well? My expectation was that the line: Me.Invoke(SI, New Object() {Text}) would cause the program to execute the Else section as well. Can anybody help me understand why it doesn't? Thanks.

Recommended Answers

All 2 Replies

First of all - if you surround your code with "

" then people will respond quicker.

Secondly,
in an if statement you follow this line of resaoning

if condition is met then perform the routine then go directly to "end if" do not pass go and do not collect $200. If the condition isn't met and there is an else, then everything that doesn't meet your condition gets the else. Let's say you can only choose chocolate or vanilla ice cream. It would be

if iceCream = chocolate
     give user chocolate (then you head to the end if)
else
     give user vanilla (then head to the end if)
end if

First of all - if you surround your code with "

" then people will respond quicker.

OK, thanks.

Secondly,
in an if statement you follow this line of resaoning

if condition is met then perform the routine then go directly to "end if" do not pass go and do not collect $200. If the condition isn't met and there is an else, then everything that doesn't meet your condition gets the else. Let's say you can only choose chocolate or vanilla ice cream. It would be

if iceCream = chocolate
     give user chocolate (then you head to the end if)
else
     give user vanilla (then head to the end if)
end if

Thanks for the explanation, but my problem doesn't lie in understanding how IF-ELSE conditions are executed, but rather in the specific code found in this particular IF-ELSE statement.

According to my understanding, which, mind you, is shaky at best, since I'm a vb.net newb, on the line:

Dim SI As New mySubDelegate(AddressOf mySub)

,

SI is declared as a delegate, pointing to the mySub method. So when it's called on the line:

Me.Invoke(SI, New Object() {Text})

,

it calls the mySub method a second time. This second time, according to my understanding, doesn't need an invoke. So in effect, the mySub() method should be called twice. Once, when an invoke is required causing the IF part to be executed, and once when an invoke is not required, causing the ELSE part to be executed.

What I'd like to find out is why the line:

Me.Invoke(SI, New Object() {Text})

doesn't cause an invoke to not be required, thus causing the ELSE part to be executed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.