Solved

String Array Question - IM STUCK :P

Posted on 2006-11-28
2
1,354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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