Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this class
public class MonthlyPayment
    {
        public int Luna { get; set; }
        public double SoldInitial { get; set; }
        public double PlataLunara { get; set; }
        public double Dobanda { get; set; }
        public double Principal { get; set; }
        public double Asigurare { get; set; }
        public double Comision { get; set; }
        public double TotalLunar { get; set; }
        public double SoldCredit { get; set; }
        
    }


I have this DataContext class
public class CreditDataContext {
  public List<MonthlyPayment> MonthlyPayments { get; set; }

  public void LoadMonthlyPayments() {
    MonthlyPayments = new List<MonthlyPayment>();
    MonthlyPayment payment = null;
    payment = new MonthlyPayment() {
      Luna = 0,
      SoldInitial = 0,
      PlataLunara = 0,
      Dobanda = 0,
      Principal = 0,
      Asigurare = 0,
      Comision = 200,
      TotalLunar = 200,
      SoldCredit = 10000
    };

    MonthlyPayments.Add(payment);

    for (int i = 1; i <= 12; i++) {
      MonthlyPayments.Add(InitializeMyObject(i, MonthlyPayments[i - 1]));
    }
  }

  public MonthlyPayment InitializeMyObject(int i, MonthlyPayment lastMonth) {
    MonthlyPayment x = new MonthlyPayment();
    x.Luna = i;
    x.SoldInitial = Math.Round(lastMonth.SoldCredit, 2);
    double s = 10000;
    double a = 0.10 / 100;
    double c = (0.5 / 12) / 100;;
    double d = (9.75 / 100) / 12;
    x.Dobanda = Math.Round(d * lastMonth.SoldCredit, 2);
    double v = 1 + d;
    x.PlataLunara = Math.Round((s * d) / (1 - Math.Pow(v, -12)), 2);
    x.Principal = Math.Round(x.PlataLunara - x.Dobanda, 2);
    x.Asigurare = Math.Round(a * lastMonth.SoldCredit, 2);
    x.Comision = Math.Round(c * s, 2);
    x.TotalLunar = Math.Round(x.Comision + x.Asigurare + x.PlataLunara, 2);
    x.SoldCredit = Math.Round(lastMonth.SoldCredit - x.Principal, 2);
    return x;
  }

And textbox:
<pre><TextBox x:Name="textBoxValoareCredit" Width="100" Height="30" Margin="10" VerticalAlignment="Bottom"/>
//textbox for a
//textbox foc c
.....


Now the data is fixed in the code, but I want to use the code above instead of 'SoldInitial',"a","c","d",value entered in this textbox:

What I have tried:

double SoldInitial, Dobanda,ComisionAnaliza,Asigurare,Comision;
           SoldInitial= float.Parse(textBoxValoareCredit.Text);
            Dobanda = float.Parse(textBoxDobanda.Text);
            ComisionAnaliza = float.Parse(textBoxComisionAnaliza.Text);
            Asigurare = float.Parse(textBoxAsigurare.Text);
            Comision = float.Parse(textBoxComisionLunar.Text);

How do I use the data entered in the textbox in my LoadMonthlyPayments() and MonthlyPayment InitializeMyObject() method?
Posted
Updated 12-May-21 1:28am

1 solution

You should first capture all the values from the textboxes. But do not use float.Parse, as it assumes that the text is valid, and if it is not, then your application could crash. Use TryParse to verify that you have valid numbers. You should also range check the converted values before you use them. Once you have captured and verified all the required data, then you can create a new instance of your class with the new values.
 
Share this answer
 
Comments
Member 15180481 13-May-21 2:10am    
I use TryParse now.But in what class do I have to do this operation? I did it in MainWindow.Xaml.Cs , but I don't know if it's good.
Richard MacCutchan 13-May-21 2:56am    
I cannot answer that as I do not know the design of your system. I would assume that you do it in the View portion.
Member 15180481 13-May-21 3:05am    
I have a datagrid in which I display something calculated with this information taken from the textbox
Richard MacCutchan 13-May-21 3:22am    
I have no idea what that is supposed to tell us.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900