简体   繁体   English

Visual Basic 2010-创建一个类,用户可以在其中输入变量,然后将其保存以供以后在表中引用

[英]Visual Basic 2010 - Create a class where the user can enter variables in then save it to reference in a table later

Unfortunately for me the book I brought on Visual Basic 2010 doesn't cover this and I can't find this solved anywhere else. 对我来说不幸的是,我在Visual Basic 2010上带来的这本书没有涵盖这一点,而且在其他任何地方也找不到解决的办法。

As part of my coursework I need to create a program and inside that program I need to have used 2 classes. 在我的课程中,我需要创建一个程序,并且在该程序中需要使用2个类。

The classes are very similar so if I can do one, I can do the other. 这些类非常相似,所以如果我可以做一个,我可以做另一个。 I have a form with lots of data entry boxes like when you fill in your information online. 我有一个包含大量数据输入框的表单,例如当您在线填写信息时。 You have name, address etc, all in different boxes and you click save and you're done. 您具有名称,地址等,都在不同的框中,然后单击“保存”就完成了。

I want to take all the different variables entered and save them into a class, because I'll be saving more than one I figured I'd need a database then each time I enter a new client the client ID needs to go up one, 0001, 0002 etc. Then I want to view and be able to sort the entries in a table on a separate form. 我想输入所有不同的变量并将其保存到一个类中,因为要保存的变量不止一个,我认为我需要一个数据库,所以每次输入新客户端时,客户端ID都需要增加一个, 0001、0002等。然后,我想查看表格并以单独的表格对条目进行排序。

Can anyone offer a step by step guide to do something like this. 任何人都可以提供逐步指导以执行类似操作。

If you could not use code and just try and explain it that would help me out a hell of a lot. 如果您不能使用代码,而只是尝试解释一下,那将对我有很大帮助。 I understand coding and stuff so you don't need to explain what for loops and iterations and code means. 我了解编码和内容,因此您无需解释循环和迭代的意义以及代码的含义。

Thank you so much in advance. 提前非常感谢您。

What you need to do: 你需要做什么:

  1. Create a class that has members all the information you want to retain. 创建一个包含您要保留的所有信息的成员的类。

  2. Whenever the user adds new information, create one such above mentioned object, populate it with the information entered and keep these objects in your program (eg an array, a list etc) This should take care of entering the data and maintaining them for the life span of the application. 每当用户添加新信息时,请创建一个上述对象,并​​使用输入的信息填充该对象,并将这些对象保留在您的程序中(例如数组,列表等)。这应注意输入数据并终身保存应用程序的范围。

  3. But you also need to save it into the database. 但是您还需要将其保存到数据库中。 I would use a separate object for that, one object that will take care of saving the data in the database in order to decouple the components. 为此,我将使用一个单独的对象,该对象将负责将数据保存在数据库中以使组件分离。 In this case, the new object will have a method that will take as input parameter one of the objects mentioned at point 1 and save the information into the database. 在这种情况下,新对象将具有一种方法,该方法将第1点提到的对象之一作为输入参数并将信息保存到数据库中。 Also, you could use some edit function, that will act pretty much the same but, instead of insert, will perform edit. 另外,您可以使用一些编辑功能,其功能几乎相同,但是将执行插入操作,而不是插入操作。 The same object will have an additional method for loading all the information from the database and populate the list - mentioned at point 2 - that you keep in your application You can extend this object also with delete functionality, if needed. 同一对象将具有从数据库加载所有信息并填充保留在应用程序中的列表(第2点提到)的其他方法。如果需要,还可以使用删除功能扩展该对象。

Good luck! 祝好运!

Your class needs to contain public properties relating to the fields you are enterring. 您的课程需要包含与您输入的字段相关的公共属性。

So you would have a Class called say Person . 因此,您将拥有一个名为say Person的课程。

Inside this class you would add Public Properties for each item you want to store, for instance; 例如,在该类中,您将为要存储的每个项目添加“公共属性”;

  • Name 名称
  • Address 地址
  • Telephone Number 电话号码
  • etc etc

You would then also add variables for each item you want to store, to the class Constructor . 然后,您还可以将要存储的每个项目的变量添加到类Constructor The constructor is the New Sub. 构造函数是New Sub。 This would allow you to create a new Person class, passing in the variables; 这将允许您创建一个新的Person类,并传入变量。

Dim MyNewPerson As New Person(txtName.Text, txtaddress.Text, txtTelephoneno.Text, etc, etc)

If you have a number of these objects, then you should use something like a List, which you can create using something like; 如果您有许多这样的对象,则应使用诸如List之类的东西,可以使用诸如List之类的东西来创建。

Dim MyPersonList As New List(Of Person)

And then you can simply add your MyNewPerson object from above, with something like; 然后您可以简单地从上方添加MyNewPerson对象,例如:

MyPersonList.Add(MyNewPerson)

Regarding adding this to a database, this is a far bigger subject, and here I'd suggest looking at Entity Framework Code First, as this is Microsofts recommended Data Access Framework; 关于将其添加到数据库中,这是一个更大的主题,在这里,我建议您先查看Entity Framework Code,因为这是Microsoft推荐的Data Access Framework。

http://msdn.microsoft.com/en-gb/data/ef.aspx http://msdn.microsoft.com/en-gb/data/ef.aspx

Incidentally, You DONT save the variables INTO the class, You structure your class so as to perform those functions. 顺便说一句,您不要将变量保存到类中,您可以构造类以执行那些功能。

So maybe your class maybe structured as... 因此,也许您的课程结构可能是...

Class MyClass

  Private mFileName as string
  Private mVariables as Dictionary(of TKey, TValue)


  'filename property 
  Property Filename() as string

  'variables collection as key value/pairs
  Property Variables as Dictionary(of TKey, TValue)

  'property accessor for single key/value from collection like Variables(2)
  Property VariableX(Index as integer) as string

  'function to load saved data from xml file
  Function LoadMySavedXmlData(Filename) as Dictionary(of TKey, TValue)

  'subroutine to save variables data as xml file
  Sub SaveMyXmlData(Filename as string, Data as Dictionary(of TKey, TValue))

End Class

And then your xml file maybe structured as follows 然后,您的xml文件的结构可能如下

<?xml version="1.0"?>
<MySavedData>
  <Filename>Bobs_Prefs.Xml</Filename>
  <Data>
    <Variable1>
      <Key>Name</Key>
      <Value>Bob Johnson</Value>
    </Variable1>
    <Variable2>
      <Key>Telephone</Key>
      <Value>0123 456 7896</Value>
    </Variable2>
    <Variable3>
      <Key>Car</Key>
      <Value>Chrysler</Value>
    </Variable3>
  </Data>
</MySavedData>

By using an XML file there is no limitation of having to STICK to predefined field names or field types, the user may type a number or string according to his/her variable type, for example a Color may be string like 'RED' or HEX like 'FF0000' 通过使用XML文件,没有必须要粘帖到预定义的字段名称或字段类型的限制,用户可以根据其变量类型键入数字或字符串,例如,颜色可以是类似于“ RED”或HEX的字符串就像“ FF0000”

This would be much simpler than dabbling into SQL and by asking for a file name when loading and saving you may save/load a particular one. 这比涉足SQL更为简单,并且在加载和保存时询问文件名可以保存/加载特定的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM