|
-
Aug 31st, 2012, 05:53 PM
#1
Thread Starter
Member
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?
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
|