vb.net - 如何在父类中使用子类的值?网络

标签 vb.net

我有一个令我沮丧不已的问题,我在父类中有一个可重写的函数,在子类中有一个重写函数,如下所示:

子类

Public Overrides Sub UpdatePrice(ByVal dblRetailPrice As Double)
    If dblWholesalePrice < 0 Then
        MessageBox.Show("Error, amount must be greater than 0.")
    Else
        dblRetailPrice = dblWholesalePrice * dblStandardMargin
    End If
End Sub

在父类中,我有

Public ReadOnly Property RetailPrice() As Double
    Get
        Return dblRetailPrice
    End Get
End Property

Public Overridable Sub UpdatePrice(ByVal dblRetailPrice As Double)
    If dblWholesalePrice < 0 Then
        MessageBox.Show("Please input an amount greater than 0,wholesale price has not changed", "error")
    Else
        dblRetailPrice = 1.1 * dblWholesalePrice
    End If
End Sub

当我调试时,产生了值,但它没有传递给 ski.RetailPrice() 的父类,这里似乎有什么问题?任何帮助将不胜感激。

最佳答案

您不应在更高范围内传递与类级变量同名的参数。局部变量将覆盖另一个,这意味着您的 setter 中的此语句:

dblRetailPrice = 1.1 * dblWholesalePrice

将设置您刚刚传入的 dblRetailPrice 临时参数的值,而不是您的类级 dblWholesalePrice 成员变量。

简单的解决方案是通过删除无用的类型符号前缀来更改参数的名称:

Public Class MyClass

    Protected dblRetailPrice As Double

    Public ReadOnly Property RetailPrice() As Double
        Get
           Return dblRetailPrice
        End Get
    End Property

    Public Overridable Sub UpdatePrice(ByVal retailPrice As Double)

       If dblWholesalePrice < 0 Then
           MessageBox.Show("Please input an amount greater than 0,wholesale price has not changed", "error")
       Else
           dblRetailPrice = 1.1 * dblWholesalePrice
       End If
    End Sub

End Class

关于vb.net - 如何在父类中使用子类的值?网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9953319/

相关文章:

regex - VB.Net正则表达式匹配

c# - vb.Net c# 运行批处理文件并流式传输其输出

vb.net - Vb Net检查arrayList是否包含子字符串

javascript - 如何通过查询字符串传递和检索 Html 标签

java - android studio图像按钮单击时更改src

vb.net - 正确使用 List.Exists 和 Predicates

vb.net - 在运行时从主应用程序中的类库更改连接字符串

vb.net - 如何使该函数不生成“"doesn' t return a value on all paths”警告?

asp.net - 访问 gridview 列中的超链接

.net - 在多线程期间检索 Windows 窗体属性