Error Calling Function

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
At the moment i am just playing around with functions and i keep getting this annoying error message with the code below


'An unhandled exception of type 'System.NullReferenceException' occurred in Procedures and Functions - Hr 10.exe

Additional information: Object reference not set to an instance of an object.'

Visual Basic:
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      Dim c As Class1
      Dim i As Integer

      i = c.ComputeLength("hELLO")
      MsgBox(i)

   End Sub

Visual Basic:
Public Class Class1

   Public Function ComputeLength(ByVal strText As String) As Integer
      Return strText.Length
   End Function


End Class

Can someone please point me in the right direction?
 
The error means that an object is not created yet. You have declared here
Dim c As Class1
but you did not initialize it. You did not create it. You made it ready to be created.
c=new Class1(); - c# code, but vb is very similar.
 
Visual Basic:
Dim c As New Class1
In case like this you should consider making your function shared so you dont have to create class instance everytime.
 
Back
Top