Solved

String Array Question - IM STUCK :P

Posted on 2006-11-28
2
1,343 Views
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
...etc...


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

WSDL
------------
<xsd:complexType name="AccountInformation">
        <xsd:sequence>
          <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" />
        </xsd:sequence>
      </xsd:complexType>

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)


References.cs
---------------
   /// <remarks/>
    [System.Xml.Serialization.SoapTypeAttribute("AccountInformation", "java:com.services.workflow.soap.data")]
    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;
    }


WebServiceComponent.cs
-------------------------------

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......

Then....in the C# wrapper.....

public string[] SearchAccountInfoByAccountNumber(string accountNumber)
{
      try
      {

            logInIfNecessary();
            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.


knox


0
Comment
Question by:sakibomber
  • 2
2 Comments
 
LVL 18

Expert Comment

by:DarrenD
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,

Darren





0
 
LVL 18

Accepted Solution

by:
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()
        {
            InitializeComponent();
        }

        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);

            MessageBox.Show(mystring[0]);
            MessageBox.Show(mystring[1]);
            MessageBox.Show(mystring[2]);
            MessageBox.Show(mystring[3]);
            MessageBox.Show(mystring[4]);
   
            // 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];
                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...

Darren
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now