Delegate Example

Delegate Example

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Delegate Function NumberChanger(Of T)(ByVal n As T) As T

Namespace DelegatesExample1
    Class Program
        Shared num As Integer = 10

        Public Shared Function AddNum(ByVal p As Integer) As Integer
            num += p
            Return num
        End Function

        Public Shared Function MultNum(ByVal q As Integer) As Integer
                        ''' Cannot convert ExpressionStatementSyntax, System.ArgumentOutOfRangeException: Exception of type 'System.ArgumentOutOfRangeException' was thrown.
''' Parameter name: op
''' Actual value was MultiplyAssignmentStatement.
'''    at ICSharpCode.CodeConverter.Util.VBUtil.GetExpressionOperatorTokenKind(SyntaxKind op)
'''    at ICSharpCode.CodeConverter.VB.NodesVisitor.MakeAssignmentStatement(AssignmentExpressionSyntax node)
'''    at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitAssignmentExpression(AssignmentExpressionSyntax node)
'''    at Microsoft.CodeAnalysis.CSharp.Syntax.AssignmentExpressionSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
'''    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
'''    at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node)
'''    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitAssignmentExpression(AssignmentExpressionSyntax node)
'''    at Microsoft.CodeAnalysis.CSharp.Syntax.AssignmentExpressionSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
'''    at ICSharpCode.CodeConverter.VB.MethodBodyVisitor.ConvertSingleExpression(ExpressionSyntax node)
'''    at ICSharpCode.CodeConverter.VB.MethodBodyVisitor.VisitExpressionStatement(ExpressionStatementSyntax node)
'''    at Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionStatementSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
'''    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
'''    at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.ConvertWithTrivia(SyntaxNode node)
'''    at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)
''' 
''' Input: 
'''            
            num *= q;

''' 
            Return num
        End Function

        Public Shared Function getNum() As Integer
            Return num
        End Function

        Private Shared Sub Main(ByVal args As String())
            Dim nc1 As NumberChanger(Of Integer) = New NumberChanger(Of Integer)(AddressOf AddNum)
            Dim nc2 As NumberChanger(Of Integer) = New NumberChanger(Of Integer)(AddressOf MultNum)
            nc1(25)
            Console.WriteLine("Value of Num: {0}", getNum())
            nc2(5)
            Console.WriteLine("Value of Num: {0}", getNum())
            Console.ReadKey()
        End Sub
    End Class
End Namespace

©Copyright 2016 All Rights Reserved LearnDotNetHome.com