[英]How do I get this basic OOP code to work in Visual Basic?
感謝您閱讀我的問題。 我正在闡述一個class,其中,必須通過鍵盤輸入3個值,必須使用屬性處理屬性,class必須具有構造函數和析構函數,並且必須顯示最大值和最小值。 我不知道我應該如何使我的代碼正常工作,因此我需要有人幫助來查看它並找到錯誤。 它向我顯示了編譯錯誤,在測試它並輸入第一個值時,它在屏幕上顯示“錯誤”。 有些部分是西班牙語,因為他們要求我這樣做,我希望這不會造成不便。
我在下面附上我的代碼,希望有人能幫助我,謝謝。
Public Class ValoresPorTeclado
Private Val1 As Integer
Private Val2 As Integer
Private Val3 As Integer
Public Sub New() 'Constructor
Dim p = Val1 And Val1 AndAlso Val3 = 0
End Sub
Protected Shared Sub Finalize() 'Destructor
End Sub
Public Property Valor() As Integer
Get
Return Valor
End Get
Set(ByVal num As Integer)
If num > 0 Then
Dim p = Val1 And Val2 AndAlso Val3 = num
ElseIf num < 0 Then
Dim p = Val1 And Val2 AndAlso Val3 = num
Else
End If
End Set
End Property
Public Sub CargarValores(valor As Integer)
Dim v As Integer
Console.Write("Ingrese el primer valor ")
v = Console.ReadLine()
valor = Val1
Console.Write("Ingrese el segundo valor")
valor = Val2
Console.Write("Ingrese el tercer valor")
valor = Val3
CargarValores(valor)
End Sub
Public Sub Calcular(ByVal v As Integer)
Dim f, Compare As Integer
For f = 1 To 100
Compare = f > v AndAlso f < v
Dim Comapare As Boolean = Nothing
Console.Write(Comapare)
Console.Write("-")
Next
Console.WriteLine()
End Sub
End Class
Sub Main()
Dim Valores As New ValoresPorTeclado()
Valores.CargarValores()
Console.ReadKey()
End Sub
End Module
錯誤太多,很難告訴您如何修復它們。
你應該從設置開始
Option Strict On
在代碼頂部或項目屬性中。 這會產生更多的編譯錯誤。 這很好,因為它經常揭示代碼中的錯誤,阻止它正常運行。
有一個沒有匹配Module moduleName
的End Module
。 寫
Module Program
Sub Main()
...
End Sub
End Module
構造函數聲明了一個局部變量p
。 該變量只能在構造函數本身中訪問,因此無用。
我知道,您已被告知這樣做,但 .NET 中的終結器不是一個好主意,除非它們有令人信服的理由,例如釋放資源。 C# sharp 的文檔說(VB也是如此):
不應使用空的終結器。 當 class 包含終結器時,將在終結隊列中創建一個條目。 當調用終結器時,會調用垃圾收集器來處理隊列。 一個空的終結器只會導致不必要的性能損失。
Property Valor
毫無意義。
num > 0
和num < 0
,但是這兩種情況完全相同。Val1 And Val2 AndAlso Val3 = num
是錯誤的。 它可能應該是Val1 = num And Val2 = num And Val3 = num
(但我看不出它在這種情況下的目的是什么)。And
或短路版本AndAlso
。 將兩者混為一談是沒有意義的。由於我不知道這個屬性的意圖是什么,我無法告訴你如何修復它。
Sub CargarValores
有一個未使用的 Integer 參數。 這是沒用的。 你只有一個ReadLine()
。 它應該如何從用戶那里讀取 3 個值? 您將Val1
、 Val2
和Val3
分配給valor
。 相反,您應該將您從用戶那里讀取的值分配給Val1
、 Val2
和Val3
。
CargarValores
在最后調用自己,從而再次創建了無休止的遞歸。
您寫了“必須通過鍵盤輸入 3 個值,必須使用屬性處理屬性” ,但您已經聲明了 3 個字段。 使它們成為屬性。
試圖把東西放在一起。
將 class 聲明為
Option Strict On
Public Class ValoresPorTeclado
Public Property Val1 As Integer
Public Property Val2 As Integer
Public Property Val3 As Integer
... the methods go here
End Class
由於您必須輸入 3 個值,因此聲明一個 function 提示用戶,將輸入讀取為字符串並將此輸入轉換為 integer
Private Function PromptForValue(prompt As String) As Integer
Console.Write(prompt + ": ")
Dim input = Console.ReadLine()
Return Integer.Parse(input)
End Function
此 function 可用於Sub CargarValores
加載 3 個值並設置屬性
Public Sub CargarValores()
Val1 = PromptForValue("Ingrese el primer valor")
Val2 = PromptForValue("Ingrese el segundo valor")
Val3 = PromptForValue("Ingrese el tercer valor")
End Sub
您必須獲得最低和最高值。 為此創建函數
Public Function LowestValue() As Integer
Dim lowest As Integer = Val1
If Val2 < lowest Then
lowest = Val2
End If
If Val3 < lowest Then
lowest = Val3
End If
Return lowest
End Function
Public Function HighestValue() As Integer
Dim highest As Integer = Val1
If Val2 > highest Then
highest = Val2
End If
If Val3 > highest Then
highest = Val3
End If
Return highest
End Function
現在您可以將Main
方法編寫為
Module Program
Sub Main()
Dim Valores As New ValoresPorTeclado()
Valores.CargarValores()
Dim lowest As Integer = Valores.LowestValue()
Dim highest As Integer = Valores.HighestValue()
Console.WriteLine($"The lowest value is {lowest}")
Console.WriteLine($"The highest value is {highest}")
Console.ReadKey()
End Sub
End Module
構造函數和析構函數可以留空。 如果這不是您分配的要求,我會放棄它們。
我希望看到您的 class 獨立於用戶界面。 如前所述,您的代碼有點混亂。
這個簡單的 class 包含 2 個構造函數、3 個屬性和一個方法。
參數化構造函數設置所有三個屬性。 我們添加回默認構造函數,因此使用 class 的應用程序可以根據需要單獨設置屬性。
Function
使用List(Of T)
的.Max
方法返回 3 個屬性中的最高值。
Public Class ValoresPorTeclado
Public Property Valor1 As Integer
Public Property Valor2 As Integer
Public Property Valor3 As Integer
Public Sub New()
End Sub
Public Sub New(Int1 As Integer, Int2 As Integer, Int3 As Integer) 'Constructor
Valor1 = Int1
Valor2 = Int2
Valor3 = Int3
End Sub
Public Function GetMaxInteger() As Integer
Dim ListOfValues As New List(Of Integer)
ListOfValues.AddRange({Valor1, Valor2, Valor3})
Return ListOfValues.Max
End Function
End Class
要在控制台應用程序中使用 class...
我們正在使用Integer.TryParse
,它返回True
或False
,如果返回True
,還將i
的值設置為轉換后的字符串。
在我們得到用戶輸入后,我們創建一個新的ValoresPorTeclado
class 實例,調用參數化的構造函數。 然后我們可以在這個實例val
上調用Function
。
Module Module1
Sub Main()
Dim Counter As Integer
Dim IntegerList As New List(Of Integer)
Do Until Counter = 3
Console.WriteLine("Please enter a whole number")
Dim i As Integer
If Integer.TryParse(Console.ReadLine, i) Then
IntegerList.Add(i)
Counter += 1
Else
Console.WriteLine("Not a valid number. Try again.")
End If
Loop
Dim val As New ValoresPorTeclado(IntegerList(0), IntegerList(1), IntegerList(2))
Console.WriteLine($"The highest number you entered is {val.GetMaxInteger}")
Console.ReadKey()
End Sub
End Module
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.