Results 1 to 3 of 3

Thread: Convert.ToXXXX(object) vs (XXX)object

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Location
    Mesa, Arizona
    Posts
    45

    Convert.ToXXXX(object) vs (XXX)object

    What is more effecient to use when converting data types in C#.
    Convert.ToXXXX(object) method or (XXXXX)object way?

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    i find the Convert.To method the best
    eg:
    Code:
    string str="i'm some text " + Convert.ToChar(169);///169 is copyright symbol
    MessageBox.Show(str);
    in this example you can see another use of Convert , calculating time span differences :
    Code:
    private void button8_Click(object sender, System.EventArgs e)
    {
    	try
    	{
            string strDate="11/06/2003 10:20:15";
    	string strFinish="11/06/2003 18:28:37";
            TimeSpan s=Convert.ToDateTime(strFinish).Subtract(Convert.ToDateTime(strDate));
    	MessageBox.Show("Difference in time:\n" + s.ToString());
    	}
    	catch
    	{
            System.Exception f = new System.Exception();
    	MessageBox.Show(f.Message);
    	}
    }
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by dynamic_sysop
    i find the Convert.To method the best
    eg:
    Code:
    string str="i'm some text " + Convert.ToChar(169);///169 is copyright symbol
    MessageBox.Show(str);
    in this example you can see another use of Convert , calculating time span differences :
    Code:
    private void button8_Click(object sender, System.EventArgs e)
    {
    	try
    	{
            string strDate="11/06/2003 10:20:15";
    	string strFinish="11/06/2003 18:28:37";
            TimeSpan s=Convert.ToDateTime(strFinish).Subtract(Convert.ToDateTime(strDate));
    	MessageBox.Show("Difference in time:\n" + s.ToString());
    	}
    	catch
    	{
            System.Exception f = new System.Exception();
    	MessageBox.Show(f.Message);
    	}
    }
    for casting numbers to char i prefer a lot using (char)number as a char is just a kind of typedef of a ushort(from 0 to 65535 if i am not mistaken) so if u put a number from 0 to 65535(altough i think when using regular encoding you just use from 0 to 255) no error will happen and the code gets a lot easier to read from if u have many chars u want to convert
    Last edited by PT Exorcist; Jun 15th, 2003 at 12:03 PM.
    \m/\m/

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