Jump to content

Need help converting variable types

Go to solution Solved by Little Bear,

figured it out myself :3

How can I convert a Boolean to an integer in C#, and vise versa? More preferably compare them in a numerical way?

Motivation is where, and what you make of it.

 

“It is relatively unusual that a physical scientist is truly an atheist. Why is this true? Some point to the anthropic constraints, the remarkable fine tuning of the universe. For example, Freeman Dyson, a Princeton faculty member, has said, ‘Nature has been kinder to us that we had any right to expect.'”  Albert Einstein

Link to comment
https://linustechtips.com/topic/621606-need-help-converting-variable-types/
Share on other sites

Link to post
Share on other sites

but according to your profile pick your jesus, jesus never needs help

Omega-  I5 6600k | Gigabyte GTX 1060 | Cougar Panzer | DDR4 16GB 3000MHz | MSI Z170 Gaming M5

               EVGA 650 GQ | AOC 60Hz Freesync Panels x2 | AOC 144hz Freesync Panel x1

Epsilon- I7 2700k | Asus GTX 970 | Corsair 780t | DDR3 8GB 1600MHz | EVGA Z68 FTW Mobo

               Corsair 750W G2 | Acer R240HY x2

Upsilon- i7 5500u | 6GB DDR3 | 720p 60Hz panel

Link to post
Share on other sites

2 minutes ago, 21rkosta said:

but according to your profile pick your jesus, jesus never needs help

I'm not Jesus, I am the dude on the right in the light orange tunic.

Motivation is where, and what you make of it.

 

“It is relatively unusual that a physical scientist is truly an atheist. Why is this true? Some point to the anthropic constraints, the remarkable fine tuning of the universe. For example, Freeman Dyson, a Princeton faculty member, has said, ‘Nature has been kinder to us that we had any right to expect.'”  Albert Einstein

Link to post
Share on other sites

lol

Omega-  I5 6600k | Gigabyte GTX 1060 | Cougar Panzer | DDR4 16GB 3000MHz | MSI Z170 Gaming M5

               EVGA 650 GQ | AOC 60Hz Freesync Panels x2 | AOC 144hz Freesync Panel x1

Epsilon- I7 2700k | Asus GTX 970 | Corsair 780t | DDR3 8GB 1600MHz | EVGA Z68 FTW Mobo

               Corsair 750W G2 | Acer R240HY x2

Upsilon- i7 5500u | 6GB DDR3 | 720p 60Hz panel

Link to post
Share on other sites

How would I get this to work                

 

if(currentAvailableMemory / 1024 << 1)
                {
                    string AvailableMemoryVocalMessage = String.Format("The current available memory in megabytes is: {0}", currentAvailableMemory);
                    synth.Speak(AvailableMemoryVocalMessage);
                }
                else
                {
                    string AvailableMemoryVocalMessage = String.Format("The current available memory in gigabytes is: {0}", currentAvailableMemory / 1024);
                    synth.Speak(AvailableMemoryVocalMessage);
                }

Motivation is where, and what you make of it.

 

“It is relatively unusual that a physical scientist is truly an atheist. Why is this true? Some point to the anthropic constraints, the remarkable fine tuning of the universe. For example, Freeman Dyson, a Princeton faculty member, has said, ‘Nature has been kinder to us that we had any right to expect.'”  Albert Einstein

Link to post
Share on other sites

6 minutes ago, M.Yurizaki said:

This:


if(currentAvailableMemory / 1024 << 1)

Will always execute unless it's 0.

its converting currentAvailableMemory to gigabytes, and seeing if it is 1 or over, because currentAvailableMemory is by default scaled in megabytes.

Motivation is where, and what you make of it.

 

“It is relatively unusual that a physical scientist is truly an atheist. Why is this true? Some point to the anthropic constraints, the remarkable fine tuning of the universe. For example, Freeman Dyson, a Princeton faculty member, has said, ‘Nature has been kinder to us that we had any right to expect.'”  Albert Einstein

Link to post
Share on other sites

figured it out myself :3

Motivation is where, and what you make of it.

 

“It is relatively unusual that a physical scientist is truly an atheist. Why is this true? Some point to the anthropic constraints, the remarkable fine tuning of the universe. For example, Freeman Dyson, a Princeton faculty member, has said, ‘Nature has been kinder to us that we had any right to expect.'”  Albert Einstein

Link to post
Share on other sites

10 hours ago, Little Bear said:

figured it out myself :3

It's nice to include the answer for anyone who might find it useful or in case there are better ways of achieving what you want that someone can tell you.

 

For reference, here are 3 ways I can think of at the moment that does it.

bool b = true;

// Option 1 - Convert class
int i = Convert.ToInt32(b);

// Option 2 - if else
int j;
if (b)
{
    j = 1;
}
else
{
    j = 0;
}

// Option 3 - ternary operator
int k = b ? 1 : 0;

I would avoid option 2 as it's needlessly verbose no matter how you format it, but it's up to you and your preference.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×