String Array Question - IM STUCK :P

Posted on 2006-11-28
Last Modified: 2008-01-09
I have a VB6 app that references a c# dll.  In my VB6 app, I make calls to this (dll) wrapper, which in turns makes webservice calls and returns data to the client VB6 app.  Most all data is returned in string array format.  

arr[1] RS=ITEM1
arr[2] ES=ITEM2
arr[3] BR=ITEM3

I can split the array in the VB6 code and then I can assign each value as needed.  Everthing works except that I am now introducing a string array being returned from the webservice call and its SCREWING ME ALL UP. :)

my c# wrapper appends this return code (RS) and delimeter (=) to the actual value of the webservice result set.

Everything works great except I now have a webservice call that returns a string[] instead of a regular string.  My C# knowledge is limited and Im struggling with it thus far.

Here is a portion of the code from the C# wrapper

<xsd:complexType name="AccountInformation">
          <xsd:element minOccurs="1" maxOccurs="1" name="masterAccountSegment" nillable="true" type="xsd:string" />
         <xsd:element minOccurs="1" maxOccurs="1" name="returnCode" type="xsd:int" />
          <xsd:element minOccurs="1" maxOccurs="1" name="accountNumber" nillable="true" type="xsd:string" />
          <xsd:element minOccurs="1" maxOccurs="1" name="tierOffer" nillable="true" type="xsd:string" />
          <xsd:element minOccurs="1" maxOccurs="1" name="accountOverlay" nillable="true" type="tp:ArrayOfString" />

The tierOffer and accountOverlay are new.  Notice that the accountOverlay type is ArrayOfString (Im assuming this is how I return a string array from the actual webservice call)

   /// <remarks/>
    [System.Xml.Serialization.SoapTypeAttribute("AccountInformation", "")]
    public class AccountInformation {
        /// <remarks/>
        public string masterAccountNumber;    

        /// <remarks/>
        public int returnCode;
        /// <remarks/>
        public string accountNumber;

       /// <remarks/>
       public string tierOffer;

       /// <remarks - Here is where the issue lies.  I have a string array. Its returning x number of strings in the array/>
       public string[] accountOverlay;


Here is where the call is made.  My VB6 app calls something like this...

myArray = oWebServiceComponent.SearchAccountInfoByAccountNumber(sAccountNum)

Then I parse it (in the VB6 app like this)....

30         astrSplit() = Split(astrAcctVals(0), "=")

40         If astrSplit(0) = RESULT_INDICATOR Then

50             If astrSplit(1) = "0" Then
                   '***The result set is good                    
250                For ll = 1 To UBound(astrAcctVals())
260                    If astrAcctVals(ll) <> "" Then
270                        astrSplit() = Split(astrAcctVals(ll), "=")
280                        Select Case UCase(astrSplit(0))
420                            Case ACCOUNT_NUMBER_INDICATOR
430                            txtAcctNum.Text = astrSplit(1)
440                            Case MASTER_ACCOUNT_NUMBER_INDICATOR
450                            txtMastAcctNum.Text = astrSplit(1)

ETC...... the C# wrapper.....

public string[] SearchAccountInfoByAccountNumber(string accountNumber)

            AccountService ws = computeAccountService();
                               /// HERE IS WHERE THE ACTUAL WEBSERVICE CALL IS MADE
            AccountInformation accountInfo = ws.getAccountInformationByAccountNumber(cookie, long.Parse(accountNumber));
                               ///THEN HERE IS WHERE MY ISSUES BEGIN!!!
            return GetAccountData(accountInfo);
      catch (Exception e)
            return wrapException(e);

HERE IS THE GetAccountData method...

//Private Method that create AccountData object
        private string[] GetAccountData(AccountInformation accountInformation)
            return new String[] {
               formatKeyCodeDescription(RESULT_INDICATOR,          accountInformation.returnCode, accountInformation.errorMessage),
               formatKeyValue(ACCOUNT_NUMBER_INDICATOR,            accountInformation.accountNumber),
               formatKeyValue(MASTER_ACCOUNT_NUMBER_INDICATOR,     accountInformation.masterAccountNumber),
               formatKeyValue(TIER_OFFER_INDICATOR,                  accountInformation.tierOffer),
               ///I had to create 'formatArrayKeyValue' because now I have a string array (GETTING - 'Cannot implicitly convert type 'string[]' to 'string'' )
               formatArrayKeyValue(ACCOUNT_OVERLAY_INDICATOR,            accountInformation.accountOverlay)

And lastly....Here is the formatKeyValue and formatArrayKeyValue (shouldnt I be able to overload this method and keep the names the same??? again.....Im new. :)  )

The prefix is the constant used above.. Her's the declarations..

            static private string DELIMITER  = "=";
            static private string ACCOUNT_NUMBER_INDICATOR  = "AC";
            static private string ACCOUNT_OVERLAY_INDICATOR = "AO";
            static private string MASTER_ACCOUNT_NUMBER_INDICATOR = "MA";
            static private string RESULT_INDICATOR = "RS";
            static private string TIER_OFFER_INDICATOR = "TO";

private String formatKeyValue(string prefix, string value)
            return prefix + DELIMITER + value;

private string[] formatArrayKeyValue(string prefix, string[] value)
           string[] arrValue;
           arrValue = new string[value.Length];
          for (int index = 0; index < value.Length; index++)
      arrValue[index] = prefix + DELIMITER + value[index];
      return arrValue;

Right now....when I build it.....I get  "Cannot implicitly convert type 'string[]' to 'string'"

QUESTION:  How can I incorporate the AccountOverlay string array into this existing code base??????

Thank you all so much for taking the time to look at this.


Question by:sakibomber
  • 2
LVL 18

Expert Comment

ID: 18037376
Hi sakibomber,

The error is because you are trying to create an array of strings using a string and and array of strings.

For example...

On the line:

'return new String[] {'
: You are saying create a new array of string.

'formatKeyValue(ACCOUNT_NUMBER_INDICATOR, accountInformation.accountNumber)'
: Populate the first element with this line of text.

'formatArrayKeyValue(ACCOUNT_OVERLAY_INDICATOR, accountInformation.accountOverlay)'
: Populate the second element in the new array with an already created array of string which is not possible as the second element is only expecting a string not and array of string.

To Fix this problem depends on what you want:

1: What version of .NET are you using?
2: Do you simply want to return an array of string which has
Element 0 = 'formatKeyValue(ACCOUNT_NUMBER_INDICATOR, accountInformation.accountNumber)'
Element1 = Element 0 of this array 'formatArrayKeyValue(ACCOUNT_OVERLAY_INDICATOR, accountInformation.accountOverlay)'
Element2 = Element 1 of this array 'formatArrayKeyValue(ACCOUNT_OVERLAY_INDICATOR, accountInformation.accountOverlay)'
Element3 = Element 2 of this array 'formatArrayKeyValue(ACCOUNT_OVERLAY_INDICATOR, accountInformation.accountOverlay)'

Also on the overloading point it is possible to overload the method like so:

        private string formatKeyValue(string prefix, string value)
            return prefix + DELIMITER + value;

        private string[] formatKeyValue(string prefix, string[] value)
            string[] arrValue;
            arrValue = new string[value.Length];
            for (int index = 0; index < value.Length; index++)
                arrValue[index] = prefix + DELIMITER + value[index];
            return arrValue;

This should not return any errors

Let me know,


LVL 18

Accepted Solution

DarrenD earned 300 total points
ID: 18037761
Here is some code which will return what you want.

Basically i create a new string array and pass it to each function by reference and resizr the array each time. This will have to be perormed unless you are running on .NET 2.0 which has a built in Resize method. It's not in 1.1

Just create a new test Windows form prject and copy in the following over the Form1 class in the code.

public partial class Form1 : Form
        string[] arr = new string[] { "RS=ITEM1", "ES=ITEM2", "BR=ITEM3" };
        static private string DELIMITER = "=";
        static private string ACCOUNT_NUMBER_INDICATOR = "AC";
        static private string ACCOUNT_OVERLAY_INDICATOR = "AO";
        static private string MASTER_ACCOUNT_NUMBER_INDICATOR = "MA";
        static private string RESULT_INDICATOR = "RS";
        static private string TIER_OFFER_INDICATOR = "TO";

        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            string[] mystring = new string[0] ;

            formatKeyValue(TIER_OFFER_INDICATOR, "HELLO", ref mystring);
            formatKeyValue(TIER_OFFER_INDICATOR, "BYE", ref mystring);
            formatKeyValue(ACCOUNT_OVERLAY_INDICATOR, arr, ref mystring);

            // Then simply return the string
            return mystring ;

        private void formatKeyValue(string prefix, string value, ref string[] mys)
            MyArrayResize(ref mys, 1);
            mys[mys.Length -1] = prefix + DELIMITER + value;

        private void formatKeyValue(string prefix, string[] value, ref string[] mys)
            MyArrayResize(ref mys, value.Length);
            //string[] arrValue;
            //arrValue = new string[value.Length];
            int j = 0;
            for (int index = mys.Length - value.Length; index < mys.Length; index++)
                mys[index] = prefix + DELIMITER + value[j];

        private void MyArrayResize(ref string[] ArrStringToResize, int ElementsToAdd)
            string[] NewArray = new string[ArrStringToResize.Length + ElementsToAdd];

            // copy the contents of the original array to the new array
            ArrStringToResize.CopyTo(NewArray, 0);

            // and then assign the new array to the old variable
            ArrStringToResize = NewArray;

Hope this helps,

Let me know if you need anything else...


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question