简体   繁体   English

在ASP.NET Web应用程序中使用功能

[英]Using function in ASP.NET web application

Excuse me guys, i'm a beginner in c# and needed help and some guidance in creating a calculator web app. 不好意思,我是C#的初学者,在创建计算器Web应用程序时需要帮助和一些指导。 So i was given a task to create a calculator web app using the ASP.NET web application web form with the UI looking like this: 因此,我被赋予了使用ASP.NET Web应用程序Web表单和UI来创建计算器Web应用程序的任务,如下所示:

Calculator UI 计算器用户界面

The thing is, i made a mistake and made it using Windows Forms App (WFA) instead and i could get the calculator to work. 关键是,我犯了一个错误,而是改用Windows Forms App(WFA),可以使计算器正常工作。

But when i tried to make the calculator using the ASP.NET web application web form the calculator won't work because somehow the variable that i set to run the method didn't get any value unlike when i run it in the WFA. 但是,当我尝试使用ASP.NET Web应用程序Web表单制作计算器时,计算器将无法工作,因为以某种方式设置为运行该方法的变量没有任何值,这与在WFA中运行该变量不同。

Here is my code: 这是我的代码:

  namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        bool lastpressedIsOperation;

        string input = String.Empty;

        protected void num_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            //this.display.Text = "";
            input+=button.Text;
            if (display.Text == "0" && display.Text != null)
            {
                display.Text = button.Text;
            }
            else
            {
                display.Text += button.Text;
            }

        }

        protected void op_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            input+=button.Text;
            if (display.Text == "0" && button.Text == "-" && display.Text != null)
            {
                display.Text = button.Text;
            }
            else
            {
                display.Text += button.Text;
            }
        }

        protected void del_Click(object sender, EventArgs e)
        {

            //this.display.Text = input.ToString(0,input.Length);
        }

        protected void del_all_Click(object sender, EventArgs e)
        {
            this.display.Text = "0";
            this.input = string.Empty;
        }
        private void enter_Click(object sender, EventArgs e)
        {
            string inFix = input;
            string isValid = Validate(inFix);
            string rpn = ConvertToPostFix(isValid);
            string result = Convert.ToString(CalculateRPN(rpn));

            this.display.Text = result;
            input = result;

        }

        private static string Validate(string inFix)
        {
            StringBuilder newstring = new StringBuilder(inFix);
            Stack<int> lb_index = new Stack<int>();//stack for left bracket index
            Queue<int> rb_index = new Queue<int>();//stack for right bracket index
            char temp = '#';
            Console.WriteLine("temp: ", temp);


            for (int i = 0; i < newstring.Length; i++)
            {
                if (newstring[i] == '(')
                    lb_index.Push(i);
                else if (newstring[i] == ')')
                    rb_index.Enqueue(i);
                Console.WriteLine("temp: {0}", temp);
                if (newstring[i] == '-')//change unary - to ~
                {
                    if (temp.IsOperator())
                    {
                        newstring[i] = '~';
                    }
                }
                temp = newstring[i];
            }
            if (lb_index.Count == rb_index.Count)
            {
                bool bracket_valid = true;
                for (int i = 0; i < lb_index.Count; i++)
                {
                    if (lb_index.Pop() > rb_index.Dequeue())
                    {
                        bracket_valid = false;
                        break;
                    }
                }
                if (bracket_valid != true)
                {
                    newstring.Clear();
                    newstring.Append("Error, Bracket wrong");
                }
            }
            else if (lb_index.Count < rb_index.Count || lb_index.Count > rb_index.Count)
            {
                newstring.Clear();
                newstring.Append("Error, Bracket wrong");
            }
            Console.WriteLine("newstring = {0}", newstring);
            return newstring.ToString();

        }

The idea is i want to get the string from textbox after the user inputted the value using number and operation buttons and pressed the enter button. 我的想法是,我想在用户使用数字和操作按钮输入值并按下Enter键之后,从文本框中获取字符串。

The string is then validated first using Validate(inFix) , then formatted into postfix ConvertToPostFix(isValid) , which then calculated using CalculateRPN(rpn) . 然后首先使用Validate(inFix)验证字符串,然后将其格式化为后缀ConvertToPostFix(isValid) ,然后使用CalculateRPN(rpn)计算得出。

But i dont know why the isValid variable never get the value from Validate(inFix) which cause the other methods not working. 但我不知道为什么isValid变量从不从Validate(inFix)获取值,这会导致其他方法无法正常工作。 Is there some difference on how to use the function in ASP Web app form? 在ASP Web应用程序表单中如何使用该功能有什么区别? If so, how do i use method/function in this? 如果是这样,我该如何使用方法/功能?

And is there any better way to implement this so i can fulfill my task? 还有没有更好的方法来实现这一点,以便我可以完成任务?

I believe your problem is that you are setting: 我相信您的问题是您正在设置:

string inFix = input;

In your 在你的

enter_Click

method. 方法。

Yet, the 然而,

 input 

variable is initialized to: 变量初始化为:

string input = String.Empty;

So, each time the form is loaded, ie on an initial load or a postback, the input variable is re-initialized to an empty string. 因此,每次加载表单时,即在初始加载或回发时,输入变量都会重新初始化为空字符串。

I'll not reinvent the post here, but to resolve this, you need to do something like: 我不会在这里重新创建帖子,但是要解决此问题,您需要执行以下操作:

Session["INPUT"] = input;

whenever input is modified, and do something like: input = (string)Session["INPUT"]; 只要修改了输入,就执行以下操作:input =(string)Session [“ INPUT”]; To initialize the input variable on the page load. 在页面加载时初始化输入变量。 If it is the initial page load, the input variable will be null (ie the session variable Session["INPUT"] does not exist). 如果是初始页面加载,则输入变量将为null(即,会话变量Session [“ INPUT”]不存在)。

Basically, what I am saying is that, while the asp.net session stores the state of controls on the forms, it does not save the state of your own class variables that you add to the page. 基本上,我的意思是,尽管asp.net会话在窗体上存储控件的状态,但它不会保存添加到页面上的自己的类变量的状态。

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

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