As fascinating as it might be to make a page display "Hello world," eventually your classes need to do something with data you provide and then return new data. You'll find that there are different ways to do the same thing.Chapter 2, "Classes: The Code Behind the Objects," you can get data in and out through a combination of parameters passed into methods, method return values, and properties. Listing 3.1 shows a class that uses only properties, with no parameters or return values for the methods. Listing 3.2 shows how you would call this class.
C#
public class SomeClass { public void MyMethod() { _myName = "new name"; _myAge = 12; } private string _myName; public string MyName { get { return _myName; } set { _myName = value; } } private int _myAge; public int MyAge { get { return _myAge; } set { _myAge = value; } } }
VB.NET
Public Class SomeClass Public Sub MyMethod() _myName = "new name" _myAge = 12 End Sub Private _myName As String Public Property MyName() As String Get Return _myName End Get Set _myName = value End Set End Property Private _myAge As Integer Public Property MyAge() As Integer Get Return _myAge End Get Set _myAge = value End Set End Property End Class
C#
SomeClass ThisClass = new SomeClass(); ThisClass.MyName = "Jeff"; ThisClass.MyAge = 30; ThisClass.MyMethod(); Trace.Warn(ThisClass.MyName); // outputs "new name" Trace.Warn(ThisClass.MyAge.ToString()); // outputs "12"
VB.NET
Dim ThisClass As New SomeClass() ThisClass.MyName = "Jeff" ThisClass.MyAge = 30 ThisClass.MyMethod() Trace.Warn(ThisClass.MyName) ' outputs "new name" Trace.Warn(ThisClass.MyAge.ToString()) ' outputs "12"
We can rewrite this code to a similar class that doesn't use properties at all but arrives at a similar result, as shown in Listing 3.3 and Listing 3.4.
C#
public class SomeClass { public string MyMethod(string oldName) { return "new name"; } public int AnotherMethod(int oldAge) { return 12; } }
VB.NET
Public Class SomeClass Public Function MyMethod(oldName As String) As String Return "new name" End Function Public Function AnotherMethod(oldAge As Integer) As Integer Return 12 End Function End Class
C#
SomeClass ThisClass = new SomeClass(); Trace.Warn(ThisClass.MyMethod("Jeff")); // outputs "new name" Trace.Warn(ThisClass.AnotherMethod(30).ToString()); // outputs "12"
VB.NET
Dim ThisClass As New SomeClass() Trace.Warn(ThisClass.MyMethod("Jeff")) ' outputs "new name" Trace.Warn(ThisClass.AnotherMethod(30).ToString()) ' outputs "12"
I know what you're thinking"Wow, the second version sure looks easier. I'm going to do things that way!" Stop and think about it first because these classes don't do much of anything exciting. Imagine now that these classes have a dozen different methods that need to manipulate the same data. In the first example, you'd need to assign the data once to the properties and then execute your various methods. In the second example, you would need to pass in that data on every single method. Not a good use of your time! What's worse is that you can't get a number of different values out of the same method because the method can only return one value. That's why we have two separate methods to alter the name and age values in our example.
Another way to introduce data to the instantiated class right off the bat is to pass in values to the constructor of the class, as in Listing 3.5. Listing 3.6 shows how to pass in the data when you create a class instance. Your constructors can have parameters, just as a method can. You can also overload constructors, just as you would any other method.
C#
public class SomeClass { public SomeClass(string someString) { // do something here with someString } }
VB.NET
Public Class SomeClass Public Sub New(someString As String) ' do something here with someString End Sub End Class
C#
SomeClass ThisClass = new SomeClass("my string");
VB.NET
Dim ThisClass As New SomeClass("my string")