Results 1 to 8 of 8

Thread: convert an object to string array

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2002
    Location
    Mumbai
    Posts
    10

    convert an object to string array

    Hi,
    Can anyone help me to convert an object into a string array or int array using C#?


    Thanks
    Pal

  2. #2
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    Can you give me a little more info?

  3. #3
    Lively Member
    Join Date
    Oct 2003
    Location
    Guildford, UK
    Posts
    91
    If it can be done, just say:

    object obj = something;
    int[] intarray = (int[])obj;

    Not totally sure about that but it seems to fit.

  4. #4
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    I don't think you can type cast as an array I thought you could only typecast the elements. ;\

  5. #5
    Hyperactive Member Hampster's Avatar
    Join Date
    Feb 2001
    Location
    On my hamster wheel.
    Posts
    374
    Originally posted by Tewl
    I don't think you can type cast as an array I thought you could only typecast the elements. ;\
    You can cast arrays:
    PHP Code:
    int[] intNew={0,1,2,3,7};
    foreach(
    int i in intNew)
                         
    Console.WriteLine("Before casting: "+i);
    object obj intNew;
    int[] intarray = (int[])obj;
    foreach(
    int i in intarray)
                    
    Console.WriteLine("After casting: "+i); 

  6. #6
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    i stand corrected then

  7. #7
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245

    Re: convert an object to string array

    Originally posted by pal_amu
    Hi,
    Can anyone help me to convert an object into a string array or int array using C#?


    Thanks
    Pal
    I'm not sure if these other responses helped you out or not, but another option may be to serialize your object and then deserialize it into a string array.
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  8. #8
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502
    Code:
    object test;
    //the object, offcourse you needs to have a value assigned
    
    char[] chrarray = new char[100]; //100 elements for example
    chrarray = (test.ToString()).ToCharArray();

    some (var-types) classes don't have their own (overridden) ToString() method, but simply inherrit it from the object class.
    In that case this will not work. But a lot of classes have their own. (The Integer classes have it, the string class itself has it too.) Instead of the ToString you can also use (string) as shown below...

    Code:
    object test; 
    //the object, offcourse you needs to have a value assigned
    
    char[] chrarray = new char[100]; //100 elements for example
    chrarray = ((string)test).ToCharArray();
    here you go !
    greetings
    Last edited by BramVandenbon; Mar 29th, 2004 at 11:33 AM.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

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