Problem with AddHandler

aDvo

New member
Joined
Sep 3, 2005
Messages
2
Programming Experience
1-3
Hi guys,

Here's my basic code, i'm trying to create a timer and add a handler to the timer that calls the form's paint sub.
VB.NET:
Public Class Form1

   Dim t As New System.Timers.Timer(500)

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

	   AddHandler t.Elapsed, AddressOf Me.Paint
	 
   End Sub


   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
	   'do something
   End Sub
End Class

I get the following error for the "Me.Paint" part of the AddHandler method:
'AddressOf' operand must be the name of a method; no
parentheses are needed.


I read in a forum somewhere else that AddressOf cannot call a sub with args?

Any idea?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,570
Location
Sydney, Australia
Programming Experience
10+
As Kulrom says, the entry point for a thread can have no arguments and no return value. You would need to create another method with that signature and then call your Paint event handler from there, although you should really be calling Me.Refresh() rather than the event handler itself. Because Refresh is a procedure with no arguments, you can use it as the entry point for a thread. I'm not quite sure why you would want to use a spereate thread anyway though, because if painting your form takes so long that it requires a new thread then your form is going to perform very poorly.
 

aDvo

New member
Joined
Sep 3, 2005
Messages
2
Programming Experience
1-3
Thanks for your replies...

I'm very new to vb.net, I'm not sure what is referred to as "delegate" or what is meant by "the entry point for a thread can have no arguments and no return value". Could you explain further?
My story as follows:
-------------------------------------------------------------------------

I'm trying to build a blackjack game.

I have in mind a client server setup, server as the dealer, while clients connect to the server over the net individual players.

The thing I cannot picture in my head is how the animations will be integrated in the game.

The point is, animations (client side) do not happen all the time. THey onnly happen when a player requests for a card, in which a card will fly from the dealer's deck to the player's hand.

Is a game (animation) loop suitable for this? Or perhaps a gameloop which only runs on demand? This is more of an animation loop than an actual game loop as there is not much game logic involved in real time.

Also, should I use a "timer" anywhere to time/control my card animations?

Any advice is greatly appreciated.

-Kevin-
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,570
Location
Sydney, Australia
Programming Experience
10+
I suggest you take a look at these two MSDN topics, if only to get some ideas.

http://www.microsoft.com/mind/0396/games.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/pocketjack.asp

With regards to delegates and threads, basically all you need to know for this situation is that the method that you use with the AddressOf statement must be a procedure (Sub) and not a function, and it cannot have any parameters, e.g.
VB.NET:
Private Function Method1() As String
Private Sub Method2(ByVal argument As String)
Private Sub Method3()
Method1 cannot be the entry point for a thread because it returns a value. Method2 cannot be the entry point for a thread because it takes an argument. Method3 can be the entry point for a thread because it has the correct signature, i.e. takes no arguments and returns no value.
 
Top Bottom