【问题标题】:property return sevral value属性返回多个值
【发布时间】:2016-11-12 16:11:23
【问题描述】:

我想从一个只读属性中获取几个值。下面是我的代码

Public Class Class1
            ReadOnly Property Ca As New Class2
End Class





Public Class Class2

        ReadOnly Property getass(q As Integer, ww As String) As Integer
            Get
                Codes that return q And ww

            End Get

        End Property
End Class


Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim a As New Class1
        Dim ret As Integer
        Dim qq As Integer = Nothing
        Dim qqq As String = Nothing
        ret = a.Ca.getass(qq, qqq)
    End Sub
End Class

我想最终得到 qq=q 和 qqq=ww... 谢谢

【问题讨论】:

  • “我想要”不是一个问题,也不会告诉我们您是否遇到错误、错误是什么以及它在哪里。请仔细阅读How to Ask,也请收下Tour
  • 如何从只读属性中获取几个值,如上面的代码
  • in真的我想通过ret=a.ca.getass(qq,qqq)得到qq和qqq,qq在学生的ID和qqq是学生的姓。如果qq和qqq得到到数据库并返回值 ret=0 否则 ret =1。

标签: vb.net oop readonly


【解决方案1】:

您不想为此目的使用属性。

而只是声明一个 Sub 来修改传递的参数,如下所示:

Public Class Class2
    Sub getass(ByRef q As Integer, ByRef ww As String)
        Dim _q as Integer
        Dim _w as String

        'do whatever you want 
        'then assign the final values and end sub
        q = _q
        ww = _ww
    End Sub
End Class

然后像这样使用子:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim a As New Class1
        Dim qq As Integer = Nothing
        Dim qqq As String = Nothing
        a.Ca.getass(qq, qqq)
        'At this point your local qq and qqq will have the value setted by the getass Sub
    End Sub
End Class

请考虑到这对于您拥有学生 ID 和姓名的最终意图来说不是一个好的设计模式。

考虑使用您想要的所有属性创建一个“学生”类,并使 Class2(我可以假设是教室或类似的东西)返回一个“学生”对象。

或者你可以使用KeyValuePair 结构

编辑: 如果您仍想通过界面执行此操作,请尝试以下操作:

Public Class Class2
    Public ReadOnly Property getass(ByRef q As Integer, ByRef ww As String) as Integer
      Get
        Dim _q as Integer
        Dim _w as String

        'do whatever you want 
        'then assign the final values and end sub
        q = _q
        ww = _ww

        return ID 'ID is what you want (Integer)
      End Get
    End Property
End Class

【讨论】:

  • 非常感谢...如果我想 ret 返回一个类似 0 的值,表明代码可以访问数据库并返回值我该怎么办?
  • 对不起,我听不懂你的英文
  • 是的,你是对的。我的英语很弱。如果运行成功,我想要 ret (ret = a.Ca.getass(qq, qqq)) 我上面的代码返回 0。
  • @MDA 如果要返回整数,请将方法设为 Function (Private Function getass(ByRef q As Integer, ByRef ww As String) As Integer) 而不是 Sub,您可以使用 Return 语句返回整数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多