简体   繁体   English

在 VB.net 中使用类

[英]Using classes in VB.net

Excuse me if i sound like a complete noob or use improper terminology below.如果我听起来像一个完全的菜鸟或在下面使用不正确的术语,请原谅我。

I am trying to create a class in vb.net from which multiple form class can inherit from.我正在尝试在 vb.net 中创建一个类,多个表单类可以从中继承。 So in the 'form class' if i say, get a list from the user, sent it to the 'seperate class' to order the list and then pass the list back to the form class to save in a database or do whatever with how would i proceed.因此,在“表单类”中,如果我说,从用户那里获取列表,将其发送到“单独的类”以对列表进行排序,然后将列表传递回表单类以保存在数据库中或执行任何操作我会继续吗?

So far this is what i have done.到目前为止,这就是我所做的。

below is the class itself without the contents下面是没有内容的类本身

Public Class RandomClassName

End Class

Below is what i have done to 'inherit' from the above class以下是我为“继承”上述课程所做的工作

Public Class frmStudentLogin
Dim con As New OleDbConnection
Dim ValidUserNameLength As Boolean
Inherits RandomClassName

I would like to know how i would set up the code to allow me to properly inherit from and refer to the class link.我想知道如何设置代码以允许我正确继承和引用类链接。

Thanks.谢谢。

It is not clear which problem are you trying to solve with this inheritance approach, but surely you could inherit a form class (MyForm) from another class (BaseForm) provided that the other class (BaseForm) inherits from the form base class (Form).不清楚你想用这种继承方法解决哪个问题,但你肯定可以从另一个类(BaseForm)继承一个表单类(MyForm),前提是另一个类(BaseForm)从表单基类(Form)继承.

At that point you could insert in the MyForm all the function that you want to share between your forms but be aware that, in this scenario, every form will be affected by your BaseForm class此时,您可以在 MyForm 中插入您想要在表单之间共享的所有函数,但请注意,在这种情况下,每个表单都会受到 BaseForm 类的影响

I will show an example tested in LinqPAD我将展示一个在 LinqPAD 中测试的示例

Sub Main

    Dim x = New MyForm()
    x.Show()

End Sub

Public Class MyForm
    Inherits BaseForm

    Public Sub New ()
        Dim txt = New TextBox()
        txt.Multiline = True
        txt.Height = 200

        'Here you could use the GetAList from the BaseForm class
        ' and use it to set the initial text for the multiline textbox.
        txt.AppendText (String.Join(Environment.NewLine, GetAList().ToArray()))
        Me.Controls.Add(txt)
    End Sub
End Class


Public Class BaseForm
    Inherits Form

    ' Common methods required by your uplevel forms
    Public Function GetAList() As List(Of String)
        Dim a = New List(Of String)() From
            {"ABC", "DEF", "GHJ"}
        Return a
    End Function

End Class

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

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