Results 1 to 13 of 13

Thread: [RESOLVED] How To Define Variables In Public Class. VB.NET

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2021
    Posts
    17

    Resolved [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 ?

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    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)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How To Define Variables In Public Class. VB.NET

    Quote Originally Posted by Delaney View Post
    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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2021
    Posts
    17

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2021
    Posts
    17

    Resolved Re: How To Define Variables In Public Class. VB.NET

    thank you very much
    it is work

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How To Define Variables In Public Class. VB.NET

    Quote Originally Posted by Niya View Post
    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.

  8. #8
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: How To Define Variables In Public Class. VB.NET

    Quote Originally Posted by jmcilhinney View Post
    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)

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: How To Define Variables In Public Class. VB.NET

    Quote Originally Posted by jmcilhinney View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How To Define Variables In Public Class. VB.NET

    Quote Originally Posted by Niya View Post
    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.

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Nov 2021
    Posts
    17

    Re: How To Define Variables In Public Class. VB.NET

    thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width