Results 1 to 3 of 3

Thread: What is the need for typecasting

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Answers
    16

    What is the need for typecasting

    hello friends,

    Actually enum contains a collection of integer constants.
    In button1_click i declared i as an integer only then what is the need for typecasting?

    enum gtb
    {
    sa=12

    }
    private void button1_Click(object sender, EventArgs e)
    {
    int i;
    // i = gtb.sa;
    i=(int)gtb.sa ;
    MessageBox.Show(i.ToString());

    }


  2. #2
    Junior Member
    Join Date
    Jul 2006
    Answers
    22

    Re: What is the need for typecasting

    Hi,

    enum gtb
    {
    sa=12

    }
    private void button1_Click(object sender, EventArgs e)
    {
    int i;
    // i = gtb.sa;
    i=(int)gtb.sa ;
    MessageBox.Show(i.ToString());

    }

    Here if u use like gtb.sa then u r trying to access the member name not the value. and in c# it takes everything like string that is the need we need to convert externally,so we need TypeCasting,if u want to get integer value u need to convert externally.


  3. #3
    Expert Member
    Join Date
    Dec 2007
    Answers
    138

    Re: What is the need for typecasting

    If you need the value from the enum then you have to type cast else enum will return the only name


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact