|
-
Jul 19th, 2010, 12:26 AM
#1
Thread Starter
Frenzied Member
Type Casting Issue
Can anyone tell me ???Why i am getting Error.when i tried to run the following code.let me know please.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CastingDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Int16 a= 10;
Int32 b= 20;
Int16 c;
c = short(a + b);
MessageBox.Show(c);
}
}
}
Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.
-
Jul 19th, 2010, 12:35 AM
#2
Addicted Member
Re: Type Casting Issue
Here buddy try this out.
Code:
private void Form1_Load(object sender, EventArgs e)
{
Int16 a= 10;
Int32 b= 20;
Int16 c;
c = (short) (a + b);
MessageBox.Show(c.ToString());
}
-
Jul 19th, 2010, 01:16 AM
#3
Re: Type Casting Issue
Just for future reference, as I've read a lot of your posts, I'd like to point out that questions are asked like this:
Can someone tell me why this happens?
rather than like this:
Can someone tell me ?why this happens.
It's a small thing, I know, but why not do things the right way, especially when it's no more difficult than doing it the wrong way?
-
Jul 19th, 2010, 03:44 AM
#4
Thread Starter
Frenzied Member
Re: Type Casting Issue
i am still little confuse.why we write short inside the small braket (short).Can someone tell me why this happens? Clarify Please.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CastingDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Int16 a= 10;
Int32 b= 20;
Int16 c;
c = (short)(a + b);
MessageBox.Show(c.ToString());
}
}
}
additional why this following code is not working ???.
Code:
private void Form1_Load(object sender, EventArgs e)
{
Int16 a= 10;
Int32 b= 20;
Int32 c;
c = (long)(a + b);
MessageBox.Show(c.ToString());
}
Last edited by firoz.raj; Jul 19th, 2010 at 03:49 AM.
-
Jul 19th, 2010, 04:49 AM
#5
Re: Type Casting Issue
 Originally Posted by firoz.raj
i am still little confuse.why we write short inside the small braket (short).
Because that's how you cast.
 Originally Posted by firoz.raj
additional why this following code is not working ???.
Why would it work? You cast as type 'long' and then attempt to assign to a variable of type 'int'.
You seem to have missed the point of casting. The whole point of casting is to specify exactly what type an object is so that it can be used as that type. If you want to use an object as type 'int' then casting it as type 'long' makes no sense.
-
Jul 22nd, 2010, 04:05 AM
#6
PowerPoster
Re: Type Casting Issue
indeed, agreed with jmcilhinney. further more, it just would be inefficient to do so in terms of the processing internally.
boxing/unboxing also costs in terms of performance so only be sure you absolutely have to do it or a better design approach is to use the items you absolutely require with the right data type.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|