Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Really stuck with this and I am new to OOP. I'm new have created a class and want to store each object in a list rather than an array. A list will give me more methods to play with and I don't have to specify its size. This is what I have:
VB.NET
Public 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 New List(Of STUDENT)
   Dim tempStudent As New STUDENT()


   Sub Main()
       Dim selection As Char
       While selection <> "C"
           Console.WriteLine("Welcome to student database")
           Console.WriteLine("Number of tempStudent: " & 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: ")
               tempStudent.firstname = Console.ReadLine()

               Console.Write("Please enter a surname: ")
               tempStudent.surname = Console.ReadLine()

               Console.Write("Please enter a date of birth: ")
               tempStudent.DOB = Console.ReadLine()

               students.Add(tempStudent)
               Console.WriteLine("Student entered!")
               Console.ReadLine()
               Console.Clear()


           ElseIf selection = "B" Then

               For Each temp In students
                   Console.WriteLine("Student firstname: " & temp.firstname)
                   Console.WriteLine("Student surname: " & temp.surname)
                   Console.WriteLine("Student date of birth: " & temp.DOB)
                   Console.ReadLine()
               Next
               Console.Clear()
           Else
               'Quit the application
               End

           End If
       End While


The program will run and will allow me to store, for example, 2 records, but when I use the for each (by pressing option B), it shows only shows the last record I typed in.

What I have tried:

Cant find a simple example online that I could adapt or help me understand how the list would work in this context. Cant be sure if the record is being overwritten of it my for each loop has not index so to speak
Posted
Updated 29-Jun-16 11:42am

you always keep the reference to your object in tempStudent
so you don't create a new one, you only change the Propertys of the same Object


move that
VB.NET
Dim tempStudent As New STUDENT()


to here

VB.NET
If selection = "A" Then
     Dim tempStudent As New STUDENT()
     Console.Write("Please enter a firstname: ")


so you create a new Object each time when adding a new Student.
 
Share this answer
 
Comments
SlowCoach 29-Jun-16 6:59am    
F.Xaver Arrrrghhhh. Thank you so much. I've spent hours on this. Really grateful. Hopefully someone out there will wonder the same thing and get a starting point
Sergey Alexandrovich Kryukov 29-Jun-16 11:16am    
Good catch, a 5.
The inquirer's code has many problems; one of them is hard-coded "A", "B", "C" and the like. This is not maintainable. One solution would be using enumeration type, or at least a set of explicitly defined constants, instead of those immediate constants. The code should be neat, nothing should be repeated, and so on...
—SA
SlowCoach 29-Jun-16 13:10pm    
I guess if you don't enter a letter, things will go horribly wrong and I probably have a couple of casting issues, but how do you envisage enumerators or constants as helping? I would have thought that having declared them in this manner, i'd still need a select case statement or IF, ELSEIF Else statements? Please could you explain?
Sergey Alexandrovich Kryukov 29-Jun-16 17:23pm    
Isn't that obvious? You define it once in one place, never repeat. Each enumeration value is converted to string via .ToString() or .ToString().ToUpper(), depending on what you want. It makes code maintainable. Besides, you can have a data structure directly associating input with actions.
—SA
Sergey Alexandrovich Kryukov 29-Jun-16 17:43pm    
I answered in detail in Solution 2, please see.
—SA
SlowCoach asked:
I guess if you don't enter a letter, things will go horribly wrong and I probably have a couple of casting issues, but how do you envisage enumerators or constants as helping? I would have thought that having declared them in this manner, I'd still need a select case statement or IF, ELSEIF Else statements? Please could you explain?
Please see my comment to solution one, in response to this question. It thought it should be quite obvious.

It's not really about having "ifs" or "switched". But…

Even those "switch" of "if", strictly speaking, does not have to be there. Again, this is all mostly the matter of maintainability of code and the ways you can avoid repeating the same things again and again. Do you understand that there are generics? dictionaries and other data structures, other means of abstraction. As a rule of thumb, if you see too much if "ifs" and "switched", this is a good indication that you are doing something wrong and should think about some better ways, more abstracted. Let's take your case. How can you trigger some actions in response to some entered string/character? Easy! You can create dictionary, with first generic parameter, key, to become a string or a char, and the second one, value, to become a delegate instance of certain type. It's enough to build such mechanism just once. My article on the topic explains it all: Dynamic Method Dispatcher.

Now, I would suggest you to make one more step and think of something not really related to this issue. Why do you have those "A", "B" and "C" keys at all? Because you try to create an interactive console-based application. Is it a good idea? As a rule of thumb, but. Look at the well-known console-only applications. Almost all of them have no interactive input at all (some exclusion apply, but there are very few of them). Why so? First of all, because such applications are very inconvenient. Make a little mistake and you have to enter data again. Instead, such applications are usually based on command-line parameters. The command line can be relatively long, but when you do a mistakes, the cost of mistake is very low: most applications starting other applications with command line, command interpreters (such as CMD.EXE, but there are a lot more "commanders") always give you the possibility to quickly repeat last commands. You just fix a character or two, if you make a mistake. My other article suggests a very convenient, well maintainable, simple and easy-to-use way of parsing command line: Enumeration-based Command Line Utility (enumerations again, by the way). Interactive console-based applications are just not needed, they are too dreadful to be convenient.

Finally, for more complicated input, the graphical UI can be the best.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900