Welcome to the CodeSmith Community!

Re: AddSharedRules() Error

Template Frameworks

A description has not yet been added to this group.

AddSharedRules() Error

Answered (Verified) This question is answered

Hi There,

I'm trying to include AddSharedRules into my VB PLINQ project using the below code 

 

 

' Place custom code here.

Private Shared Partial Sub AddSharedRules()
	RuleManager.AddShared(New CustomRule("UserName", "UserName must be 5 characters.", AddressOf MinLengthUserName))
End Sub

Private Shared Function MinLengthUserName(username As String) As Boolean
	If [String].IsNullOrEmpty(username) OrElse username.Length < 5 Then
		Return False
	Else
		Return True
	End If
End Function

But I am getting the error message Method 'AddSharedRules' cannot be declared 'Partial' because only one method 'AddSharedRules' can be marked 'Partial'

Obviously I am converting the C# example code on the site into VB.

Can you tell me what the issue is please.

Marcus

 

  • Post Points: 35
Verified Answer
  • Hello,

    Here is a full working example:

     

    Imports System
    Imports System.Linq
    Imports System.Data.Linq
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.ComponentModel.DataAnnotations
    Imports CodeSmith.Data
    Imports CodeSmith.Data.Rules.Validation
    Imports CodeSmith.Data.Rules
     
    Namespace PlinqTesting.Data
     
        Partial Public Class Address
     
            ' Place custom code here.
     
            Private Shared Sub AddSharedRules()
                 RuleManager.AddShared(Of CustomRule(Of String))(New CustomRule(Of String)("PostCode", "PostCode must be 5 characters.", AddressOf MinLengthPostCode))
     
            End Sub
     
     
            Private Shared Function MinLengthPostCode(PostCode As String) As Boolean
                If [String].IsNullOrEmpty(PostCode) OrElse PostCode.Length < 5 Then
                    Return False
                Else
                    Return True
                End If
            End Function
     
     
     
    #Region "Metadata"
            ' For more information about how to use the metadata class visit:
            ' http://www.plinqo.com/metadata.ashx
            <CodeSmith.Data.Audit.Audit()> _
            Friend Class Metadata
                ' WARNING: Only attributes inside of this class will be preserved.
     
                Public Property AddressID() As Object
                    Get
                        Return Nothing
                 
        End Get
                    Set(ByVal value As Object)
                    End Set
                End Property
     
                Public Property ApplicantID() As Object
                    Get
                        Return Nothing
                    End Get
                    Set(ByVal value As Object)
                    End Set
                End Property
     
                <Required()> _
                Public Property AddressMember() As Object
                    Get
                        Return Nothing
                    End Get
                    Set(ByVal value As Object)
                    End Set
                End Property
     
                Public Property PostCode() As Object
                    Get
                        Return Nothing
                    End Get
                    Set(ByVal value As Object)
                    End Set
                End Property
     
                Public Property Applicant() As Object
                    Get
                        Return Nothing
                    End Get
                    Set(ByVal value As Object)
                    End Set
                End Property
     
            End Class
     
    #End Region
     
        End Class
     
    End Namespace

     

     

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

All Replies
  • Hello,

    Your method needs to be named: Private Shared Sub AddSharedRules(). Please see this for more info: msdn.microsoft.com/.../bb531431.aspx

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

  • Hi Blake I have changed the code to the following...

     

    	Partial Public Class Address
    
    		' Place custom code here.
    
    		Private Shared Sub AddSharedRules()
    
    			RuleManager.AddShared(New CustomRule("UserName", "UserName must be 5 characters.", AddressOf MinLengthUserName))
    
    		End Sub
    
    
    		Private Shared Function MinLengthUserName(username As String) As Boolean
    			If [String].IsNullOrEmpty(username) OrElse username.Length < 5 Then
    				Return False
    			Else
    				Return True
    			End If
    		End Function

    The CustomRule KeyWord returns an error??

    I know this may seem like a very basic question but all the code on the site is virtually in C# (Not all of us code in this language).

    Plus the suggested code is very fragmented. ie to a Newbee it hard to understand exactly where the little snippet of code displayed goes. The above is not included in the Tracker sample application either.

    Hopefully you can help me get this working.

    Thanx.

    Marcus

     

  • Hello,

    You will need  to import the namespace CodeSmith.Data.Rules.Validation and CodeSmith.Data assembly for this type to show up. The completed code should look something like this:

     

    RuleManager.AddShared(Of CustomRule)(New CustomRule("UserName", "UserName must be 5 characters.", AddressOf MinLengthUserName))

     

     

    I'd recommend opening up the PLINQO Solution that is located in the Source folder as this contains all the rules. You can also view them here (code.google.com/.../CustomRule.cs).

    We are working on updating all of the documentation for the framework templates, currently it is slightly scattered on our websites and we are moving it all to the wiki (docs.codesmithtools.com/dashboard.action). I'll make a note to add more VB documentation. If you have any comments / suggestions on where the docs can be improved please let us know.

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

    Suggested by
  • Hi Blake,

    My Class is as follows:-

    CustomRule returns the error:-

    Error 1 Too few type arguments to 'CodeSmith.Data.Rules.Validation.CustomRule(Of T)'.

    Any ideas? Marcus

     

    Imports System
    Imports System.Linq
    Imports System.Data.Linq
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.ComponentModel.DataAnnotations
    Imports CodeSmith.Data
    Imports CodeSmith.Data.Rules.Validation
    Imports CodeSmith.Data.Rules
    
    Namespace PlinqTesting.Data
    
    	Partial Public Class Address
    
    		' Place custom code here.
    
    		Private Shared Sub AddSharedRules()
    
    			RuleManager.AddShared(New CustomRule("UserName", "UserName must be 5 characters.", AddressOf MinLengthPostCode))
    
    		End Sub
    
    
    		Private Shared Function MinLengthPostCode(PostCode As String) As Boolean
    			If [String].IsNullOrEmpty(PostCode) OrElse PostCode.Length < 5 Then
    				Return False
    			Else
    				Return True
    			End If
    		End Function
    
    
    
    #Region "Metadata"
    		' For more information about how to use the metadata class visit:
    		' http://www.plinqo.com/metadata.ashx
    		<CodeSmith.Data.Audit.Audit()> _
    		Friend Class Metadata
    			' WARNING: Only attributes inside of this class will be preserved.
    
    			Public Property AddressID() As Object
    				Get
    					Return Nothing
    			
    	End Get
    				Set(ByVal value As Object)
    				End Set
    			End Property
    
    			Public Property ApplicantID() As Object
    				Get
    					Return Nothing
    				End Get
    				Set(ByVal value As Object)
    				End Set
    			End Property
    
    			<Required()> _
    			Public Property AddressMember() As Object
    				Get
    					Return Nothing
    				End Get
    				Set(ByVal value As Object)
    				End Set
    			End Property
    
    			Public Property PostCode() As Object
    				Get
    					Return Nothing
    				End Get
    				Set(ByVal value As Object)
    				End Set
    			End Property
    
    			Public Property Applicant() As Object
    				Get
    					Return Nothing
    				End Get
    				Set(ByVal value As Object)
    				End Set
    			End Property
    
    		End Class
    
    #End Region
    
    	End Class
    
    End Namespace
    

     

     

  • Added this and still the same error....

     

    RuleManager.AddShared(Of CustomRule)(New CustomRule("PostCode", "PostCode must be 5 characters.", AddressOf MinLengthPostcode))

     

     

  • Hello,

    CustomRule is a generic class, and thus one must delcare the generic type like this:

    RuleManager.AddShared(Of CustomRule(Of String))(New CustomRule(Of String)("PostCode", "PostCode must be 5 characters.", AddressOf MinLengthPostCode))

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

  • Hi Blake - That's great. Everything is now working.

    As I suggested, if more code was in VB then it would be easier to implement the more detailed aspects PLINQO

  • Hello,

    Here is a full working example:

     

    Imports System
    Imports System.Linq
    Imports System.Data.Linq
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.ComponentModel.DataAnnotations
    Imports CodeSmith.Data
    Imports CodeSmith.Data.Rules.Validation
    Imports CodeSmith.Data.Rules
     
    Namespace PlinqTesting.Data
     
        Partial Public Class Address
     
            ' Place custom code here.
     
            Private Shared Sub AddSharedRules()
                 RuleManager.AddShared(Of CustomRule(Of String))(New CustomRule(Of String)("PostCode", "PostCode must be 5 characters.", AddressOf MinLengthPostCode))
     
            End Sub
     
     
            Private Shared Function MinLengthPostCode(PostCode As String) As Boolean
                If [String].IsNullOrEmpty(PostCode) OrElse PostCode.Length < 5 Then
                    Return False
                Else
                    Return True
                End If
            End Function
     
     
     
    #Region "Metadata"
            ' For more information about how to use the metadata class visit:
            ' http://www.plinqo.com/metadata.ashx
            <CodeSmith.Data.Audit.Audit()> _
            Friend Class Metadata
                ' WARNING: Only attributes inside of this class will be preserved.
     
                Public Property AddressID() As Object
                    Get
                        Return Nothing
                 
        End Get
                    Set(ByVal value As Object)
                    End Set
                End Property
     
                Public Property ApplicantID() As Object
                    Get
                        Return Nothing
                    End Get
                    Set(ByVal value As Object)
                    End Set
                End Property
     
                <Required()> _
                Public Property AddressMember() As Object
                    Get
                        Return Nothing
                    End Get
                    Set(ByVal value As Object)
                    End Set
                End Property
     
                Public Property PostCode() As Object
                    Get
                        Return Nothing
                    End Get
                    Set(ByVal value As Object)
                    End Set
                End Property
     
                Public Property Applicant() As Object
                    Get
                        Return Nothing
                    End Get
                    Set(ByVal value As Object)
                    End Set
                End Property
     
            End Class
     
    #End Region
     
        End Class
     
    End Namespace

     

     

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

Page 1 of 1 (9 items)