Как вызвать переменные из класса (.cs) в C# asp. net бэкэнде (файл .aspx.cs) - PullRequest
0 голосов
/ 10 января 2020

Как вызвать переменные из файла класса .cs в файл .aspx.cs и наоборот. У меня есть код ниже. Я уже поместил помещенные пространства имен в коде. Например, я хочу вызвать CantTakeLoan переменную в файле .aspx.cs.

.cs файл класса

    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)
            {

            }
        }
    }

1 Ответ

1 голос
/ 10 января 2020

Вы можете изменить свой код следующим образом:

    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);
    }

А затем в вашем 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)
            {

            }
        }

Вы также можете изменить функцию Calculator на конструктор и использовать вот так

   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);
    }

И в Aspx, если у вас есть метка для привязки текста ошибки

<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)
            {

            }
        }
...