Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

I need some help figuring out syntax for functions in vb.net

So I have just made a test solution to play around and figure out how to return a list of values from a function in vb.net. But when I do so I only get heaps of errors. Here is My code:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Idk() As Integer = SolveMath()

    End Sub

    Function SolveMath() As Integer
        Dim Num1 As Integer = 23
        Dim Num2 As Integer = 88
        Dim Result1 As Integer = Num1 + Num2
        Dim Result2 As Integer = Num2 - Num1
        Dim ResultsList() As Integer = (Result1, Result2)
        Return ResultsList
    End Function
End Class

How do I write the code so that the list Idk() has Result1 And Result2 in it?

question from:https://stackoverflow.com/questions/65622762/i-need-some-help-figuring-out-syntax-for-functions-in-vb-net

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It would be more useful if you passed numbers to your function. I used 2 NumericUpDown controls to get the numbers.

The result should be visible in the immediate window.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Idk() As Integer = SolveMath(NumericUpDown1.Value, NumericUpDown2.Value)
    For Each i In Idk
        Debug.Print(i.ToString)
    Next
End Sub

Private Function SolveMath(Num1 As Integer, Num2 As Integer) As Integer() 'You are returning an array so add the ()
    Dim Result1 As Integer = Num1 + Num2
    Dim Result2 As Integer = Num2 - Num1
    'You can initialize an array easily by putting comma separated values of the correct type in braces.
    Dim ResultsList() = {Result1, Result2}
    Return ResultsList
End Function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...