希望覆盖基本类型'Double' [英] Looking to override the base type 'Double'

查看:100
本文介绍了希望覆盖基本类型'Double'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我已经看了一遍这个,它的输入/输出是我有一个类丰富的属性,按结构划分等,而不是编写一个类来处理更新'Double'值时所有的dictionarys / keypairs / subclass。



所以我想要做的是添加一个'RaiseEvent',所以当值设置为例如:



Hi,

I have looked all over for this, the ins/outs of it are that i have a class with an abundance of properties, split by structures etc, instead of writing a class to handle all of the dictionarys/keypairs/subclass for when a 'Double' Value is updated.

So what i want to do is add a 'RaiseEvent' so when the value is set for example:

'Raise an Event here and i will use an event handler in my class to handle it
Dim dblTest as Double = 1.234







Dim dblTest as Double
'Raise Event when the below is done
Double = 1.234



我知道你不能打电话


I know you can't call

Dim WithEvents dblTest as Double



因为这会引发错误'WithEvents'变量只能是键入类,接口或具有类约束的类型参数。


as this raises an error 'WithEvents' variables can only be typed as classes, interfaces, or type parameters with class constraints.

Public Class clsMyClass
    Public Structure MyStruct
        Public myVal As Double
    End Structure
    Public Structure MyStruct2
        Public myVal As Double
    End Structure
    Dim MyVar As New MyStruct
    Public Event MyCls_DoubleChangeEvent(ByVal dblValue As Double)

    Private Sub MyCls_HandleEvent() Handles MyClass.MyCls_DoubleChangeEvent
        'Do My Calculations
    End Sub

End Class





如果有人可以协助添加事件处理程序以在更新double值时触发事件,因为我不想添加负载功能/ Sub的属性,很容易出错和意外。



问候

Dave



Really appreicate if someone can assist in adding an event handler to fire an event when a double value is updated as i dont want to have to add loads of properties to a Function/Sub, far too prone to errors and mishaps.

Regards
Dave

推荐答案

看起来问题的标题和正文是不同的帖子。



不仅你在标题中说的是不可能的,这些话根本没有意义。标签继承,类型和覆盖的组合也是如此。 覆盖是OOP的核心概念,它仅适用于方法(包括属性getter / setter);并且它也不适用于(在.NET中)值类型或其成员。



至于问题的主体,相反,这不仅是可能的,但是微不足道。我的意思是,你可以做一些像Dim WithEvents dblTest as Double这样的事情(当然不是这样)。这是您应该在类中定义的事件和一些double属性的简单组合。我希望你知道如何定义和调用事件,或者你可以阅读它。现在,该属性看起来与使用它的代码的字段完全相同;它模仿了读取和赋值语法,但这些操作可以带有副作用。在您的情况下,特别是副作用是调用事件。在事件参数中,您可以传递一些数据,例如在分配时属性的旧值和新值。



所有你需要的是:

http://msdn.microsoft.com/en-us/library/ms172877。 aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/ bc3dtbky.aspx [ ^ ]。







这是一个简单的代码示例:

It looks like the title and the body of the question are different post.

Not only what is you say in the title is impossible, these words simply make no sense. Same goes to the combination of tags "inheritance", "types" and "override". "Override" is a central concept of OOP, it is only applicable to methods (including property getters/setters); and it also inapplicable (in .NET) to value types or their members.

As to the body of the question, this is, in contrast, not only possible, but trivial. I mean, you can do something like "Dim WithEvents dblTest as Double" (not exactly this way, of course). This is a simple combination of an event you should define in your class, and some double property. I hope you know how to define and invoke the events, or you can just read about it. Now, the property is something which looks exactly as the field for the code using it; it mimic reading and assignment syntax, but these operations can be backed with side effect. In your case, in particular, the side effect would be invocation of the event. In the event arguments, you can pass some data, such as old and new values of the properties at the moment of assignment.

All you need is this:
http://msdn.microsoft.com/en-us/library/ms172877.aspx[^],
http://msdn.microsoft.com/en-us/library/bc3dtbky.aspx[^].



This is a simple code sample for you:
Public Class DoubleAssignedEventArgs
	Inherits System.EventArgs
	Friend Sub New(oldValue As Double, newValue As Double)
		Me.OldValue = oldValue
		Me.NewValue = newValue
	End Sub
	Public Property OldValue() As Double
		Get
			Return m_OldValue
		End Get
		Private Set
			m_OldValue = Value
		End Set
	End Property
	Private m_OldValue As Double
	Public Property NewValue() As Double
		Get
			Return m_NewValue
		End Get
		Private Set
			m_NewValue = Value
		End Set
	End Property
	Private m_NewValue As Double
End Class

Public Class MyClass
	Public DoubleValueModified As EventHandler(Of DoubleAssignedEventArgs)
	Public Property DoubleValue() As Double
		Get ' getter
			Return doubleValueField
		End Get
		Set ' setter
			Dim oldValue As Double = doubleValueField
			If value = oldValue Then
                                Return ' we don't want to do anything
                                       ' it the new value is the same
			End If
			doubleValueField = value
			If DoubleValueModified IsNot Nothing Then
				DoubleValueModified.Invoke(
                                    Me,
                                    New DoubleAssignedEventArgs(oldValue, value))
			End If
		End Set
	End Property
	Private doubleValueField As Double ' backing field for the property
End Class

< br $> b $ b

我希望它很清楚。



-SA


这篇关于希望覆盖基本类型'Double'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆