3

I have just started to learn about OOP and I am wondering if it is possible to create objects using a list rather than an array. The list seems to have oodles of methods that are really useful and can be of an indeterminate length So, this is what I have

Class STUDENT
    'establish properties / members
    Public firstname As String
    Public surname As String
    Public DOB As Date
End Class

'declare a variable of the data type above to put aside memory
Dim students As List(Of STUDENT)

Sub Main()
    Dim selection As Char
    While selection <> "C"
        Console.WriteLine("Welcome to student database")
        Console.WriteLine("Number of students: " & students.Count)
        Console.WriteLine(" (A) Add a student")
        Console.WriteLine(" (B) View a student")
        Console.WriteLine(" (C) Quit")

        selection = Console.ReadLine.ToUpper

        If selection = "A" Then
            Console.Write("Please enter a firstname: ")
            students.firstname.add= Console.ReadLine
...etc
END While

This line is causing a problem

students.firstname.add= Console.ReadLine

I don't think this is how you would add an object using the list I set up. So how is it done?? Will the syntax need adjusting to add more than one item?

3
  • 1
    Five Minute Intro to Classes and Lists may be helpful. I'd use properties not fields in the class: Public Property firstname As String. Fields dont work the same as properties when it comes to binding Commented Jun 28, 2016 at 14:15
  • Thank you all. I think this will really help others too Commented Jun 28, 2016 at 17:11
  • Strictly speaking, a list is not of indeterminate length: you can determine the number of elements with list.Count(). A list has an automatically adjusted size. Commented Jun 28, 2016 at 17:14

1 Answer 1

3

There are multiple issues with this line: students.firstname.add= Console.ReadLine

Breaking it down we have:

students.firstname.add and add = Console.ReadLine

You need a student object first. students.firstname doesn't exist.

Dim tempStudent = New STUDENT()
tempStudent.firstname = Console.ReadLine()
' Other property assignments, etc

Once you have fully created your student object, you then add it to the list. Add is a method so we use parentheses:

students.Add(tempStudent)

Besides that there are a few casing errors which you should address.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I see. Thanks. Feel like I have the tools to really experiment now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.