|
-
Jul 27th, 2009, 03:06 PM
#1
Thread Starter
Addicted Member
Module Help
I have a module containing several functions which are used for testing. The following is an example of one of the functions:
Code:
Public Function IsEmpty(ByVal TestPosition As Microsoft.Xna.Framework.Point) As Boolean
TestPosition.X = (TestPosition.X + BlockSize - 515) / BlockSize - 1
TestPosition.Y = (TestPosition.Y - 213) / BlockSize - 1
If TestPosition.X >= 0 And TestPosition.Y >= 0 And TestPosition.X < 10 And TestPosition.Y < 15 Then
If Form1.Blocks(TestPosition.X, TestPosition.Y) Is Nothing Then
Return True
Else
Return False
End If
End If
End Function
As you can see in this I am referring specifically to form1. However I need to be able to pass the form as a parameter to the function so I can use it on multiple forms.
Any help appreciated
-
Jul 27th, 2009, 03:19 PM
#2
Re: Module Help
Once again, the default instance rears its ugly head.
What you need to do is add an argument to the method:
Code:
Public Function IsEmpty(ByVal TestPosition As Microsoft.Xna.Framework.Point, frm1 as Form1) As Boolean
TestPosition.X = (TestPosition.X + BlockSize - 515) / BlockSize - 1
TestPosition.Y = (TestPosition.Y - 213) / BlockSize - 1
If TestPosition.X >= 0 And TestPosition.Y >= 0 And TestPosition.X < 10 And TestPosition.Y < 15 Then
If frm1.Blocks(TestPosition.X, TestPosition.Y) Is Nothing Then
Return True
Else
Return False
End If
End If
End Function
The comment about the default instance is that Form1 is both a class type, and an instance of type Form1. That default instance was a feature added with version 2005 and on. I feel that it causes more confusion than any possible benefit that it could provide. Because the class type is also an instance of the type, which was created secretly before the program started, you can be fooled into not realizing that a form is just an object like any other object, and form objects can be passed around just like any other variable. All I did was add an argument of type Form1, and use it in the method in place of Form1. If there hadn't been a default instance of Form1 called Form1, your code wouldn't have worked because Form1 would be a type, not an instance of a type. With the default, Form1 was both, so your code worked, yet it wasn't clear to you WHY it worked, or you wouldn't have asked. After all, you passed in one argument, so you certainly knew what you were doing with normal objects that don't have 'hidden' default instances. The default instance just muddied the water.
I really dislike default instances.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|