Question Object variable or With block variable not set

svibuk

Active member
Joined
Jul 15, 2008
Messages
31
Programming Experience
1-3
if i add the reference of class file to the windows form page i and execute the form page i get
Object variable or With block variable not set. error
 

svibuk

Active member
Joined
Jul 15, 2008
Messages
31
Programming Experience
1-3
class file


Public Class mkey
Public Sub getkey()
Try
MsgBox("hello")

Catch ex As Exception

End Try
End Sub


referencing the method in windows forms
Imports myproject1.mkey


in form load

Dim mk As mkey = Nothing

mk.getkey()
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,746
Location
Sydney, Australia
Programming Experience
10+
Well there you go then. You are explicitly setting 'mk' to Nothing and then you try to call 'getkey' on it. How can you call a method on an object that doesn't exist? That's like saying "I have no car and I'm going to go out and drive it". How can you drive your car if you have no car? Likewise, how can you call 'getkey' on an 'mkey' object if you have no 'mkey' object? If you want an 'mkey' object then you have to create one:
Dim mk As mkey = New mkey
or use the more common shorthand:
Dim mk As New mkey
 

Latest posts

Top Bottom