vb.net 调用 Sub 并返回一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9725646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:00:35  来源:igfitidea点击:

Calling a Sub and returning a value

vb.netvisual-studio

提问by Nefariis

This might seem like an insanely easy question, but I can't seem to find an answer anywhere to it. I'd like to think that I am decent at VB but while I was learning javascript the other day I found something that seemed awesome and now I can't figure out how to do it in VB.

这似乎是一个非常简单的问题,但我似乎无法在任何地方找到答案。我想认为我在 VB 方面很不错,但是前几天我在学习 javascript 时发现了一些看起来很棒的东西,现在我无法弄清楚如何在 VB 中做到这一点。

In javascript it looks like this:

在javascript中它看起来像这样:

var someValue = getThatValue()

var someValue = getThatValue()

It's both calling and setting the value from the getThatValue() sub. what is the VB equivalent?

它从 getThatValue() 子调用和设置值。什么是 VB 等价物?



Edit

编辑

I've tried doing this:

我试过这样做:

   private sub main()
       dim value = getValue()
       'do something with value
   end sub

   private sub getValue()
       return 3
   end sub

That doesn't seem to work, how can I get that to work?

这似乎不起作用,我怎样才能让它起作用?

回答by Kevin

Private Sub Main()
    Dim value = getValue()
    'do something with value
End Sub

Private Function getValue() As Integer
    Return 3
End Function

回答by George Filippakos

You should be using a Property:

你应该使用一个属性:

Private _myValue As String
Public Property MyValue As String
    Get
        Return _myValue
    End Get
    Set(value As String)
        _myValue = value
     End Set
End Property

Then use it like so:

然后像这样使用它:

MyValue = "Hello"
Console.write(MyValue)