none
can't convert object to string to show it in textfield ! RRS feed

  • Question

  • Hi,

    I searched a lot of on internet but I still can not convert my object to string to view the chosen index in textfield :(

    this is my screenshot on watch you can see the object and the value,.. how can I convert the object to view the customer name on index [2] in textfield for example ??

     WebApplication1.srv042.FilterItem Test = new WebApplication1.srv042.FilterItem();
    	 Test.Name = "Nummer";
             Test.Filter = "2510306";
             objects = service.GetDataFiltered("Klant", "", new WebApplication1.srv042.FilterItem[] { Test }, 1, 1);
             //txtCustName.Text = Convert.ToString(objects[2]);
             //txtCustName.Text =objects[2].ToString();
             //string[] cust = objects[2].Cast<string>().ToArray();
             //string[] cust = (string[])objects[2];
             

    -		objects	{object[1][]}		object[][]
    -		[0]	{object[6]}		object[]
    		[0]	"Customer: 2510306"	object {string}
    		[1]	"2510306"		object {string}
    		[2]	"J VAN PUFFELEN"	object {string}
    		[3]	"MALUSSTR 8 A"		object {string}
    		[4]	"BORCULO"		object {string}
    		[5]	"7271HV"		object {string}
    


    Wednesday, December 12, 2012 8:34 PM

Answers

  • My observation from the screenshot is that the array your referring to is 2-dimensional (ie. an array of array). So that to access the value "J VAN PUFFELEN", you should use (string)objects[0][2].

    In fact, the safer code is

    string temp = objects[0][2] as string;

    txtCustName.Text = (temp == null) ? string.Empty : temp;

    FYI -  'as' operator returns null when the object being converted (in this case) is not a string, otherwise the value contained in the object.


    Thanks, Murugesan M - Please Mark as the Answer, if this answers your question. Please vote as helpful, if this post is helpful.

    • Proposed as answer by Jason Dot Wang Friday, December 14, 2012 7:50 AM
    • Marked as answer by Jason Dot Wang Thursday, December 20, 2012 6:15 AM
    Wednesday, December 12, 2012 10:59 PM

All replies

  • I didn't quite understand what want to do, and why isn't what's commented working. If there's some error, please refer the error code and message.

    string unboxed = (string)obj;
    // unbox an object cointaining a string
    string converted = obj.ToString();
    // get the string value of an object

    These will be the only things you need to get a string (specially the second one, which works with alll objects). Then, you should assign the boxes' Text property to the string.

    someTextContainer.Text = converted;

    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel


    • Edited by JMCF125 Wednesday, December 12, 2012 8:49 PM adding info and correcting a typo
    Wednesday, December 12, 2012 8:45 PM
  • Hi,

    what is the type of "objects"? If that an array, you can do that:

    objects[2].ToString();

    to get a string, but if that a boxed array you can convert the object to an array and get the element as a string:

    ((MyObject[])objects)[2].ToString();

    Post the code of class from object, so we can understand your problem better.


    Koopakiller [kuːpakɪllɐ] http://koopakiller.ko.ohost.de/

    Wednesday, December 12, 2012 8:51 PM
  • My observation from the screenshot is that the array your referring to is 2-dimensional (ie. an array of array). So that to access the value "J VAN PUFFELEN", you should use (string)objects[0][2].

    In fact, the safer code is

    string temp = objects[0][2] as string;

    txtCustName.Text = (temp == null) ? string.Empty : temp;

    FYI -  'as' operator returns null when the object being converted (in this case) is not a string, otherwise the value contained in the object.


    Thanks, Murugesan M - Please Mark as the Answer, if this answers your question. Please vote as helpful, if this post is helpful.

    • Proposed as answer by Jason Dot Wang Friday, December 14, 2012 7:50 AM
    • Marked as answer by Jason Dot Wang Thursday, December 20, 2012 6:15 AM
    Wednesday, December 12, 2012 10:59 PM