Results 1 to 4 of 4

Thread: How to convert object array to string array in VB.Net?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    5

    How to convert object array to string array in VB.Net?

    Hi there,

    I have a problem here for converting object of array to string of array in my VB.Net code. The object of array is quite long and contains 6 sets of data here.

    Dim varReturnResult As Object
    Dim varMapData As Object
    Dim StrMap() As String

    varMapData = varReturnResult(2)
    StrMap = Split(varMapData, vbLf)

    I encountered this error "Conversion from type 'Object()' to type 'String' is not valid." when trying to retrieve the StrMap value.

    Appreciate if somebody can help me here.

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

    Re: How to convert object array to string array in VB.Net?

    Your code doesn't really make sense. You aren't trying to convert an Object array to a String array. You are calling Split, which takes a single String object and splits it into a String array. You are passing it your 'varMapData' variable. That variable apparently refers to an Object array, not a String. How exactly are you expecting an Object array to be split on line breaks at all? Your question like saying that you have a pumpkin and you want to drive to town so how do you convert it to a car. The answer is that you don't. It's just not the right type of thing.

    Let's start at the beginning. You have a 'varReturnResult' variable and it is declared as type Object. You are apparently treating it as an array so why is it not declared as an array? Similarly for 'varMapData', i.e. why is it declared simply as Object when it is apparently an array? You should be declaring all your variables as the appropriate type for the objects that they will refer to. Where does this data come from in the first place and what does it look like?
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    5

    Re: How to convert object array to string array in VB.Net?

    Thanks for your advise...I've changed the declaration from object to array now. The data came from a server and I have to retrieve them out, trim out the unnecessary vbLF and finally arrange it back to a flat file line by line. Do you have a better way to do this?

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

    Re: How to convert object array to string array in VB.Net?

    That's all still a bit vague I'm afraid. You say that you have to remove line feeds and then write back to a file line by line, but that's just putting line feeds back in again. Also, we still don't really know what those first two variables contain or how it gets there.
    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

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