Visual Basic 中的子程序和变量

subroutines and variable in visual basic

我正在用 Visual Basic 编写代码,我想调用一个不同的子例程,该子例程具有关于特定变量的特定代码。

Dim MonthSales As Double
Call GetMonthSales()

在我的备用子例程中:

Console.WriteLine("How much did you sell this month?")
MonthSales = CDbl(Console.ReadLine())

我想要做的是让我的替代子例程将我在主例程中设定的尺寸设置为用户作为他们的月销售额的任何内容

你想做的是糟糕的设计。相反,将方法写入 return 一个值,如下所示:

Public Shared Function GetMonthSales() As Decimal
   Console.WriteLine("How much did you sell this month?")
   Return CDec(Console.ReadLine())
End Function

然后在您的 Main() 方法中:

Dim MonthSales As Decimal = GetMonthSales()

同样值得注意的是,无论何时处理金钱,您都应该使用 Decimal 类型,而不是 Double