tags:

views:

290

answers:

3

I'm following along with an excellent Scott Guthrie article (MVC form posting scenarios) and trying to convert it to VB along the way. I've got everything working except one piece. At one point in the article he's adding his own business rules to a LINQ to SQL entity like this:

public partial class Product
{
    partial void OnValidate(ChangeAction action)
    {
       ...
    }
}

In converting it to VB, I'm not sure how to translate the "partial" part of OnValidate. If I do this:

Partial Public Class Product

    Private Sub OnValidate(ByVal action As ChangeAction)
       ...
    End Sub

End Class

then the business rules I put in OnValidate work, but it doesn't throw any exceptions for bad data (i.e. character in a decimal field), which makes sense, since I'm basically overriding Product's validation.

What's the syntax to make sure the underlying class's OnValidate executes in addition to my version?

Edit: Note that making OnValidate "Partial Private Sub" produces the following errors:

  • Partial methods must have empty method bodies.
  • Method 'OnValidate' cannot be declared 'Partial' because only one method 'OnValidate' can be marked 'Partial'.
+1  A: 
Partial Private Sub OnValidate(ByVal action As ChangeAction)
   ...
End Sub
Micah
Doing that gives me two errors: 1) "Partial methods must have empty method bodies", and 2) Method 'OnValidate' cannot be declared 'Partial' because only one method 'OnValidate' can be marked 'Partial'.
gfrizzle
+1  A: 

As usually happens, the problem lied elsewhere in the code. I had switched from "UpdateModel" to "TryUpdateModel" somewhere along the way, which means simple assignment errors were no longer being thrown. Going back to "UpdateModel" and using "Private Sub OnValidate" as above now works as it should.

OnValidate is already marked as "Partial" in the data context because it is meant to be overridden - I wasn't clobbering the underlying code after all.

gfrizzle
A: 

marking it as private does the implemetation

'designer' 
Partial Class Product

    ' Definition of the partial method signature.'
    Partial Private Sub OnValidate(ByVal action As ChangeAction)
    End Sub

End Class

'your implmentation'
Partial Class Product

    ' Definition of the partial method signature.'
    Private Sub OnValidate(ByVal action As ChangeAction)
      'do things'
    End Sub

End Class

see this http://msdn.microsoft.com/en-us/library/bb531431.aspx

Hath