简体   繁体   English

用数据VB.net填充对象

[英]Populating Object with Data VB.net

I'm looking to populate an object then display the data to labels. 我正在寻找一个对象,然后将数据显示到标签上。

I've created a Student Class: 我创建了一个学生班:

Public Class student
    Public Dim sNum As Integer
    Public sName As String
   Public Sub New(ByVal sNum As Integer)
        MyBase.New()
        Me.sNum = sNum
    End Sub

I've got a database class that I want to use to populate this. 我有一个数据库类,我想用它来填充它。

Public Function populateStudent() As Object
        Dim ObjStudent As New student(1)
        ObjStudent.sName = "Bitz"
        Return ObjStudent
    End Function

Obviously this is just a step, eventually I'll be querying the database to populate the data, but I want to get this working first so I know I'm creating this correctly. 显然,这只是一个步骤,最终我将查询数据库以填充数据,但是我想首先使其正常工作,所以我知道我正确地创建了它。

In my main class attached to my .aspx I want to be able to do 在附加到.aspx的主类中,我希望能够

lblStudentName.Text = ObjStudent.sName

Am I going about this correctly, or is there a better way? 我是正确解决这个问题,还是有更好的方法?

You need not have 你不需要

   MyBase.New()

because you don't have a explicit base class. 因为您没有显式的基类。

The return type of populateStudent() of Object does not make much sense; Object的populateStudent()的返回类型没有多大意义; it should be either a list of Student if you are planning to return a collection of student after querying the db. 如果您打算在查询数据库后返回学生集合,则它应该是学生列表。 if you are planning on populating the view from this method itself, then it should be a Sub returning nothing and not a Function . 如果您打算从此方法本身填充视图,则它应该是不返回任何内容的Sub而不是Function

Otherwise everything else looks okay. 否则,其他一切看起来都还可以。

EDIT: Sounds like you need something like this. 编辑:听起来你需要这样的东西。

Public Function populateStudent(Id as String) As student
        Dim ObjStudent As New student(1)
        ObjStudent.sName = "Bitz"
        Return ObjStudent
    End Function

Close. 关。 You'll want to set the .Text property on the Label control: 您需要在Label控件上设置.Text属性:

lblStudentName.Text = ObjStudent.sName

(which you have since edited your question to contain... it often bothers me that SO doesn't show that something was edited if the edit is very soon after the initial post) (此后您已经编辑了要包含的问题...如果最初发布后不久就进行了编辑,这常常使我感到困扰,因此SO不会显示某些内容已被编辑)

As for a "better way" just remember that there are many, many ways to do just about anything. 至于“更好的方法”,请记住,有很多方法可以完成任何事情。 "Better" is very relative and depends on other factors not present in the code you have so far. “更好”是相对的,并且取决于到目前为止您的代码中没有的其他因素。 As of now, you have a method which returns an instance of an object (similar to the Factory pattern , feel free to research more on that and other patterns) and you use properties on that object to populate data fields in the UI. 到目前为止,您已经有了一个返回对象实例的方法(类似于Factory模式 ,可以随时研究该模式和其他模式),并使用该对象的属性填充UI中的数据字段。 Pretty straightforward, nothing wrong with it. 很简单,没有错。

As the system grows and the problem domain becomes more complex, there will be more definition of "good design" vs. "bad design." 随着系统的发展和问题域变得越来越复杂,“好设计”与“坏设计”的定义将会更多。 But in just getting started, this is perfectly fine. 但是,从入门开始,这是完全可以的。

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

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