Chapter 5: Creating and Building Projects

127

If you’re using C# when you do this, VS will ask if you want to change the class filename from Class1 to Student. VB will make the class name change automatically, without asking. This is a convenient way to keep your classes and filenames in sync. It is common to create only one class per file. Listing 5-1 shows the new student file after renaming and adding code to make it functional.

Listing 5-1 Class library code

C#:

using System;

using System.Collections.Generic; using System.Linq;

using System.Text;

namespace ClassLibraryDemo

{

public class Student

{

public List<int> GetStudentGrades(string studentName)

{

return new List<int> { 80, 100, 95 };

}

}

}

VB:

Public Class Student

Public Function GetStudentGrades(ByVal studenName As String) As List(Of Integer)

Dim intList As New List(Of Integer) intList.Add(80) intList.Add(100) intList.Add(95)

Return intList End Function

End Class

The important parts of Listing 5-1, for the current discussion, is that Student is a class inside of the ClassLibraryDemo namespace. You’ll need to remember the namespace so that you can obtain a reference to a Student instance from the calling code. Listing 5-2 shows how. Remember that the VB namespace is implicitly set to whatever is defined as the namespace setting on the My Project page, which defaults to the project name.

Page 150
Image 150
Microsoft 9GD00001 manual 127, Listing 5-1 Class library code