So I've got a class with private method and I want to use a variable from one method to another one. I don't know how and where to start. Should I use get and set or a creat a full public method, I don't know.
Here is the first method and I want to use the variable "prixTotal"
private string RecupPrixTransaction(XmlDocument doc)
{
XmlNodeList nl = null;
XmlNode nodeDevise = null;
string data = string.Empty;
string devise = string.Empty;
decimal acompteTotal = 0;
decimal prixTotal = 0;
if (doc == null)
return (data);
nodeDevise = doc.SelectSingleNode("/Caddie/GroupesProduits/GroupeProduit[@IdGroupe='" + this.guid + "']/Devise");
if (nodeDevise == null)
return (data);
devise = nodeDevise.InnerText;
nl = doc.SelectNodes("/Caddie/GroupesProduits/GroupeProduit[@IdGroupe='" + this.guid + "']/Produits/Produit");
if (nl == null)
return (data);
try
{
foreach (XmlNode nd in nl)
{
prixTotal += decimal.Parse(nd["PrixTotal"].InnerText, System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
acompteTotal += decimal.Parse(nd["AcompteTotal"].InnerText, System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
}
data += "Prix total de la transaction: <strong>" + prixTotal + "</strong> " + devise + "<br />";
data += "Acompte total payé: <strong>" + acompteTotal + "</strong> " + devise + "<br />";
}
catch (Exception)
{
data += "Prix/acompte invalide.<br />";
}
return (data);
}
And here it's the method where I want to use the variable "prixTotal"
private void AffTransa(string id, SqlConnection sqlConnect)
{
SqlDataReader reader = null;
decimal recapPrixTotal = RecupPrixTransaction(prixTotal);
reader = Database.ExecuteReader(EnumConstant.SqlLecture + "SELECT * FROM transactionfin WHERE IdTransactionFin='" + id + "'", sqlConnect);
if (reader.Read())
{
this.lTransa.Text = "<h2>Détail de la transaction " + id + "</h2><br />";
this.lTransa.Text += "Date de la transaction: <b>" + reader["TransactionFinDate"] + "</b><br />";
this.date = reader["TransactionFinDate"].ToString();
this.lTransa.Text += "Solution de paiement: <b>" + MyRegex.ReplacePaiementSol(reader["PaiementSolution_IdPaiementSolution"].ToString()) + "</b><br />";
this.lTransa.Text += "{[{-}]}Mode: <b>" + MyRegex.ReplaceMode(reader["TransactionFinMode"].ToString()) + "</b><br />";
this.lTransa.Text += "Etat: <b>" + MyRegex.ReplaceEtat(reader["TransactionFinEtat"].ToString()) + "</b><br />";
if (reader["TransactionFinUrlRetour"].ToString() != " ")
this.lTransa.Text += "Url de retour: <a href=\"" + reader["TransactionFinUrlRetour"].ToString() + "\">" + MyRegex.ReplaceUrlRetour(reader["TransactionFinUrlRetour"].ToString()) + "</a><br />";
this.lTransa.Text += "<br /><br />Id Transaction: <b>" + reader["TransactionFinIdTransaction"] + "</b><br />";
this.lTransa.Text += "Guid: <b>" + reader["TransactionFinGuidGroupe"].ToString() + "</b><br />";
this.guid = reader["TransactionFinGuidGroupe"].ToString();
this.lTransa.Text += "Id action contact: <b>" + reader["ActionContact_IdActionContact"] + "</b><br />";
this.lTransa.Text += "Id vente entete: <b>" + reader["VenteEntete_IdVenteEntete"] + "</b><br />";
if (reader["TransactionFinDetail"] != null && reader["TransactionFinDetail"].ToString() != string.Empty)
{
this.tbTransa.Visible = true;
this.tbTransa.Text = XmlParsing.IndentXml(reader["TransactionFinDetail"].ToString());
this.lRecap.Text += "Recapitulatif de la Transaction : " ;
this.recapTransa.Visible = true;
this.recapTransa.Text += "#Fournisseur" + "\n" +
"#Internaute" + "\n" +
"#Montants" + "\n" + recapPrixTotal +
"#Produits" + "\n" +
"#Suppléments" + "\n";
}
}
reader.Close();
}
I tried to call the method like this but this doesn't work
decimal recapPrixTotal = RecupPrixTransaction(prixTotal);
EDIT :
At the top of the class i've put the variable "prixTotal" like this
public partial class SeeDetail : System.Web.UI.Page
{
private decimal prixTotal = 0;
...
}
I've removed it from the method RecupPrixTransaction Andin the AffTransa method, i call it like this
decimal recapPrixTotal = prixTotal;
In the method RecupPrixTransaction the result of "prixTotal" is the good one But in the AffTransa method the number is always 0
Thank you for your help ;)
In this way its, impossible. Now prixTotal
is in method scope and its inaccessible from outside. But you can move decimal prixTotal = 0;
to class scope and then this variable will be accessible from both methods.
Class A
{
private decimal prixTotal = 0;
private void Method A ....
}
I tried to call the method like this but this doesn't work decimal recapPrixTotal = RecupPrixTransaction(prixTotal);
RecupPrixTransaction
is of type String
RecupPrixTransaction
is of type XmlDocument
and not decimal
To Solve:
private decimal prixTotal { get; set; }
private decimal prixTotal { get; set; }
private decimal prixTotal { get; set; }
at Class scope private string RecupPrixTransaction(XmlDocument doc) { prixTotal =0; ... }
private void AffTransa(string id, SqlConnection sqlConnect) { decimal recapPrixTotal= prixTotal; ... }
I can't see any way this will work.
You are calling
private string RecupPrixTransaction(XmlDocument doc)
which to me suggests the method is returning a string and passing a XmlDocument
but what you are passing is
decimal recapPrixTotal = RecupPrixTransaction(prixTotal);
Where it looks like you are wanting a decimal
and passing an (to us at least) variable called prixTotal
If you want it to return a decimal
, I suggest changing the return type of the method, assuming that you ARE passing it an XmlDocument
.
If they are in the same class, I don't see why the above would not hit the method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.