Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ochreous · Jun 01, 2013 at 12:45 AM · c#if-statementsdeclare

C# if If Statement is True Declare Variable

Hi everyone, how do declare a variable inside of an if statement? I want to declare a variable when a specific if statement is true. But I'm not sure how.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by hiddenspring81 · Jun 01, 2013 at 12:50 AM

You cannot actually do that, but here's an alternative option. Suppose that you have a class Foo,

 Foo instance = null;
 if (x == 1)
 {
   instance = new Foo(); 
 }

Does that solve your problem? If not, let me know more specifically what the problem is.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ochreous · Jun 01, 2013 at 02:46 AM 0
Share

So basically what I would have to do is create a variable, set it to null and in the if statement have it equal to a new variable.Does null set it so it can't be used?

avatar image hiddenspring81 · Jun 01, 2013 at 03:38 AM 0
Share

The reason that you have to declare instance before the if statement is that you need to make sure the variable will exist, regardless of which code path the application executes at runtime. For example, if your code was

 // This won't work
 if (x == 1)
 {
   Foo instance = new Foo();
 }
 instance.fooBar();

Your compiler would choke, because instance was declared inside of the if statement. At runtime, if x was equal to 2, then it would never assign Foo instance = new Foo(), and the instance variable wouldn't exist.

To make the compiler happy, you just need to declare instance before the if statement. That way, regardless of whether or not the if statement is satisfied, the instance variable will always exist. Assigning it a value of null is just a convenient default value. Assigning a variable null essentially means that it has no value.

avatar image Ochreous · Jun 01, 2013 at 04:35 AM 0
Share

Is there a way to set it so it can't be used unless the if statement is true?

avatar image hiddenspring81 · Jun 01, 2013 at 06:08 PM 0
Share

Soft of. You would just need to place null-checks before you attempt to you use the variable.

 Foo instance = null;
 if (x == 1)
 {
   instance = new Foo();
 }
 
 // Check if instance has been assigned, then do stuff with it
 if (instance != null)
   instance.FooBar();

Don't worry about instance "wasting" memory. Because instance is null, it's not consu$$anonymous$$g any memory whatsoever.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Character Controller Collision If Statement 2 Answers

C# Declaring Variables Within If Statements 2 Answers

How to check if an object is colliding with another from another script ? 1 Answer

How to check if string array contains duplicates C# 1 Answer

Finding Distance between Angles and Points 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges