Results 1 to 6 of 6

Thread: Type Casting Issue

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question 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.

  2. #2
    Addicted Member
    Join Date
    Feb 2008
    Posts
    207

    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());
            }

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question 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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Type Casting Issue

    Quote Originally Posted by firoz.raj View Post
    i am still little confuse.why we write short inside the small braket (short).
    Because that's how you cast.
    Quote Originally Posted by firoz.raj View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width