0

How do i call variables from a .cs class file to .aspx.cs file and the other way round. I have the code below. I have already placed placed namespaces int the code. For example i want to call CantTakeLoan variable in .aspx.cs file.

.cs class file

    public class LoanCalculator
    {
        public  decimal Amnt;
        public double MonthlyInstallment;
        private string ex;
        public decimal Calculator(double Amount, double Months)
        {
            try
            {
                var Rate = 0.75;
                MonthlyInstallment = Amount / ((Math.Pow(1 + Rate, Months) - 1) / (Rate * Math.Pow(1 + Rate, Months)));
                double LoanLimit = 0.25 * Salary;
                if( (MonthlyInstallment  < LoanLimit)
                {
                    CantTakeLoan = "Please Not That You Cant Take Loan";
                }
                else
                {                    
                }
            }
            catch (Exception ex)
            {
            }
             return Amnt;
        }
    }

.aspx.cs file

        protected void Unnamed_Click(object sender, EventArgs e)
        {
            try
            {
               //call variables from .cs file 
            }
            catch(Exception ex)
            {

            }
        }
    }
3
  • create an instance of LoanCalculator class in Unnamed_Click method Commented Jan 10, 2020 at 8:15
  • Like this LoanCalculator a =new LoanCalculator(); then how will i call the variable. Forgive me i am new to C# development. Commented Jan 10, 2020 at 8:22
  • a.MonthlyInstallment to call MonthlyInstallment variable Commented Jan 10, 2020 at 8:24

1 Answer 1

1

You can change your code like this:

    public class LoanCalculator
    {
        public  decimal Amnt;
        public double MonthlyInstallment;
        public string CanTakeLoan;
        private string ex;
        public decimal Calculator(double Amount, double Months)
        {
            try
            {
                var Rate = 0.75;
                double LoanLimit = 0.25 * Salary;
                if( (!CanTakeLoanFn(Amount, Months))
                {
                    // Do not assign anything here. Handle you text 
                    // where you need it.
                    // If you want to handle it here, then declare it as
                    // A class member
                    this.CantTakeLoan = "Please Not That You Cant Take Loan";
                }
                else
                {                    
                }
            }
            catch (Exception ex)
            {
            }
             return Amnt;
        }
    }

    public boolean CanTakeLoanFn(double Amount, double Months) {
        MonthlyInstallment = Amount / ((Math.Pow(1 + Rate, Months) - 1) / (Rate * Math.Pow(1 + Rate, Months)));
        double LoanLimit = 0.25 * Salary;
        return (MonthlyInstallment  > LoanLimit);
    }

And then in you Aspx:

protected void Unnamed_Click(object sender, EventArgs e)
        {
            try
            {
               var loanCalculator = new LoanCalculator();
               if(!loanCalculator.CanTakeLoan(value1, value2)) {
                    // Do your logic here
               }
            }
            catch(Exception ex)
            {

            }
        }

You can also change the Calculator function to constructor and use like this

   public class LoanCalculator
    {
        public  decimal Amnt;
        public double MonthlyInstallment;
        public string CanTakeLoan;
        private string ex;
        public LoanCalculator(double Amount, double Months)
        {
            try
            {
                var Rate = 0.75;
                double LoanLimit = 0.25 * Salary;
                if( (!CanTakeLoanFn(Amount, Months))
                {
                    // Do not assign anything here. Handle you text 
                    // where you need it.
                    // If you want to handle it here, then declare it as
                    // A class member
                    this.CantTakeLoan = "Please Not That You Cant Take Loan";
                }
                else
                {                    
                }
            }
            catch (Exception ex)
            {
            }
            this.Amnt = // Set the amount here
        }
    }

    public boolean CanTakeLoanFn(double Amount, double Months) {
        MonthlyInstallment = Amount / ((Math.Pow(1 + Rate, Months) - 1) / (Rate * Math.Pow(1 + Rate, Months)));
        double LoanLimit = 0.25 * Salary;
        return (MonthlyInstallment  > LoanLimit);
    }

And in Aspx if you have a label to bind error text

<asp:label id="myLabel" runat="server" />
protected void Unnamed_Click(object sender, EventArgs e)
        {
            try
            {
               var loanCalculator = new LoanCalculator(value1, value2);
               if (!loanCalculator.CanTakeLoanFn(value1, value2)) {
                   myLabel.Text = loanCalculator.CanTakeLoan;
                   // Or better yet without the CanTakeLoan
                   myLabel.Text = "Please Not That You Cant Take Loan";
               }
               loanCalculator.Amnt // this is the amount variable
               loanCalculator.CanTakeLoan // this is the string variable with the text
            }
            catch(Exception ex)
            {

            }
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Lets say i have i have put my logic there how do i then echo the output in the form?
Depends on the Control you want to use. Which one is it?
Don't forget to accept the answers if they solve your problem :) Glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.