简体   繁体   中英

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. 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:

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.

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.

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.

The string is then validated first using Validate(inFix) , then formatted into postfix ConvertToPostFix(isValid) , which then calculated using CalculateRPN(rpn) .

But i dont know why the isValid variable never get the value from Validate(inFix) which cause the other methods not working. Is there some difference on how to use the function in ASP Web app form? 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"]; 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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