Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Somma sottoforma di stringa
Forum - C# / VB.NET - Somma sottoforma di stringa

Avatar
LLawliet (Normal User)
Newbie


Messaggi: 17
Iscritto: 26/06/2012

Segnala al moderatore
Postato alle 16:28
Martedì, 10/07/2012
Salve a tutti. Sò che per voi questo è banale, ma per me è un problema (: Ho una textbox con una somma scritta così "4 + 10". Cliccando un bottone vorrei che il msgbox riportasse il risultato "14" e non il testo vero e proprio "4 + 10"! Come posso fare?

PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 16:37
Martedì, 10/07/2012
Non è affatto banale.

Un esempio di codice trovato da qualche parte ...

Codice sorgente - presumibilmente VB.NET

  1. Imports System.CodeDom.Compiler
  2. Imports System.Reflection
  3.  
  4. Module Module1
  5.  
  6.     Private Function ProcessCommand(ByVal command As String) As Double
  7.         Dim MyProvider As New VBCodeProvider 'Create a new VB Code Compiler
  8.         Dim cp As New CompilerParameters     'Create a new Compiler parameter object.
  9.         cp.GenerateExecutable = False        'Don't create an object on disk
  10.         cp.GenerateInMemory = True           'But do create one in memory.
  11.         'If cp.OutputAssembly is used with a VBCodeProvider, it seems to want to read before it is executed.  
  12.         'See C# CodeBank example for explanation of why it was used.
  13.  
  14.         'the below is an empty VB.NET Project with a function that simply returns the value of our command parameter.
  15.         Dim TempModuleSource As String = "Imports System" & Environment.NewLine & _
  16.                                          "Namespace ns " & Environment.NewLine & _
  17.                                          "Public Class class1" & Environment.NewLine & _
  18.                                          "Public Shared Function Evaluate()" & Environment.NewLine & _
  19.                                          "Return " & command & Environment.NewLine & _
  20.                                          "End Function" & Environment.NewLine & _
  21.                                          "End Class" & Environment.NewLine & _
  22.                                          "End Namespace"
  23.         'Create a compiler output results object and compile the source code.
  24.         Dim cr As CompilerResults = MyProvider.CompileAssemblyFromSource(cp, TempModuleSource)
  25.         If cr.Errors.Count > 0 Then
  26.             'If the expression passed is invalid or "", the compiler will generate errors.
  27.             Throw New ArgumentOutOfRangeException("Invalid Expression - please use something VB could evaluate")
  28.         Else
  29.             'Find our Evaluate method.
  30.             Dim methInfo As MethodInfo = cr.CompiledAssembly.GetType("ns.class1").GetMethod("Evaluate")
  31.             'Invoke it on nothing, so that we can get the return value
  32.             Return Convert.ToDouble(methInfo.Invoke(Nothing, Nothing))
  33.         End If
  34.     End Function
  35.  
  36.  
  37.  
  38.     Sub Main()
  39.         Console.WriteLine(ProcessCommand("1+1").ToString()) 'Displays 2
  40.         Console.WriteLine(ProcessCommand("Math.PI").ToString()) 'Displays 3.14159265358979
  41.         Console.WriteLine(ProcessCommand("Math.Abs(-22)").ToString()) 'Displays 22
  42.         Console.WriteLine(ProcessCommand("3-4+6+7+22/3+66*(55)").ToString()) 'Displays 3649.333333333333333333
  43.     End Sub
  44.  
  45. End Module



Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
PM Quote
Avatar
luca97 (Normal User)
Newbie


Messaggi: 18
Iscritto: 08/07/2012

Segnala al moderatore
Postato alle 0:00
Mercoledì, 11/07/2012
Ciao, vedi se questo codice va bene:

Codice sorgente - presumibilmente VB.NET

  1. Public Class Form1
  2.     Dim testo As String
  3.     Dim n1 As String
  4.     Dim n2 As String
  5.     Dim somma As Double
  6.  
  7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  8.         If TextBox1.Text.Contains("+") Then
  9.             n1 = TextBox1.Text.Split("+")(0)
  10.             n2 = TextBox1.Text.Split("+")(1)
  11.             somma = Convert.ToDouble(n1) + Convert.ToDouble(n2)
  12.             testo = TextBox1.Text + "=" + somma.ToString()
  13.         End If
  14.         MsgBox(testo)
  15.  
  16.     End Sub
  17. End Class



io l'ho provato e scrivendo "4+10" mi ha dato come risultato "14" :)

Ultima modifica effettuata da luca97 il 11/07/2012 alle 0:01
PM Quote
Avatar
LLawliet (Normal User)
Newbie


Messaggi: 17
Iscritto: 26/06/2012

Segnala al moderatore
Postato alle 16:40
Mercoledì, 11/07/2012
Grazie mille a tutti :D

PM Quote