使用列表添加并迭代类中的对象 [英] Use a list to add and iterate through objects in a class

查看:99
本文介绍了使用列表添加并迭代类中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


真的坚持这个,我是OOP的新手。我是新创建的一个类,并希望将每个对象存储在列表而不是数组中。列表将为我提供更多方法,我不必指定其大小。这就是我所拥有的:

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:

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





程序将运行并允许我存储,例如,2条记录,但是当我使用for each时(通过pre选项B),它只显示我输入的最后一条记录。



我尝试了什么:



无法在网上找到一个简单的例子,我可以调整或帮助我理解列表在这种情况下的工作方式。不能确定记录是否被覆盖我的每个循环都没有索引可以这么说



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

推荐答案

SlowCoach问道:
SlowCoach asked:

我想如果你没有输入一封信,事情将会出现严重错误,我可能会遇到一些问题,但你如何设想调查员或常数有帮助?我本以为以这种方式宣布它们,我还需要一个选择案例陈述或IF,ELSEIF Else陈述?请问您能解释一下吗?

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?

请回答我对第一个解决方案的评论,以回答这个问题。它认为它应该是非常明显的。



这不是真的有ifs或切换。但是......



即使那些开关如果严格来说,不一定要在那里。同样,这主要是代码的可维护性以及避免一次又一次地重复相同事情的方法。你明白有仿制药吗?字典和其他数据结构,其他抽象方式。根据经验,如果你看到ifs和切换太多,这是一个很好的迹象,表明你做错了什么,应该考虑一些更好的方法,更抽象。我们来看你的情况吧。如何触发某些操作以响应某些输入的字符串/字符?简单!您可以使用第一个通用参数key创建字典,使其成为字符串或char,将第二个值value创建为某种类型的委托实例。仅仅构建一次这样的机制就足够了。我关于该主题的文章解释了这一切: 动态方法调度程序



现在,我建议你再做一步,想一些与这个问题没有关系的东西。为什么你有那些A,B和C键呢?因为您尝试创建基于交互式控制台的应用程序。这是个好主意吗?但作为一个经验法则。查看众所周知的仅限控制台的应用程序。几乎所有人都没有交互式输入(一些排除适用,但其中很少有)。为什么这样?首先,因为这样的应用非常不方便。犯一点错误,你必须再次输入数据。相反,此类应用程序通常基于命令行参数。命令行可能比较长,但是当你犯错时,错误的成本非常低:大多数应用程序使用命令行启动其他应用程序,命令解释器(如CMD.EXE,但还有更多的指挥官) )总是给你快速重复最后命令的可能性。如果你犯了错误,你只需要修一两个字符。我的另一篇文章提出了一种非常方便,易于维护,简单易用的解析命令行的方法: 基于枚举的命令行实用程序 (顺便说一下,再次列举)。不需要基于交互式控制台的应用程序,它们太可怕而不方便。



最后,对于更复杂的输入,图形用户界面可能是最好的。



-SA

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


你总是在中保留对象的引用tempStudent

所以你不创建一个新的,你只更改同一个对象的Propertys





移动

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
Dim tempStudent As New STUDENT()





到这里





to here

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.


这篇关于使用列表添加并迭代类中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆