View Full Version : [C#]Problema con conversione
Kleidemos
04-02-2003, 19:38
Ho questo cod:
private void button_Click(object sender, System.EventArgs e)
{
Doppi maz = new Doppi();
Object prim = (Object)txtPrimo.Text;
Object second = (Object)txtSecondo.Text;
int primo = (int)prim;
int secondo = (int)second;
maz.somma(primo, secondo);
Object res = (Object)maz.ris;
txtRis.Text = res;
}
Che pero mi da l'errore allegato:(
Kleidemos
05-02-2003, 05:58
:cry:
txtRis.Text = res;
Anche se non si capisce qual'è la riga che ti da errore mi sa che è questa
txtRis.Text = (string)res; dovrebbe andare....
Kleidemos
05-02-2003, 12:17
Originally posted by "atragon"
txtRis.Text = res;
Anche se non si capisce qual'è la riga che ti da errore mi sa che è questa
txtRis.Text = (string)res; dovrebbe andare....
mi da sto errore
Ribadisco che io non ci capisco una mazza di C#, ma così non funziona ?
string prim = txtPrimo.Text;
string second = txtSecondo.Text;
int primo = (int)prim;
int secondo = (int)second;
maz.somma(primo, secondo);
string res = (string)maz.ris;
txtRis.Text = res;
In effetti, vedendo quello che vuoi fare, non capisco il perchè ti tutti questo passaggi attraverso Object....puoi rendere comprensibile il tuo codice facendo come ti ha detto Cionci anche se l'eccezione mi sa che è divuta al fatto che dichiari degli interi e lavori su numeri decimali.....infatti in compilazione funziona poi a runtime fai del casino....cosa succede se metti 5 anzichè 0.5?
Kleidemos
05-02-2003, 12:29
mi da
Kleidemos
05-02-2003, 12:31
ho la lib esterna:
using System;
namespace Matcod {
interface IMateIntero {
int somma(int i, int y);
int sottrai(int i, int y);
}
interface IMateDoppio {
double somma(double i, double y);
double sottrai(double i, double y);
}
public class Interi:IMateIntero {
public int ris;
public int somma(int i, int y) {
int ris= i+y;
return ris;
}
public int sottrai(int i, int y) {
int ris= i+y;
return ris;
}
}
public class Doppi:IMateDoppio {
public double ris;
public double somma(double i, double y) {
double ris= i+y;
return ris;
}
public double sottrai(double i, double y) {
double ris= i+y;
return ris;
}
}
}
E il coD:
// created on 04/02/2003 at 19.21
using System;
using System.Windows.Forms;
using Matcod;// richiamo alla libreria esterna
namespace MyForm {
public class CreatedForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtRis;
private System.Windows.Forms.Button btnCalc;
private System.Windows.Forms.TextBox txtSecondo;
private System.Windows.Forms.TextBox txtPrimo;
public CreatedForm()
{
InitializeComponent();
}
void InitializeComponent() {
this.txtPrimo = new System.Windows.Forms.TextBox();
this.txtSecondo = new System.Windows.Forms.TextBox();
this.btnCalc = new System.Windows.Forms.Button();
this.txtRis = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtPrimo
//
this.txtPrimo.Location = new System.Drawing.Point(96, 8);
this.txtPrimo.Name = "txtPrimo";
this.txtPrimo.TabIndex = 0;
this.txtPrimo.Text = "";
//
// txtSecondo
//
this.txtSecondo.Location = new System.Drawing.Point(224, 8);
this.txtSecondo.Name = "txtSecondo";
this.txtSecondo.TabIndex = 1;
this.txtSecondo.Text = "";
//
// btnCalc
//
this.btnCalc.Location = new System.Drawing.Point(184, 88);
this.btnCalc.Name = "btnCalc";
this.btnCalc.TabIndex = 2;
this.btnCalc.Text = "Calcola";
this.btnCalc.Click += new System.EventHandler(this.button_Click);
//
// txtRis
//
this.txtRis.Location = new System.Drawing.Point(176, 48);
this.txtRis.Name = "txtRis";
this.txtRis.TabIndex = 3;
this.txtRis.Text = "";
//
// CreatedForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(432, 118);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.txtRis,
this.btnCalc,
this.txtPrimo,
this.txtSecondo});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "CreatedForm";
this.Text = "Sommatrice";
this.ResumeLayout(false);
}
//
//La funzione che riempie il Text di txtRis con l'operazione di somma
//
private void button_Click(object sender, System.EventArgs e)
{
Interi maz = new Interi();// oggetto esterno
Object prim = (Object)txtPrimo.Text;
Object second = (Object)txtSecondo.Text;
int primo = (int)prim;
int secondo = (int)second;
maz.somma(primo, secondo);
Object res = (Object)maz.ris;
txtRis.Text = (string)res;
}
public static void Main() {
MyForm.CreatedForm mioForm = new MyForm.CreatedForm();
Application.Run(mioForm);
}
}
}
primo:
per passare da stringa ad intero devi usare il metodo Parse:
esempio:
string a = "111";
int i = int.Parse(a);
secondo:
public int ris;
public int somma(int i, int y) {
int ris= i+y;
questo pezzo qui non mi convince per nulla....non ho il compilatore per fare qualche prova ma alla fin fine penso che ti uscirebbero fuori dei gran "0" come risultato.... secondo me ris=i+y è la forma corretta, mai sentito parlare di "scope" (all'inglese :D ) delle variabili?
Prova a correggere il tuo codice così....
Kleidemos
05-02-2003, 14:03
private void button_Click(object sender, System.EventArgs e)
{
Interi maz = new Interi();// oggetto esterno
string prim = txtPrimo.Text;
string second = txtSecondo.Text;
int primo = int.Parse(prim);
int secondo = int.Parse(second); ;
maz.somma(primo, secondo);
txtRis.Text = (string)maz.ris;//ora mi da errore qui
}
maz.somma(primo, secondo);
int res = maz.ris;
txtRis.Text = res.ToString();
Mi sembra che tu ti stia complicando la vita per niente...
private void button_Click(object sender, System.EventArgs e)
{
Interi maz = new Interi();// oggetto esterno
maz.somma(int.Parse(txtPrimo.Text), int.Parse(txtSecondo.Text));
txtRis.Text = maz.ris.ToString();
}
Dai che finalmente Cionci si converte al C# :D :D
Kleidemos
05-02-2003, 16:11
Originally posted by "cionci"
Mi sembra che tu ti stia complicando la vita per niente...
son fatto cosi :rolleyes:
Kleidemos
05-02-2003, 16:12
ora va ma mi da sempre 0 :cry:
Te l'ho detto prima.... :rolleyes:
-----------
secondo:
public int ris;
public int somma(int i, int y) {
int ris= i+y;
questo pezzo qui non mi convince per nulla....non ho il compilatore per fare qualche prova ma alla fin fine penso che ti uscirebbero fuori dei gran "0" come risultato.... secondo me ris=i+y è la forma corretta, mai sentito parlare di "scope" (all'inglese :D ) delle variabili?
Prova a correggere il tuo codice così....
-------------
Kleidemos
05-02-2003, 16:20
risolto
Come dice giustamente atragon...il ris che vai a legere non è quello in cui hai messo il risultato...
Kleidemos
05-02-2003, 16:27
Originally posted by "cionci"
Come dice giustamente atragon...il ris che vai a legere non è quello in cui hai messo il risultato...
e che ieri dopo aver letto il libro ho provato a scrivermi tutto nel blocco note e quell int mi è skappato!
Poi oggi ho dovuto declinare 2 volte tutti i pronomi greci e nn ci ho fatto caso appena aperto l'editor :D
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.