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

  • Home
  • SEARCH
  • 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 8295743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:43:14+00:00 2026-06-08T14:43:14+00:00

Can I create an instance of an object within an if statement? I’ve made

  • 0

Can I create an instance of an object within an if statement?
I’ve made 2 checkboxes in order to controll which constructor I use, but i get the error message ” The name “mth” does not exist in the current context ”

if (checkBox1.Checked && checkBox2.Checked)
        {
            Facto mth = new Facto(label3, wait_time, progressBar1);
        }
        else if(checkBox1.Checked==false && checkBox2.Checked)
        {
            Facto mth = new Facto(label3,wait_time);
        }
        else if (checkBox1.Checked && checkBox2.Checked == false)
        {
            checkBox1.Checked = false;
            Facto mth = new Facto();
        }
        else
        {
            Facto mth = new Facto();
        }


        int result = mth.Factorial(number);

What am I doing wrong? I’m new to C# and I don’t really have the hang of it yet.
Any help would be appreciated.
Thanks in advance.

  • 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-08T14:43:15+00:00Added an answer on June 8, 2026 at 2:43 pm

    This is a scoping problem. The variable mth only exists within the scope (brackets in your case) that it’s defined in. As soon as you leave the scope the variabel is no longer available. Since you use the mth variable at the end of your code (and outside the scope) you get this error. In order to fix this you need to define the variable at a higher scope. Note that you don’t have to assign it there.

    This leads to something like (Note that I reformatted your brackets to make it easier to see the scope levels)

        Facto mth; // Define it as the most outer scope level you are using it
        if (checkBox1.Checked && checkBox2.Checked)
        {
             mth = new Facto(label3, wait_time, progressBar1);
        }
        else 
            if(checkBox1.Checked==false && checkBox2.Checked)
            {
                mth = new Facto(label3,wait_time);
            }
            else 
                if (checkBox1.Checked && checkBox2.Checked == false)
                {
                    checkBox1.Checked = false;
                    mth = new Facto();
                }
                else
                {
                    mth = new Facto();
                }
    
       int result = mth.Factorial(number);
    

    EDIT: I would advice to always use {} brackets on every if and else even if they are not strictly required like in your case. As you can see in the layout it’s not so easy to see where your first else ends and that the “int result line isn’t part of it.

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

Sidebar

Related Questions

Is there an equivalent of selectors for classes? How can I create a instance
Can we able to create two instance of log4j in class one to write
Can you elaborate what goes behind the scene when we create DirectoryEntry instance? Code
I can create and use dynamic two dimensional array in Fortran (in 77 standard).
I can create database manually by going to cpanel. But I'd like to create
Here's the (potential) problem: I create a COM object, and then use a 'foreach'
How I can implement dynamic Jtree, which shows created instances of clases? For example,
You can create a table model and add it to a table TableModel tm
How can create a string in JSON after a mysql select: $ritorno = '{Prodotto:'.$riga['Prodotto'].',Prezzo:'.$riga['Prezzo'].'}'
If I create an instance of a class in Java, why is it preferable

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.