IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

fonction en VB.net


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Mai 2007
    Messages
    122
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 122
    Par défaut fonction en VB.net
    bonjour,
    je commence en VB.net,j'ai une question qui peut paraitre un peu bete:
    esque cette fonction est ecri en .net:

    Public Class Evaluator

    Dim scriptControl As New MSScriptControl.ScriptControlClass
    Private _Formula As String
    Private _VariablesPreffix As String
    Private _Operators As ArrayList
    Private _CorrectedFormula As String
    Private _FormulaVariables As ArrayList
    Private _ParseCondition As ParseCondition = ParseCondition.RaiseErroForUndefinedVariable

    Public ReadOnly Property Formula() As String
    Get
    Return Me._Formula
    End Get
    End Property

    Public ReadOnly Property VariablesPreffix() As String
    Get
    Return Me._VariablesPreffix
    End Get
    End Property

    Private Sub PreEvaluate(ByVal keyCode As String, ByVal keyValue As String)
    Me._Formula = Me._Formula.Replace(keyCode.ToUpper, keyValue)
    End Sub

    Private Sub PreEvaluate(ByVal keyValuesDictionary As Hashtable)
    For Each keyCode As String In keyValuesDictionary.Keys
    Me.PreEvaluate(keyCode, keyValuesDictionary(keyCode))
    Next
    End Sub

    Public Function Evaluate(ByVal values As Hashtable) As Double
    Dim tmpFormula As String = Me._Formula
    For Each var As String In Me._FormulaVariables
    Select Case var.ToLower
    Case "abs", "round", "fix", "sin", "exp", "cos", "int", "atn","tan","sqr", "sgn", "log"
    Case Else
    Dim index As Integer = CInt(var.Replace(Me._VariablesPreffix, ""))
    If Not values(index) Is Nothing Then
    tmpFormula = tmpFormula.Replace(var, values(index))
    Else
    If Me._ParseCondition = ParseCondition.RaiseErroForUndefinedVariable Then
    Throw New Exception("Undefined variable!" & vbCrLf & "Base formula: " & Me._Formula & vbCrLf & "Corrected formula: " & Me._CorrectedFormula & vbCrLf & "On variable: " & var)
    Else
    tmpFormula = tmpFormula.Replace(var, 0)
    End If
    End If
    End Select
    Next
    Try
    Return CDbl(scriptControl.Eval(tmpFormula))
    Catch ex As Exception
    scriptControl.Error.Clear()
    Return -1
    'Throw New Exception(tmpFormula, ex)
    End Try
    End Function

    Public Sub New(ByVal formula As String, ByVal variablePreffix As String, ByVal parseCondition As ParseCondition, Optional ByVal preEvaluateKeyValuesDictionary As Hashtable = Nothing)
    scriptControl.Language = "vbscript"
    Me._Formula = formula
    Me._VariablesPreffix = variablePreffix
    Me._ParseCondition = parseCondition
    InitializeOperators()
    If Not preEvaluateKeyValuesDictionary Is Nothing Then
    Me.PreEvaluate(preEvaluateKeyValuesDictionary)
    End If
    InitializeFormula()
    End Sub

    Private Sub InitializeOperators()
    Me._Operators = New ArrayList
    Me._Operators.Add(CChar("+"))
    Me._Operators.Add(CChar("-"))
    Me._Operators.Add(CChar("/"))
    Me._Operators.Add(CChar("*"))
    Me._Operators.Add(CChar("^"))
    Me._Operators.Add(CChar("("))
    Me._Operators.Add(CChar(")"))
    'Me._Operators.Add(CChar(","))
    End Sub

    Private Sub InitializeFormula()
    Me._CorrectedFormula = Me._Formula.Replace(" ", "")
    Me._FormulaVariables = New ArrayList
    Dim arrOperators() As Char
    Dim variables() As String = Me._CorrectedFormula.Split(CType(Me._Operators.ToArray(GetType(Char)), Char()))
    For Each var As String In variables
    var = var.Trim
    If var.Length > 0 AndAlso Not IsNumeric(var) Then
    Me._FormulaVariables.Add(var)
    If Not IsNumeric(var.Replace(Me._VariablesPreffix, "")) Then
    Select Case var.ToLower
    Case "abs", "round", "fix", "sin", "exp", "cos", "int", "atn","tan", "sqr", "sgn", "log"
    Case Else
    Throw New Exception("Wrong formula!" & vbCrLf & "Base formula: " & Me._Formula & vbCrLf & "Corrected formula: " & Me._CorrectedFormula & vbCrLf & "On variable: " & var)
    End Select
    End If
    End If
    Next
    End Sub

    Public ReadOnly Property Operators() As ArrayList
    Get
    Return Me._Operators
    End Get
    End Property

    Public ReadOnly Property ParseCondition() As ParseCondition
    Get
    Return Me._ParseCondition
    End Get
    End Property

    End Class

    Public Enum ParseCondition
    RaiseErroForUndefinedVariable
    DoNotRaiseErroForUndefinedVariable
    End Enumte

  2. #2
    Membre confirmé
    Inscrit en
    Mai 2007
    Messages
    122
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 122
    Par défaut
    j'ai une autre question aussi:
    si c'est du .net c'est une classe eval,ca normalement c'est bon,mais il n'existe pas une classe plus simple??

    merci de votre aide

  3. #3
    Rédacteur
    Avatar de SaumonAgile
    Homme Profil pro
    Team leader
    Inscrit en
    Avril 2007
    Messages
    4 028
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Team leader
    Secteur : Conseil

    Informations forums :
    Inscription : Avril 2007
    Messages : 4 028
    Par défaut
    Pour le code, il y a la balise [ CODE]...[/CODE ], sinon c'est illisible.
    Et a priori, oui c'est du dotNet...

    Besoin d'un MessageBox amélioré ? InformationBox pour .NET 1.1, 2.0, 3.0, 3.5, 4.0 sous license Apache 2.0.

    Bonnes pratiques pour les accès aux données
    Débogage efficace en .NET
    LINQ to Objects : l'envers du décor

    Mon profil LinkedIn - MCT - MCPD WinForms - MCTS Applications Distribuées - MCTS WCF - MCTS WCF 4.0 - MCTS SQL Server 2008, Database Development - Mon blog - Twitter

  4. #4
    Membre confirmé
    Inscrit en
    Mai 2007
    Messages
    122
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 122
    Par défaut
    comment ca la balise ?????

  5. #5
    Membre confirmé
    Inscrit en
    Mai 2007
    Messages
    122
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 122
    Par défaut
    non c bon ,c encore une question bete.

  6. #6
    Membre confirmé
    Inscrit en
    Mai 2007
    Messages
    122
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 122
    Par défaut
    j'ai une autre question:
    esqu'il n'existe pas d'equvalent de la fonction "eval" pour migere un projet de VBa en VB.net.

  7. #7
    Membre confirmé
    Inscrit en
    Mai 2007
    Messages
    122
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 122
    Par défaut
    j'ai encore une question:

    esque pour cette classe eval:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    Public Class Evaluator
     
        Dim scriptControl As New MSScriptControl.ScriptControlClass
        Private _Formula As String
        Private _VariablesPreffix As String
        Private _Operators As ArrayList
        Private _CorrectedFormula As String
        Private _FormulaVariables As ArrayList
        Private _ParseCondition As ParseCondition = ParseCondition.RaiseErroForUndefinedVariable
     
        Public ReadOnly Property Formula() As String
        Get
            Return Me._Formula
        End Get
        End Property
     
        Public ReadOnly Property VariablesPreffix() As String
        Get
            Return Me._VariablesPreffix
        End Get
        End Property
     
        Private Sub PreEvaluate(ByVal keyCode As String, ByVal keyValue As String)
            Me._Formula = Me._Formula.Replace(keyCode.ToUpper, keyValue)
        End Sub
     
        Private Sub PreEvaluate(ByVal keyValuesDictionary As Hashtable)
            For Each keyCode As String In keyValuesDictionary.Keys
                Me.PreEvaluate(keyCode, keyValuesDictionary(keyCode))
            Next
        End Sub
     
        Public Function Evaluate(ByVal values As Hashtable) As Double
            Dim tmpFormula As String = Me._Formula
            For Each var As String In Me._FormulaVariables
                Select Case var.ToLower
                Case "abs", "round", "fix", "sin", "exp", "cos", "int", "atn","tan","sqr", "sgn", "log"
                Case Else
                    Dim index As Integer = CInt(var.Replace(Me._VariablesPreffix, ""))
                    If Not values(index) Is Nothing Then
                        tmpFormula = tmpFormula.Replace(var, values(index))
                    Else
                        If Me._ParseCondition = ParseCondition.RaiseErroForUndefinedVariable Then
                            Throw New Exception("Undefined variable!" & vbCrLf & "Base formula: " & Me._Formula & vbCrLf & "Corrected formula: " & Me._CorrectedFormula & vbCrLf & "On variable: " & var)
                        Else
                            tmpFormula = tmpFormula.Replace(var, 0)
                        End If
                    End If
                End Select
            Next
            Try
                Return CDbl(scriptControl.Eval(tmpFormula))
            Catch ex As Exception
                scriptControl.Error.Clear()
                Return -1
            'Throw New Exception(tmpFormula, ex)
            End Try
        End Function
     
        Public Sub New(ByVal formula As String, ByVal variablePreffix As String, ByVal parseCondition As ParseCondition, Optional ByVal preEvaluateKeyValuesDictionary As Hashtable = Nothing)
            scriptControl.Language = "vbscript"
            Me._Formula = formula
            Me._VariablesPreffix = variablePreffix
            Me._ParseCondition = parseCondition
            InitializeOperators()
            If Not preEvaluateKeyValuesDictionary Is Nothing Then
                Me.PreEvaluate(preEvaluateKeyValuesDictionary)
            End If
            InitializeFormula()
        End Sub
     
        Private Sub InitializeOperators()
            Me._Operators = New ArrayList
            Me._Operators.Add(CChar("+"))
            Me._Operators.Add(CChar("-"))
            Me._Operators.Add(CChar("/"))
            Me._Operators.Add(CChar("*"))
            Me._Operators.Add(CChar("^"))
            Me._Operators.Add(CChar("("))
            Me._Operators.Add(CChar(")"))
            'Me._Operators.Add(CChar(","))
        End Sub
     
        Private Sub InitializeFormula()
            Me._CorrectedFormula = Me._Formula.Replace(" ", "")
            Me._FormulaVariables = New ArrayList
            Dim arrOperators() As Char
            Dim variables() As String = Me._CorrectedFormula.Split(CType(Me._Operators.ToArray(GetType(Char)), Char()))
            For Each var As String In variables
                var = var.Trim
                If var.Length > 0 AndAlso Not IsNumeric(var) Then
                    Me._FormulaVariables.Add(var)
                    If Not IsNumeric(var.Replace(Me._VariablesPreffix, "")) Then
                        Select Case var.ToLower
                         Case "abs", "round", "fix", "sin", "exp", "cos", "int", "atn","tan", "sqr", "sgn", "log"
                        Case Else
                            Throw New Exception("Wrong formula!" & vbCrLf & "Base formula: " & Me._Formula & vbCrLf & "Corrected formula: " & Me._CorrectedFormula & vbCrLf & "On variable: " & var)
                        End Select
                    End If
                End If
            Next
        End Sub
     
        Public ReadOnly Property Operators() As ArrayList
        Get
            Return Me._Operators
        End Get
        End Property
     
        Public ReadOnly Property ParseCondition() As ParseCondition
        Get
            Return Me._ParseCondition
        End Get
        End Property
     
    End Class
     
    Public Enum ParseCondition
        RaiseErroForUndefinedVariable
        DoNotRaiseErroForUndefinedVariable
    End Enum
    on peut pas ajouter d'autre fonction pour faire d'autres operations?????

Discussions similaires

  1. Fonction email online.net
    Par dael dans le forum Autres hébergeurs
    Réponses: 3
    Dernier message: 15/11/2007, 16h18
  2. Réponses: 11
    Dernier message: 03/04/2007, 13h34
  3. [Appel de fonction en vb .Net]
    Par monromero dans le forum VB.NET
    Réponses: 4
    Dernier message: 13/03/2007, 13h40
  4. convertir fonction en vb.net to C#
    Par ouadie99 dans le forum VB.NET
    Réponses: 4
    Dernier message: 22/12/2006, 23h58
  5. Réponses: 1
    Dernier message: 12/12/2006, 15h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo