-
Nov 27th, 2021, 04:38 AM
#1
Thread Starter
Junior Member
[RESOLVED] How To Define Variables In Public Class. VB.NET
hello everyone
I have defined 3 variables .
And 3 variables have a values.
And their value are values contained within the Text Boxes.
I try to define they in public class.
but there is error
Code :
Code:
Public Class frmSignIn
Public strEmail As String = txtEmail.Text
Public strUserName As String = txtUserName.Text
Public strPassword As String = txtPassword.Text
and the error is
"An exception of type 'System.NullReferenceException' occurred in SignInLogIn.exe but was not handled in user code
Additional information: Object reference not set to an instance of an object."
What is the problem ?
how can I solve this problem ?
-
Nov 27th, 2021, 06:53 AM
#2
Re: How To Define Variables In Public Class. VB.NET
it is because the textboxs are not yet "created" at this point of the code when the compilation occurs.
you should fill your variables in the load event of your form
Code:
Public strEmail As String
Public strUserName As String
Public strPassword As String
Private Sub frmSignIn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
strEmail = txtEmail.Text
strUserName = txtUserName.Text
strPassword = txtPassword.Text
End Sub
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Nov 27th, 2021, 08:21 AM
#3
Re: How To Define Variables In Public Class. VB.NET
Originally Posted by Delaney
it is because the textboxs are not yet "created" at this point of the code when the compilation occurs.
you should fill your variables in the load event of your form
Code:
Public strEmail As String
Public strUserName As String
Public strPassword As String
Private Sub frmSignIn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
strEmail = txtEmail.Text
strUserName = txtUserName.Text
strPassword = txtPassword.Text
End Sub
While that will at least not throw an exception, there's no point to getting data from a control in the Load event handler because the user can't have entered any data before the form has been displayed. There's no point trying to get text from a TextBox until the user has actually entered text into that TextBox. The code should probably be in the Click event handler of a Button or maybe the TextChanged event handler of the individual TextBoxes.
-
Nov 27th, 2021, 08:44 AM
#4
Thread Starter
Junior Member
Re: How To Define Variables In Public Class. VB.NET
In my program
user enter the informations in Text Boxes.
and my program put the values in Variables.
Code that puts values into variables happens when a button is clicked.
-
Nov 27th, 2021, 08:48 AM
#5
Thread Starter
Junior Member
-
Nov 27th, 2021, 08:49 AM
#6
Re: How To Define Variables In Public Class. VB.NET
Something about the OP's question doesn't make sense. Let's assume the most common thing, that he placed these TextBoxes on the Form at design time. This would mean he shouldn't be getting null reference exceptions when trying to reference them because the designer code would have already created the TextBoxes by this time. We don't have enough information.
-
Nov 27th, 2021, 09:13 AM
#7
Re: How To Define Variables In Public Class. VB.NET
Originally Posted by Niya
Let's assume the most common thing, that he placed these TextBoxes on the Form at design time. This would mean he shouldn't be getting null reference exceptions when trying to reference them because the designer code would have already created the TextBoxes by this time.
Nope. The field declarations and their initialising expressions are executed before the class constructor, so before the InitializeComponent method is executed, so before the TextBoxes are created.
-
Nov 27th, 2021, 09:19 AM
#8
Re: How To Define Variables In Public Class. VB.NET
Originally Posted by jmcilhinney
While that will at least not throw an exception, there's no point to getting data from a control in the Load event handler because the user can't have entered any data before the form has been displayed. There's no point trying to get text from a TextBox until the user has actually entered text into that TextBox. The code should probably be in the Click event handler of a Button or maybe the TextChanged event handler of the individual TextBoxes.
I agree but the OP may have defined, in the designer, default text for the textbox. But after some thought, I think The OP believes that he has "linked" by that way both variables (public one and textbox) so when he change the textbox text, the public variable automatically update but it is not the case.
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Nov 27th, 2021, 09:21 AM
#9
Re: How To Define Variables In Public Class. VB.NET
Originally Posted by jmcilhinney
Nope. The field declarations and their initialising expressions are executed before the class constructor, so before the InitializeComponent method is executed, so before the TextBoxes are created.
Oh you're right I didn't notice that the class was a Form. I thought it was a generic data class.
Code:
Public Class frmSignIn
Public strEmail As String = txtEmail.Text
Public strUserName As String = txtUserName.Text
Public strPassword As String = txtPassword.Text
I didn't notice the frm in the class name.
-
Nov 27th, 2021, 09:28 AM
#10
Re: How To Define Variables In Public Class. VB.NET
I recommend this approach:-
Code:
Public Class frmSignIn
Public Property UserName As String
Get
Return Me.txtUserName.Text
End Get
Set(value As String)
Me.txtUserName.Text = value
End Set
End Property
Public Property Email As String
Get
Return Me.txtEmail.Text
End Get
Set(value As String)
Me.txtEmail.Text = value
End Set
End Property
Public Property Password As String
Get
Return Me.txtPassword.Text
End Get
Set(value As String)
Me.txtPassword.Text = value
End Set
End Property
End Class
-
Nov 27th, 2021, 09:46 AM
#11
Re: How To Define Variables In Public Class. VB.NET
Originally Posted by Niya
I recommend this approach:-
Code:
Public Class frmSignIn
Public Property UserName As String
Get
Return Me.txtUserName.Text
End Get
Set(value As String)
Me.txtUserName.Text = value
End Set
End Property
Public Property Email As String
Get
Return Me.txtEmail.Text
End Get
Set(value As String)
Me.txtEmail.Text = value
End Set
End Property
Public Property Password As String
Get
Return Me.txtPassword.Text
End Get
Set(value As String)
Me.txtPassword.Text = value
End Set
End Property
End Class
I'm guessing that, in this case, ReadOnly properties would be more appropriate, as the values are probably being read from outside the form but not set.
-
Nov 27th, 2021, 09:50 AM
#12
Re: How To Define Variables In Public Class. VB.NET
In that case, only a minor modification is needed, just remove the Set blocks and add ReadOnly modifiers:-
Code:
Public ReadOnly Property UserName As String
Get
Return Me.txtUserName.Text
End Get
End Property
-
Dec 1st, 2021, 12:07 PM
#13
Thread Starter
Junior Member
Re: How To Define Variables In Public Class. VB.NET
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
|