6 Replies - 311 Views - Last Post: 19 September 2011 - 09:50 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 4
  • View blog
  • Posts: 529
  • Joined: 31-August 11

What does using Parentheses around (int) and stuff do?

Posted 18 September 2011 - 12:46 PM

Ok I'm assuming that it converts different types but why wouldn't you use parse? Or am I even right about that.

For example in the below code

pictureBox1.Top= (int)(pictureBox1.Top - (pictureBox1.Height * 0.025)); 
pictureBox1.Left = (int)(pictureBox1.Left - (pictureBox1.Width * 0.025));
pictureBox1.Height = (int)(pictureBox1.Height + (pictureBox1.Height* 0.05));
pictureBox1.Width = (int)(pictureBox1.Width + (pictureBox1.Width * 0.05));



Will zoom in on an image in a picturebox1 but why can't you use just

pictureBox1.Height = (PictureBox1.Height *2);

etc. etc.


I know this seems like a dumb question but yeah. What does (int) do to magically make this work.

Is This A Good Question/Topic? 0
  • +

Replies To: What does using Parentheses around (int) and stuff do?

#2 tlhIn`toq  Icon User is online

  • Closing in on 5,000
  • member icon

Reputation: 4926
  • View blog
  • Posts: 10,460
  • Joined: 02-June 10

Re: What does using Parentheses around (int) and stuff do?

Posted 18 September 2011 - 12:59 PM

It's called casting.
It casts the result from whatever form it comes in to the specified form.

So it might cast a float to an int, for example.




Resources, references and suggestions for new programmers. - Updated Sep 2011
Spoiler

Was This Post Helpful? 2
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 6431
  • View blog
  • Posts: 23,416
  • Joined: 12-June 08

Re: What does using Parentheses around (int) and stuff do?

Posted 18 September 2011 - 12:59 PM

It's a short cut way of expressly converting your types. It shows you what you want to convert it to versus letting the compiler decide or typing something long out.

It operates like the 'Convert.to<x>' class.
http://msdn.microsof...y/bds4fye2.aspx
Was This Post Helpful? 2
  • +
  • -

#4 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 4
  • View blog
  • Posts: 529
  • Joined: 31-August 11

Re: What does using Parentheses around (int) and stuff do?

Posted 18 September 2011 - 07:29 PM

View PosttlhIn`toq, on 18 September 2011 - 12:59 PM, said:

It's called casting.
It casts the result from whatever form it comes in to the specified form.

So it might cast a float to an int, for example.




Resources, references and suggestions for new programmers. - Updated Sep 2011
Spoiler


Thanks guys by the way.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3792
  • View blog
  • Posts: 6,387
  • Joined: 08-June 10

Re: What does using Parentheses around (int) and stuff do?

Posted 19 September 2011 - 08:01 AM

View Postmodi123_1, on 18 September 2011 - 02:59 PM, said:

It's a short cut way of expressly converting your types. It shows you what you want to convert it to versus letting the compiler decide or typing something long out.

It operates like the 'Convert.to<x>' class.
http://msdn.microsof...y/bds4fye2.aspx


Well, this might be more technical than the OP wanted, but casting is not the same as converting. You can't cast a string to an int, but you can Convert.ToInt32 it. Just a point of clarification.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Closing in on 5,000
  • member icon

Reputation: 4926
  • View blog
  • Posts: 10,460
  • Joined: 02-June 10

Re: What does using Parentheses around (int) and stuff do?

Posted 19 September 2011 - 08:44 AM

Agreed. Which is why I phrased it as 'casting'. Because putting a type in parenthesis is not converting, and does not act like convert.to.... as earlier mentioned. I just wasn't going to make a deal out of it. But since it has been brought up.

Attached Image

As you can see, using Curtis' example, if you try to cast an int to a string in the way you questioned it will fail and you will be told you cannot cast in that direction.

But on the next line we perform an explicit Convert.To and that is legal.

So, Modi, hate to say it but (int)somevalue is not like Convert. It is casting.
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 809
  • View blog
  • Posts: 1,946
  • Joined: 04-October 09

Re: What does using Parentheses around (int) and stuff do?

Posted 19 September 2011 - 09:50 AM

View PosttlhIn`toq, on 19 September 2011 - 08:44 AM, said:

But on the next line we perform an explicit Convert.To and that is legal.


Just to throw some more terms into the fray:

There are two types of casting: implicit and explicit.

Implicit casting is done without you having to specify the type to cast into, the compiler does this for you. An example is
int a = 5;
double d = a;
Here we see the Int32 type being cast into the Double type, but we don't tell it that is what we want.

Explicit casting requires that you specify the type to cast into. An example of this is
double d = 5.0;
int a = (int)d;
Without the type specifier, the compiler will complain and tell you that it can't do this implicitly and that an explicit cast exists.

For your own types you can create casts, both implicit and explicit
class Example {
    public int Value { get; private set; }
    public Example(int v) {
        Value = v;
    }

    static public implicit operator Example(int i) {
        return new Example(i);
    }

    static public explicit operator Int32(Example e) {
        return Value;
    }
}

With that class, we can now do this
int i = 15;
Example e = i;  // implicit

i = (Int32)e;  // explicit

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1