Results 1 to 5 of 5

Thread: Confused about instantiating classes

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    Confused about instantiating classes

    There is something confusing to me about classes. Suppose I have a form with a textbox and a button on it and I define a class as follows:

    Public Class Checktext
    Private Shared m_Title As String = "TextBox Error"

    Public Shared Property Title() As String
    Get
    Return m_Title
    End Get
    Set(ByVal value As String)
    m_Title = value
    End Set
    End Property
    Public Shared Function IsValid(ByVal control As Control) As Boolean
    If control.GetType.ToString = "System.Windows.Forms.TextBox" Then
    Dim textBox As TextBox = CType(control, TextBox)
    If textBox.Text = "" Then
    MessageBox.Show(textBox.Name.ToString & " cannot be empty.", Title)
    textBox.Select()
    Return False
    Else
    MessageBox.Show("That's better", Title)
    Return True
    End If
    Else
    MessageBox.Show("This is not a textbox", Title)
    Return False
    End If
    End Function

    End Class

    The button click method looks like this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Not Checktext.IsValid(TextBox1) Then
    TextBox1.Text = "Try again"
    End If
    End Sub

    But I never make an instance of the class. That's the entire project. So why does this code work?

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Confused about instantiating classes

    Shared members don't require an instance to use, more over you cannot use Shared members from an instance. e.g. the following code will cause a warning.
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim c As New Checktext
            If Not c.IsValid(TextBox1) Then ' this cause a warning
                TextBox1.Text = "Try again"
            End If
        End Sub
    Last edited by 4x2y; Aug 31st, 2012 at 07:48 PM.



  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    Re: Confused about instantiating classes

    Ah, OK, then this is like static?

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

    Re: Confused about instantiating classes

    Quote Originally Posted by franceint View Post
    Ah, OK, then this is like static?
    It is static.
    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

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

    Re: Confused about instantiating classes

    Quote Originally Posted by franceint View Post
    Ah, OK, then this is like static?
    Just note that there is a Static keyword in VB but it means something different. Shared in VB is equivalent to 'static' in C# or other C-based languages and Static in VB is used to make a local variable retain its value between method calls.

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