Results 1 to 4 of 4

Thread: Overloads

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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?

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    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:
    1. Public Class Student
    2.  
    3.     private _firstName as String
    4.     private _lastName as String
    5.     private _grade as Integer
    6.  
    7.     Public Sub New(pFName as String, pLName as String, pGrade as String)
    8.  
    9.         _firstName = pFName
    10.         _lastName = pLName
    11.         _grade = pGrade
    12.     End Sub
    13.  
    14.     Public Property FirstName as String
    15.         Get
    16.             Return _firstName
    17.         End Get
    18.         Set (value as String)
    19.             _firstName = value
    20.         End Set
    21.     End Property
    22.  
    23.     Public Property LastName as String
    24.         Get
    25.             Return _lastName
    26.         End Get
    27.         Set (value as String)
    28.             _lastName = value
    29.         End Set
    30.     End Property
    31.  
    32.     Public Property Grade as Integer
    33.         Get
    34.             Return _grade
    35.         End Get
    36.         Set (value as Integer)
    37.             _grade= value
    38.         End Set
    39.     End Property
    40.  
    41. End Class

    Then to create it, you'd do:

    vb Code:
    1. 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>.

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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"}

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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
  •  



Click Here to Expand Forum to Full Width