Håller som sagt på att convertera ett C# exempel till Vb.net men stött på patrull. Du använder C#-syntax, vb har något tillknökad historia med addhandler tror jag. Testa att söka på det C# vs VB!Ett implements problem till
I Public Sub New : "_model.PropertyChanged" sätts med följande felmeddelande
"'Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event."
Kan inte komma på vad jag skall göra... bitte hilfe!
EmployeePresenter.vb
<code>
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports MVPsession.Common
Namespace MVP
'-- Presenter contains the behavior
Public Class EmployeePresenter
Private _view As IEmployeeView
Private _service As IEmployeeService
Private _model As EmployeeModel
Public Sub New(ByVal view As IEmployeeView, ByVal model As EmployeeModel, ByVal service As IEmployeeService)
_view = view
_service = service
_model = model
_model.PropertyChanged += New System.ComponentModel.PropertyChangedEventHandler(AddressOf Model_PropertyChanged)
End Sub
Private Sub Model_PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs)
'-- if you want to process data when item value was changed.
Select Case e.PropertyName
Case "EmployeeType"
'-- code goes here
Exit Select
Case "Salary"
'-- code goes here
Exit Select
End Select
End Sub
Public Sub Initialize()
_view.EmployeeTypes = _service.GetEmployeeTypes()
_view.SalaryRanges = _service.GetSalaryRange(_view.EmployeeType)
End Sub
Private Sub ResetSalaryRanges()
_view.SalaryRanges = _service.GetSalaryRange(_view.EmployeeType)
End Sub
Private Sub ComputeTAXAmount()
_view.TAXAmount = _view.Salary * _view.TAX
End Sub
Public ReadOnly Property ErrorMessages() As ErrorMessageCollection
Get
Return _model.ErrorMessages
End Get
End Property
''' <summary>
''' Save data.
''' </summary>
Public Function Save() As Boolean
If Not _view.IsDirty Then
Return False
End If
'-- no changes
_model.EmployeeID = _view.EmployeeID
_model.Lastname = _view.Lastname
_model.Firstname = _view.Firstname
_model.EmployeeType = _view.EmployeeType
_model.Salary = _view.Salary
_model.TAX = _view.TAX
'-- for tracing and debugging
Debug.WriteLine([String].Format("Lastname: {0}, Firstname: {1}, EmployeeType: {2}, Salary: {3}", _model.Lastname, _model.Firstname, _model.EmployeeType, _model.TAX))
'-- validate the data.
If Not _model.Validate() Then
Debug.WriteLine("Messages: " & Convert.ToString(ErrorMessages))
_view.ShowValidationErrors(ErrorMessages)
ErrorMessages.Clear()
Return False
End If
_service.Save(_model)
Return _model.ErrorMessages.Count = 0
End Function
''' <summary>
''' Confirm the user before closing the form.
''' </summary>
''' <param name="formClosing">set to true if you are exectuting the method inside the Form_Closing event to avoid stack overflow.</param>
Public Function CloseForm(ByVal formClosing As Boolean) As Boolean
Dim result As Boolean = True
If _view.IsDirty Then
result = _view.ConfirmClose()
If Not result Then
Return result
End If
End If
If Not formClosing Then
_view.Close()
End If
Return result
End Function
Public Sub OnEmployeeTypeChanged()
ResetSalaryRanges()
End Sub
Public Sub OnEmployeeSalaryChanged()
_view.TAX = _service.GetTax(_view.Salary)
ComputeTAXAmount()
End Sub
Public Function Delete(ByVal id As Integer) As Boolean
If _view.ConfirmDelete() Then
_service.Delete(id)
Return True
End If
Return False
End Function
End Class
End Namespace
</code>
EmployeeModel.vb
<code>
Imports System.Collections.Generic
Imports System.Text
Imports System.ComponentModel
Imports MVPsession.Common
Namespace MVP
Public Class EmployeeModel
Implements IEmployee
Implements INotifyPropertyChanged
Implements IValidatable
Private _id As Integer
Private _firstname As String
Private _lastname As String
Private _employeeType As EnumEmployeeType
Private _salary As Double
Private _tax As Single
Private _errorMessages As ErrorMessageCollection
Public Sub New()
_errorMessages = New ErrorMessageCollection()
End Sub
Public Property EmployeeID() As Integer Implements IEmployee.EmployeeID
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
OnPropertyChanged("EmployeeID")
End Set
End Property
Public Property Firstname() As String Implements IEmployee.Firstname
Get
Return _firstname
End Get
Set(ByVal value As String)
_firstname = value
OnPropertyChanged("Firstname")
End Set
End Property
Public Property Lastname() As String Implements IEmployee.Lastname
Get
Return _lastname
End Get
Set(ByVal value As String)
_lastname = value
OnPropertyChanged("Lastname")
End Set
End Property
Public Property EmployeeType() As EnumEmployeeType Implements IEmployee.EmployeeType
Get
Return _employeeType
End Get
Set(ByVal value As EnumEmployeeType)
_employeeType = value
OnPropertyChanged("EmployeeType")
End Set
End Property
Public Property Salary() As Double Implements IEmployee.Salary
Get
Return _salary
End Get
Set(ByVal value As Double)
_salary = value
OnPropertyChanged("Salary")
End Set
End Property
Public Property TAX() As Single Implements IEmployee.TAX
Get
Return _tax
End Get
Set(ByVal value As Single)
_tax = value
OnPropertyChanged("TAX")
End Set
End Property
Public ReadOnly Property ErrorMessages() As ErrorMessageCollection
Get
Return _errorMessages
End Get
End Property
#Region "INotifyPropertyChanged Members"
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
#End Region
#Region "IValidatable Members"
Public Function Validate() As Boolean
If [String].IsNullOrEmpty(Firstname) Then
_errorMessages.Add(New ErrorMessage("Firstname is required"))
End If
If [String].IsNullOrEmpty(Lastname) Then
_errorMessages.Add(New ErrorMessage("Lastname is required"))
End If
If Salary <= 0.0 Then
_errorMessages.Add(New ErrorMessage("Salary must be positive number."))
End If
If TAX <= 0.0 Then
_errorMessages.Add(New ErrorMessage("TAX must be positive number."))
End If
Return _errorMessages.Count = 0
'-- no error
End Function
#End Region
Public Function Validate1() As Boolean Implements Common.IValidatable.Validate
End Function
End Class
End Namespace
</code>Sv: Ett implements problem till
Sv:Ett implements problem till
Tag en funktionspekare (egentligen inte, men vi säger så för att späda ut soppan).
Med +=, inkrementera din pekare med pekaradressen till en annan grej.
Nu har du TVÅ funktionspekare (två grejer som ska triggas av en grej).
Dvs:
0x8500A4FD + 0x8501A41E = HämtaPosten() och KöpaMjölk()
Är det rätt, logiskt, tydligt, och bra? :)
AddHandler beskriver vad som händer och vad du vill uppnå med koden.
Det är mycket sämre.. ? Pga vaddå? :)