C# COM Server returning string[] to classic ASP

I have had to write a C# COM Server using .NET to service an existing classic ASP web application.  So far so good in that I have been able to call simply functions which return say a single string or integer.

I have a call however that needs to return an array of strings.  The function is shown below.  There appears to be an issue with the marshalling of string[] back to the ASP page as when I try to iterate through the entries using a for loop I get a type mis-match.

How can I return an array of strings from this function?
public string[] EnumAssets(int instanceID, int locationID)
{
   List<string> listValues = new List<string>();
 
   // First ensure that the database is open and the user logged in
   if (ValidateOpenAndLoggedIn() == 0)
   {
	AuditWizardDataAccess awDataAccess = new AuditWizardDataAccess();
	AssetList listAssets = new AssetList(awDataAccess.GetAssets(locationID, AssetGroup.GROUPTYPE.userlocation ,false), true);
 
	// Iterate through the returned list of Asset objects and format them for return as strings
	foreach (Asset asset in listAssets)
	{
		if (asset.Auditable)
			listValues.Add(Pack(asset.Name, ICON_PERIPH, true, asset.AssetID));
		else
			listValues.Add(Pack(asset.Name, ICON_ASSET, true, asset.AssetID));
	}
   }
 
   // Convert the list to an array of strings and return it
   string[] returnValues = listValues.ToArray();
   return returnValues;
}

Open in new window

ChrisMDrewAsked:
Who is Participating?
 
MogalManicConnect With a Mentor Commented:
when you return a string array  .net COM interop is converting it to a SafeArray(string)
http://msdn.microsoft.com/en-us/library/sak564ww(VS.71).aspx

You could try returning a object array (object[]) the COM interop will return (i think) a VARIANT array.



0
 
ChrisMDrewAuthor Commented:
It appears according to that article that ALL type[] arguments are marshalled as SafeArray(type).  Given that object is marshalled as VARIANT I assume that it would be ,arshalled as SafeArray(VARIANT).  I'm not a VBScript expect so I'm not certain what VBScript would actually expect but I don't think it is a SafeArray.

Any ideas how to get this marshalled in such a way that my VBScript code which just declares a Dim can access the returned strings?
0
 
ChrisMDrewAuthor Commented:
After lots of experimenting I have arrived at a solution which I thought I would share.  It may not be the most tidy and is based on other code I found on the Internet but I spent so long searching that I thought it best to make this available here.

Even if you return object[] from a C# COM Server the actual elements of the array are still not determinable by VBScript.  What you seem to have to do is to return an object array containing objects.

The C# code to return an array (of strings but could be any type I expect) is shown below.  I have tested this using VBScript in classic ASP and the array can be accessed as expected and all array functions work.  While not an elegant solution it certainly works and no other changes I have made resulted in being able to acces the array.


public object[] EnumAssets(int instanceID, int locationID)
{
    List<string> listValues = new List<string>();
    listValues.Add("AAAA");
    listValues.Add("BBBB");
 
    // Return the list as an array of objects
    return ConvertToObjectArray(listValues);
}
 
 
 
/// <summary>
/// This is ued to convert a list of strings passed to an array of objects
/// </summary>
/// <param name="arrayIn"></param>
/// <returns></returns>
public object[] ConvertToObjectArray(List<string> listStrings)
{
	object[] arrayOut = new object[listStrings.Count];
	for (int i = 0; i < listStrings.Count; i++)
	{
		arrayOut[i] = listStrings[i];
	}
	return arrayOut;
} 

Open in new window

0
 
ChrisMDrewAuthor Commented:
Thanks for your help with this - it certainly pointed me in the right direction.  I have in fact found an answer and have posted it in case others need it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.