Basically I’m Trying to put out on the Textbox inside the Shared Function, But i didn’t access the Textbox inside the Shared Function Then i created an object of the _Default Class to access the Textbox.
After that i access the Textbox, but during running state it give me error NullReference, when i toggle i found that Textbox1.text = Nothing instead of Textbox1.text=””
Below is the Small Code which i made for sample
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
TextBox1.Text = "123"
TestClass()
End Sub
Protected Shared Sub TestClass()
Dim MyF1 As New _Default
MyF1.TextBox1.Text = "ABC"
End Sub
TextBox1is an instance variable of your Page class, created (probably) by the ASP.NET designer.Thus, you cannot access it in static/shared context.
When ASP.NET receives a request, it:
In your scenario, you create an instance of the
_Defaultclass, but don’t set any of its properties.This results in the
Textbox1property being null and that’s why it throwsNullreferenceExceptionwhen used.There are two possible solutions:
TestClassmethod as instance method (drop theSharedkeyword)pass the current instance of
_Defaultas a parameter to the static/shared method, like so:The second solution requires that theTextBox1property should be declared aspublic.(sorry if there are any VB mistakes; I’m a C# programmer ☺)