如何在Private Function语句中定义多个值以返回一个值? [英] How do you define multiple values in Private Function statement to return one value?

查看:132
本文介绍了如何在Private Function语句中定义多个值以返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看一些代码并且不得不给自己一个速成课程...基本上我正在查看的代码中的所有内容都返回到脚本中的不同Catch As语句,这会引发自定义错误。用于刷新服务的脚本的主要功能确实有效,但由于Catch As语句而出现错误。我相信这句话会引发错误,因为Windows XP(有效)与Windows 7(不起作用)之间存在差异,无法提取系统信息。



我正在查看的代码如下:



 Private Function DetermineOSVersion(ByVal Computer As String)As String 
Dim OSVersion As String
Dim sysinfo As New RemoteSysInformation.RemoteSysInformation
Dim IPS As New IPS.IPS

试试
sysinfo.Get(计算机,IPS.QL,IPS.SL)
Catch ex As Exception
'lblStatus.Text = lblStatus.Text& 错误:& ex.ToString& <峰; br> 中
结束尝试

OSVersion = sysinfo.OSName

返回OSVersion
结束函数





从我能够研究/速成课程开始,我相信如果我可以更改该值以返回多个值,则可以解决此问题。所以我尝试按如下方式更改值:



私函数确定OSVersion(ByVal计算机为字符串)字符串
Dim OSVersion As String
Dim sysinfo,systeminfo As New RemoteSysInformation.RemoteSysInformation
Dim IPS As New IPS.IPS

尝试
sysinfo.Get(计算机,IPS.QL,IPS。 SL)

systeminfo.Get(计算机,IPS.QL,IPS.SL)
Catch ex As Exception
'lblStatus.Text = lblStatus.Text& 错误:& ex.ToString& <峰; br> 中
结束尝试

OSVersion = sysinfo.OSName

返回OSVersion
结束函数





结果是它崩溃了,我不得不拿出我做的编辑。经过一些额外的研究后,我现在认为如果我能够将OSVersion定义为返回sysinfo或systeminfo,这可以解决这个问题,但我不知道如何实现这一点。如果我的逻辑是错误的,有人可以提出别的建议或者指出我正确的方向,那将是值得赞赏的。

解决方案

首先,我不确定你应该使用字符串。这是当前常见的错误,使用代表数据的字符串而不是数据本身。



一般解决方案非常简单。是的,返回的对象只有一个。因此,您可以定义一些表示多个对象的 struct ,并返回此新定义类型的实例。这将是最干净的语义感知方式。



对于一些抽象/通用解决方案,您可以返回某个数组的实例或元组 https://msdn.microsoft.com/en-us /library/system.tuple%28v=vs.110%29.aspx [ ^ ]。



另一种方法是有几个 ByRef 用于输出的参数。我不推荐它。

在大多数情况下,我仍然建议使用你的自定义类型。



-SA

I have been looking at some code and have had to give myself a crash course... basically everything in the code that I am looking at comes back to a different "Catch As" statement in the script which throws a custom error. The main functionality of the script which is to refresh a service does work but an error appears because of the "Catch As" statement. I believe that this statement is throwing the error because there is a difference from Windows XP (which worked) to Windows 7 (which doesn't work) to pull system information.

The piece of code that I am looking at is as follows:

Private Function DetermineOSVersion(ByVal Computer As String) As String
       Dim OSVersion As String
       Dim sysinfo As New RemoteSysInformation.RemoteSysInformation
       Dim IPS As New IPS.IPS

       Try
           sysinfo.Get(Computer, IPS.QL, IPS.SL)
       Catch ex As Exception
           'lblStatus.Text = lblStatus.Text & "Error: " & ex.ToString & "<br>"
       End Try

       OSVersion = sysinfo.OSName

       Return OSVersion
   End Function



From what I have been able to research/crash course I believe that this issue could be resolved if I could change the value to return multiple values. So I tried to change the values as follows:

Private Function DetermineOSVersion(ByVal Computer As String) As String
    Dim OSVersion As String
    Dim sysinfo, systeminfo As New RemoteSysInformation.RemoteSysInformation
    Dim IPS As New IPS.IPS

    Try
        sysinfo.Get(Computer, IPS.QL, IPS.SL)
        Or
        systeminfo.Get(Computer, IPS.QL, IPS.SL)
    Catch ex As Exception
        'lblStatus.Text = lblStatus.Text & "Error: " & ex.ToString & "<br>"
    End Try

    OSVersion = sysinfo.OSName

    Return OSVersion
End Function



The results is that it crashed and I had to take out the edit that I made. After some additional research I now think that if I were able to define OSVersion to Return sysinfo or systeminfo that this could fix that issue, but I am not sure how to accomplish this. If my logic is wrong and someone could suggest something else or point me in the right direction that would be appreciated.

解决方案

First of all, I'm not sure you should work with strings. This is the common mistake these days, to work with strings representing data, instead of data itself.

General solution is very simple. Yes, returned object is only one. So, you can define some class or struct representing multiple objects and return the instance of this newly defined type. This would be the cleanest semantic-aware way.

For some abstract/universal solution, you can return the instance of some array or tuple: https://msdn.microsoft.com/en-us/library/system.tuple%28v=vs.110%29.aspx[^].

Another approach is having several ByRef parameters used for output. I would not recommend it.
In most cases, I would still recommend using your custom type.

—SA


这篇关于如何在Private Function语句中定义多个值以返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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