2017-05-07 142 views
-1

我寫了一個簡單的程序來計算線性序列中的前5個數字。該程序最初是順序涉及Main()中的一個子來執行計算。然而,作爲一個相對的編程初學者,我試圖發展我對面向對象編程的理解,因此,作爲一項挑戰,我決定相應地重新編寫程序。正如你將從下面的代碼中看到的,我遇到了一個小問題,我希望你們中的一些人能夠幫助我。Public Function vs Public Sub

通過使用公共子我可以通過在Main()中創建類的新實例並調用GetSequence來執行所需的任務...沒有問題。但是,我不喜歡在Class中使用Console.Write行的想法。糾正我,如果我錯了,但這不會否定創建類的真正目的,因爲它會消除其動態功能?

因此,作爲替代方法,我試圖在公共函數中執行相同的計算,但不幸的是,在for循環中無法識別返回值。我確信有一種解決方法......我錯過了一些顯而易見的東西,但我看不到它。

任何幫助,一如既往,最讚賞。

Public Class Sequence 
    Public Property a As Integer 
    Public Property b As Integer 

    'Using Public Sub does the job, but it means using a 
    'Console.Write within the Sequence class, which I'm 
    'not too happy about. 

    Public Sub GetSequence() 

    For n = 1 To 5 
     Console.Write("{0}, ", a * n + b) 
    Next 
    End Sub 

    'I'd prefer to use a Public Function, but how do I 
    'return the value of the function through each iteration 
    'of the 'for' loop? 

    Public Function GetSequence(ByVal a As Integer, ByVal b As Integer) 

     For n = 0 To 5 
      Return a * n + b 
     Next 
    End Function 

    Public Sub New(ByVal a As Integer, ByVal b As Integer) 
     Me.a = a 
     Me.b = b 
    End Sub 

    End Class 

回答

0

對於這個小例子,你可以創建一個字符串,並多次追加到它(也確保聲明函數的返回類型 - 使Option Strict項目範圍):

Public Function GetSequence(a As Integer, b As Integer) As String 
    Dim result As String = "" 

    For n = 0 To 5 
     result &= (a * n + b) & ", " 
    Next 

    Return result 
End Function 

LINQ是更好,但也許比你現在想要進入的更先進。

Public Function GetSequence(a As Integer, b As Integer) As String 
    Return String.Join(
     ", ", 
     From n In Enumerable.Range(0, 6) Select (a * n + b).ToString() 
    ) 
End Function 
+0

感謝您的幫助瑞恩。正如在前面的評論中提到的那樣,解決問題的新概念對於初學者來說非常重要。我沒有遇到你提到的'選擇嚴格'程序,但這就是學習的內容。我會研究它,並再次感謝瑞恩,最感謝。 – Mike

2

您不妨使用yield語句 - 看看這個鏈接:

https://docs.microsoft.com/en-us/dotnet/articles/visual-basic/language-reference/statements/yield-statement

給出的示例代碼如下:

Sub Main() 
    For Each number In Power(2, 8) 
     Console.Write(number & " ") 
    Next 
    ' Output: 2 4 8 16 32 64 128 256 
    Console.ReadKey() 
End Sub 

Private Iterator Function Power(
ByVal base As Integer, ByVal highExponent As Integer) _ 
As System.Collections.Generic.IEnumerable(Of Integer) 

    Dim result = 1 

    For counter = 1 To highExponent 
     result = result * base 
     Yield result 
    Next 
End Function 

注意,對於每個循環都調用該函數,該函數被定義爲一個迭代器,從而可以將該值返回給每個生成結果的For Each。

+0

感謝您的幫助jlmt。 Yield語句是我以前沒有遇到過的概念,所以謝謝你向我介紹。我會看看它,並有一個遊戲。正如我所提到的,我是一個相對的初學者,所以在現有基礎上構建對我來說非常重要。非常感謝。 – Mike

0

所以你想要一個「序列」是一個實體,並且你希望能夠從外部枚舉這個實體的值。

這樣做將實現IEnumerable接口的慣用方式:

Public Class Sequence 
    Implements IEnumerable(Of Integer) 

    Public ReadOnly Property a As Integer 
    Public ReadOnly Property b As Integer 

    Public Sub New(ByVal a As Integer, ByVal b As Integer) 
     Me.a = a 
     Me.b = b 
    End Sub 


    Public Iterator Function GetEnumerator() As IEnumerator(Of Integer) Implements IEnumerable(Of Integer).GetEnumerator 
     For n = 0 To 5 
      Yield a * n + b 
     Next 
    End Function 

    Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator 
     Return Me.GetEnumerator() 
    End Function 
End Class 
Dim s As New Sequence(1, 3) 

For Each i In s 
    Console.WriteLine(i) 
Next 

Console.ReadLine() 
+0

感謝您的幫助GSerg。到目前爲止,我所見過的三種解決方案都向我介紹了.NET庫中全新的過程和工具。正如我所提到的,我是一名初學者,不到3個月的編碼經驗,所以這裏提出的所有解決方案至少會使我保持忙幾個星期。再一次,非常感謝你幫助我的學習之旅 - 非常感謝。 – Mike