|
-
Mar 20th, 2008, 01:06 PM
#1
Thread Starter
Hyperactive Member
Overloads
I want to be able to create a new Student in my TeacherGradebook program. My Student class has lots of fields like .FirstName, .MiddleName, .LastName, .HomePhone, .WorkPhone, .Grade(Quarter,GradeIndex).Value, etc.
I'm thinking about using a collection of students (rather than a static-sized array)
When I create a new Student, I'd like to do something like this....
dim s as new Student = {"Joe","Bob","Jones",,,,,} 'or whatever the correct syntax is
Do I need to do Overloads in a New sub within my Student Class or what?
-
Mar 20th, 2008, 01:15 PM
#2
Re: Overloads
You just put parameters into the New method and then set those parameters equal to the values.
So, say your student has properties called FirstName, LastName and Grade and you want to populate those when you create the object.
The syntax would be like:
vb Code:
Public Class Student
private _firstName as String
private _lastName as String
private _grade as Integer
Public Sub New(pFName as String, pLName as String, pGrade as String)
_firstName = pFName
_lastName = pLName
_grade = pGrade
End Sub
Public Property FirstName as String
Get
Return _firstName
End Get
Set (value as String)
_firstName = value
End Set
End Property
Public Property LastName as String
Get
Return _lastName
End Get
Set (value as String)
_lastName = value
End Set
End Property
Public Property Grade as Integer
Get
Return _grade
End Get
Set (value as Integer)
_grade= value
End Set
End Property
End Class
Then to create it, you'd do:
vb Code:
Dim s as New Student("Bob", "Jones", 75)
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Mar 20th, 2008, 01:22 PM
#3
Re: Overloads
What version of .NET is your program written in?
One of the cool new features in .NET 3.5 is object initilizers, which means you don't need to overload the constructor just to create a class with specific values from the start.
This can be BETTER than created an overloaded constructor, because you can specify any number of public properties in the class to set.
In VB9 (2008) this statement is valid, and creates a new student object, with the first and last name values set..
Code:
Dim myStudent As New Student With {.firstName = "Bob", .lastName = "Smith"}
-
Mar 20th, 2008, 02:10 PM
#4
Re: Overloads
You can write a function that take an array of student names and returns a collection of Student objects. Pretty simple. Something like this:
Code:
Public Shared CreateStudentList(ByVal studentNames() As String) As List(Of Student)
Dim studentList As New List(Of Student)
For Each name As String in studentNames
studentList.Add(New Student(name))
Next
Return studentList
End Function
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
|