Help understanding code snippet

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 18
Reputation: TriceD is an unknown quantity at this point 
Solved Threads: 0
TriceD TriceD is offline Offline
Newbie Poster

Help understanding code snippet

 
0
  #1
Feb 13th, 2009
Hi all, here is a code snippet I'm having trouble understanding.
  1. public notinheritable class myClass
  2.  
  3. ------Public Delegate Sub mySubDelegate()
  4.  
  5. ------Public Sub mySub(ByVal Text As String)
  6.  
  7. -----------------If Me.InvokeRequired Then
  8.  
  9. ------------------------Dim SI As New mySubDelegate(AddressOf mySub)
  10.  
  11. ------------------------Me.Invoke(SI, New Object() {Text})
  12.  
  13. -----------------Else
  14.  
  15. ------------------------ Text = "Something"
  16.  
  17. -----------------End If
  18.  
  19. ------End Sub
  20.  
  21. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: Help understanding code snippet

 
0
  #2
Feb 13th, 2009
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

  1. if iceCream = chocolate
  2. give user chocolate (then you head to the end if)
  3. else
  4. give user vanilla (then head to the end if)
  5. end if
Last edited by rapture; Feb 13th, 2009 at 4:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: TriceD is an unknown quantity at this point 
Solved Threads: 0
TriceD TriceD is offline Offline
Newbie Poster

Re: Help understanding code snippet

 
0
  #3
Feb 18th, 2009
Originally Posted by rapture View Post
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

  1. if iceCream = chocolate
  2. give user chocolate (then you head to the end if)
  3. else
  4. give user vanilla (then head to the end if)
  5. 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:

  1. 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:

  1. 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:

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

doesn't cause an invoke to not be required, thus causing the ELSE part to be executed.
Last edited by TriceD; Feb 18th, 2009 at 7:20 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC