Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:22:44+00:00 2026-06-15T15:22:44+00:00

I have two questions: 1) Is this the correct way to make my default

  • 0

I have two questions:

1) Is this the correct way to make my default value = 0 for variables, then pass a value given by the user into that variable?

protected void btnCheck_Click(object sender, EventArgs e)
    {
        lblYesNo.Text = "";
        //default int values are set to 0
        int remainder = 0;
        int guess = 0;

        remainder = int.Parse(txtRemainder.Text);

        guess = int.Parse(txtAnswer.Text);
        answer = (int)Session["answer"];
        if (guess == answer)
        {
            lblYesNo.Text = lblYesNo.Text + "Correct!";
        }
        else
        {
            lblYesNo.Text = lblYesNo.Text + "Try Again..";
        }
    }//END Check Answer

2) how can I stop the Check_Click(submit button) from hiding the txtRemainder(textbox)? The reason it ‘auto-hides’ now is because I set the default value to ‘txtRemainder.Visible = false;’ in the Page_Load, which will make it hide unless the math problem is division. When I click on the btnDiv_Click(divide button) it resets it to ‘txtRemainder.Visible = true;’, because this provides a division question to be solved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BasicMath : System.Web.UI.Page
{
    int number1;
    int number2;
    int answer;

    protected void Page_Load(object sender, EventArgs e)
    {
        txtRemainder.Visible = false;
    }

    protected void GetRandom()
    {
        Random rand = new Random();
        number1 = rand.Next(0, 10);
        number2 = rand.Next(0, 10);
        txtAnswer.Text = "";
        txtRemainder.Text = "";
        lblYesNo.Text = "";
    }//END Get Random Number

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        GetRandom();
        lblEquation.Text = number1.ToString() + " + " + number2.ToString();
        answer = number1 + number2;
        Session["answer"] = answer;
    }//END Addition Button

    protected void btnSub_Click(object sender, EventArgs e)
    {
        GetRandom();
        if (number2 > number1)
        {
            answer = number2 - number1;
            lblEquation.Text = number2.ToString() + " - " + number1.ToString();
        }
        else
        {
            answer = number1 - number2;
            lblEquation.Text = number1.ToString() + " - " + number2.ToString();
        }
        Session["answer"] = answer;

    }//END Subtraction Button

    protected void btnMult_Click(object sender, EventArgs e)
    {
        GetRandom();
        lblEquation.Text = number1.ToString() + " x " + number2.ToString();
        answer = number1 * number2;
        Session["answer"] = answer;
    }//END Multiplication Button

    protected void btnDiv_Click(object sender, EventArgs e)
    {

        Random rand = new Random();
        number1 = rand.Next(1, 10);
        number2 = rand.Next(1, 10);
        /*will only display the txtRemainder(textbox) while using the Divide button,
           txtRemainder will auto-hide when using another button because
           it's default setting of '.Visible = false' is placed in the Page_Load*/
        txtRemainder.Visible = true;
        lblEquation.Text = number1.ToString() + " / " + number2.ToString(); 
        answer = number1 / number2;
        Session["answer"] = answer;

    }//END Division Button

    protected void btnCheck_Click(object sender, EventArgs e)
    {
        lblYesNo.Text = "";
        //default int values are set to 0
        int remainder = 0;
        int guess = 0;

        remainder = int.Parse(txtRemainder.Text);

        guess = int.Parse(txtAnswer.Text);
        answer = (int)Session["answer"];
        if (guess == answer)
        {
            lblYesNo.Text = lblYesNo.Text + "Correct!";
        }
        else
        {
            lblYesNo.Text = lblYesNo.Text + "Try Again..";
        }
    }//END Check Answer
}

If this question doesn’t make sense please ask for clarification.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:22:45+00:00Added an answer on June 15, 2026 at 3:22 pm

    Question 1:

    Yes, that is the correct way to initialize your variables to default values of zero, although int variables are initialized to zero by default, so even if you omitted the assignment, they would still default to zero.

    The method you’re using to accept the user input is correct, but could throw an exception if the values entered by the user are not Int32 values (think 3.2). As it stands, you have no try..catch blocks to deal with these exceptions. You could either add these try..catch blocks, or you could use the TryParse() method to check that the values are valid. For example:

    protected void btnCheck_Click(object sender, EventArgs e)
    {
        lblYesNo.Text = "";
        //default int values are set to 0
        int remainder = 0;
        int guess = 0;
    
        if (!Int32.TryParse(txtRemainder.Text, out remainder))
        {
            // do something here to inform the user that remainder is invalid
            return;
        }
    
        if (!Int32.TryParse(txtAnswer.Text, out remainder))
        {
            // do something here to inform the user that answer is invalid
            return;
        }
    
        answer = (int)Session["answer"];
        if (guess > answer)
        {
            lblYesNo.Text = lblYesNo.Text + "Try Again..";
        }
        else if (guess < answer)
        {
            lblYesNo.Text = lblYesNo.Text + "Try Again..";
        }
        else
        {
            lblYesNo.Text = lblYesNo.Text + "Correct!";
        }
    }//END Check Answer
    

    TryParse() will convert the value into the output variable if it’s a valid value & return true to indicate success, otherwise it will return false if it was not able to perform the conversion.

    Question 2:

    In order to hide txtRemainder when the page first loads & then to keep it hidden if any button was clicked other than btnDiv, here is a proposed solution. First, the code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            lblTest.Visible = false;
        } else {
            foreach (string ctrl in Request.Form) {
                Control c = FindControl(ctrl);
                if (c is Button) {
                    txtRemainder.Visible = c.ID == "btnDiv";
                    return;
                }
            }
        }
    }
    

    Now for some explaining. When the page initially loads, the label is hidden (by the !IsPostBack check). If the request is indeed a postback, we go through the collection of returned form controls (contained in the Request.Form collection) & check to see if a button is present that could have caused the postback. Buttons are rendered in the HTML as <input type="submit" .. /> elements, and upon postback, only the button that was clicked is sent back in the Form collection, even if there are multiple submits on the page. A more detailed explanation can be found here

    Now, if a button did indeed cause the postback, then we check to see if this button was btnDiv. If it was, the comparison returns true & txtRemainder is displayed. If not, it is hidden.

    This way, there is no need to show or hide txtRemainder in each event handlers. You wouldn’t even need to set it to visible in the event handler for btnDiv.

    EDIT

    Based on the requirement listed in the comments, I’ve altered my answer to question 2 so as to provide an alternative method to having to show or hide the label in each button event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building this website and I have basically two questions about this page: http://sites.publishyours.com.br/silviamecozzi/pt/obra/deserto_das_palmas.php
I have two questions, actaully... First off, Why cant I do this: List<Object> object
I am not sure if people find this obvious but I have two questions:
This is two questions in one, so here we go. I have a simple
This is two part question: I have two stored procedures: sp1 & sp2. If
Following up this question , I have a further problem - I have two
I'm re-asking this question but with a different framework this time. I have two
I'm new to C++, so this question may be basic: I have two classes
I'm really new to JavaScript and jQuery. My question is this. I have two
This is a very noobish question, so I apologize in advance! I have two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.