Hi there,
I think what's confusing you is the difference between Shared and Instance methods. Consider this (fake) example:
Code:
Public Class Messagebox
Public Shared Sub SharedShow()
End Sub
Public Sub InstanceShow()
End Sub
End Class
Notice the Shared keyword on
the MessageBox class the SharedShow method. Shared means the method applies to the entire class definition, and not to an instance of a class. As such, you can do this:
MessageBox.SharedShow()
Shared methods are great for utility methods that don't need their own state. They are convenient as you don't have to create an instance of the class first. The built-in MessageBox class is a great example of that: there's no need to force you to write more code and instantiate a class if you can just call a Shared method that pops up a message box.
Now, consider the second method which is not shared. In order to call it, you need to create an instance first:
Code:
Dim myBox As New Messagebox()
myBox.InstanceShow()
Here, InstanceShow is a method that can be called on an instance of MessageBox, and as such you have to declare and instantiate one first.
And to answer your answer questions:
Quote:
|
Why would you need MessageBox.Show anyway? What else are you going to do with it?
|
In this example, not much. The only useful shared method on MessageBox is Show. However, it has 21 overloads that help you show all kind of different messageboxes. Check the IntelliSense list for the Show method for more details.
Code:
Continuing the above theme, there is intNumber.ToString. Again, Why?
Assuming intNumber is an Integer, this class gets its method from Object. All objects in .NET inherit Object (except for Object itself). On the Object class, the ToString method is defined. It's default behavior (if not overriden by a child class) is to say its name. However, for Integer, the ToString mthod (inheirited from Object) has been overriden to return itself (the number) as a String. Here, ToString() is an instance method and as such you need an instance of the Integer (intNumber).
This works:
intNumber.ToString()
but this doesn't:
Integer.ToString()
A Shared method wouldn't make sense here as the object has its own internal state (the number). You don't want all Integers in your application to return the same number ;-)
Quote:
|
Again continuing the above theme system.strings.Left(StrName,3).
|
I don't understand this one, as the way it's written now, it doesn't compile. What you normally would do is call an instance method on the string:
Dim strName = "Imar"
Dim subString As String = strName.Substring(0, 3)
MessageBox.Show(subString)
This would display the text Ima. Maybe in this example, Left was used to show how you can still use the old
VB methods such as MsgBox and Left?
Hope this clarifies things a bit.
Cheers,
Imar