943,975 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 781
  • VB.NET RSS
Feb 13th, 2009
0

Help understanding code snippet

Expand Post »
Hi all, here is a code snippet I'm having trouble understanding.
VB.NET Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008
Feb 13th, 2009
0

Re: Help understanding code snippet

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

VB.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 155
Solved Threads: 41
Posting Whiz in Training
rapture is offline Offline
294 posts
since Jul 2007
Feb 18th, 2009
0

Re: Help understanding code snippet

Click to Expand / Collapse  Quote originally posted by rapture ...
First of all - if you surround your code with "" then people will respond quicker.
OK, thanks.

Quote ...
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

VB.NET Syntax (Toggle Plain Text)
  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:

VB.NET Syntax (Toggle Plain Text)
  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:

VB.NET Syntax (Toggle Plain Text)
  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:

VB.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: how to print data from datagrid view ?
Next Thread in VB.NET Forum Timeline: Opening word 2007 hijacks embedded Instance of Word 2007 in vb.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC