简体   繁体   English

如何在另一个私有方法中使用私有方法中的变量? C# ASPX

[英]How can I use variable from a private method in another private method ? C# ASPX

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.我应该使用 get 和 set 还是创建一个完整的公共方法,我不知道。

Here is the first method and I want to use the variable "prixTotal"这是第一种方法,我想使用变量“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"这是我想使用变量“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() != "&nbsp;")
            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在课程的顶部,我把变量“prixTotal”像这样

  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我已经从 RecupPrixTransaction 方法中删除了它,并且在 AffTransa 方法中,我这样称呼它

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在 RecupPrixTransaction 方法中,“prixTotal”的结果是好的,但在 AffTransa 方法中,数字始终为 0

Thank you for your help ;)感谢您的帮助 ;)

In this way its, impossible.以这种方式其,不可能。 Now prixTotal is in method scope and its inaccessible from outside.现在prixTotal处于方法范围内并且无法从外部访问。 But you can move decimal prixTotal = 0;但是你可以移动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);我试图像这样调用该方法,但这不起作用十进制 recapPrixTotal = RecupPrixTransaction(prixTotal);

  • The return type of RecupPrixTransaction is of type String RecupPrixTransaction的返回类型是String类型
  • The parameters accepted by RecupPrixTransaction is of type XmlDocument and not decimal RecupPrixTransaction接受的参数是XmlDocument类型而不是decimal

To Solve:解决:

  • Declare private decimal prixTotal { get; set; }声明private decimal prixTotal { get; set; } private decimal prixTotal { get; set; } private decimal prixTotal { get; set; } at Class scope private decimal prixTotal { get; set; }范围
  • 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对我来说,这表明该方法正在返回一个字符串并传递一个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看起来您想要一个decimal并传递一个(至少给我们)名为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 .如果您希望它返回一个decimal ,我建议更改该方法的返回类型,假设您正在向它传递一个XmlDocument

If they are in the same class, I don't see why the above would not hit the method.如果他们在同一个班级,我不明白为什么上述不会命中该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM