2012-12-17 76 views
2

最近我開始爲我的As計算類開發項目VB,經過幾個小時的vb編碼,我碰到了實體磚牆。看起來像這樣VB.net將輸入保存到函數

Private Function store(ByVal opt As Integer, ByVal inp As String, ByVal q As String) As String 
     Dim na_me(20), amount(20), num1 As String 
     Select Case opt 
      Case 0 
       na_me(num1) += inp 
       amount(num1) += q 
       num1 += 1 
       Return "1" 
      Case 1 
       For Each Str As String In na_me 
        Console.WriteLine(Str) 
       Next 
       Return "1" 
      Case 2 
       num1 = 0 
       Return "1" 
      Case Else 
       Return "0" 
     End Select 
    End Function 

每次調用參數1時,它只是返回空白屏幕而不是數組完整的數據。 該函數設計用於保存並保存通過函數調用傳遞的任何數據,然後將其保存到數組中。並且一旦1被傳遞爲可選參數,然後打印出陣列中保存的所有數據。我知道這是一個noob問題,但我從來沒有使用過vb.net,我只在學校做過。

下面的代碼http://pastebin.com/nWxvx9pz

+1

每次要重新創建數組你運行你的函數儘量聲明爲靜態 –

+1

另外,'num1'應該是靜態的,應該是一個整數,而不是一個字符串。 –

回答

2

或者,您可以做類似這樣的操作並使用強類型集合/屬性。

如果你確實需要得到這些作爲數組所有你需要的MyStoredValues.ToArray()

Module Module1 


    Private na_me As List(Of String) 
    Public Property MyStoredValues() As List(Of String) 
     Get 
      If na_me Is Nothing Then 
       na_me = New List(Of String) 
      End If 
      Return na_me 
     End Get 
     Set(ByVal value As List(Of String)) 
      na_me = value 
     End Set 
    End Property 


    Private amount As List(Of String) 
    Public Property MyAmount() As List(Of String) 
     Get 
      If amount Is Nothing Then 
       amount = New List(Of String) 
      End If 
      Return amount 
     End Get 
     Set(ByVal value As List(Of String)) 
      amount = value 
     End Set 
    End Property 



    Sub Main() 
     store(0, "TEST TEST TEST", "10.23") 
     store(0, "TEST2 TEST2 TEST2", "11.33") 
     store(1, "", "") 
     Console.ReadKey() 
    End Sub 
    Private Function store(ByVal opt As Integer, ByVal inp As String, ByVal q As String) As Boolean 

     Select Case opt 
      Case 0 
       MyStoredValues.Add(inp) 
       MyAmount.Add(q) 

       Return True 
      Case 1 
       For Each Str As String In MyStoredValues 
        Console.WriteLine(Str) 
       Next 
       Return True 

      Case Else 
       Return False 
     End Select 
    End Function 
End Module 
+0

非常感謝你的答案,但不幸的是,我還沒有在vb.net中進一步發展,但在模塊中聲明變量而不是sub Main似乎工作得很好。但無論如何,感謝所有的答案,你們拯救了我的生命:) – 5m0k3

0

聲明在你的程序註釋上面的模塊級的所有變量的休息。 將它們從現在的位置中刪除,一旦完成即可。 在銷售屏幕頂部顯示作爲銷售物品出售的物品的選擇。

像...

 Do 
     Console.Clear() 
     Console.WriteLine("CATALOGUE" & Chr(13) & Chr(13)) 
     Console.WriteLine("Id  Name     Price" & Chr(13)) 
     Console.WriteLine("=======================================" & Chr(13)) 
     For num = 0 To 11 
      Console.WriteLine(Format(num, "00") & " " & product(num) & " " & price(num) & Chr(13)) 
     Next 
     Console.WriteLine("=======================================" & Chr(13)) 
     Console.WriteLine(Chr(13)) 

不要忘記NUM1是一個整數,而不是像其他成員指出的字符串。

要顯示你的存儲過程中Qunatity添加變量稱爲NUM2它如下代碼...

  Case 1 
      Console.WriteLine("Your Selection" & Chr(13)) 
      For num2 = 0 To UBound(na_me) - 1 
       Console.WriteLine(na_me(num2) & " " & amount(num2) & Chr(13)) 
      Next 

通過使用UBOUND(數組名稱這裏)statment,你會發現的最大數量當前陣列中的數組元素,並且您還可以通過NUM2變量訪問AMOUNT()數組

0
Try 
      Dim con As New OleDbConnection 
      Dim cmd As New OleDbCommand 
      Dim ads As OleDbDataReader 
      Dim ads1 As OleDbDataReader 
      Dim dr As OleDbDataReader 
      sql = "select joindate from Employee where Empcode='" & empid & "'" 
        cmd = New OleDbCommand(sql, con) 
        dr = cmd.ExecuteReader 
      While dr.Read 
         If dr(0) > dtpdate.Value Then 
          MsgBox("Can't Pay advance! Joined only on " & Format(dr(0), "dd-MMM-yyyy") & "") 
          Exit Sub 
         End If 
        End While 
        dr.Close() 
        cmd.CommandText = "insert into advance (Date,Empid,Amount,Remark,CompanyId,Confir)values('" & Format(dtpdate.Value, "dd/MMM/yyyy") & "','" & empid & "'," & txtamt.Text & ",'" & txtremark.Text & "'," & CompanyCode & ",'NO')" 
        cmd.ExecuteNonQuery() 
End Sub