C#: Object to string array. [*] to [] error (Knowing both VB.NET as well as C# helps)


I have been assigned to Convert a VB.NET project to C# and I got stuck. I am using a class called RsiOPCAuto, but I don't think that I'll have to go into to much detail into explaining how it works. Let's just get on with my issue.

So basicly what i do is grabbing an object from my class using this code:

public partial class FrmPartialMain : Form { RsiOPCAuto.OPCServer oOpcServer; public FrmPartialMain() { InitializeComponent(); object RsiOPCAuto; object oOPCList; oOpcServer = new RsiOPCAuto.OPCServer(); oOPCList = oOpcServer.GetOPCServers();

So far, so good. By adding a watch I can see that oOPCList now have the value {string[1..4]}.

Now I want to put these four strings into a combo box. I do this with a simple for loop:

for (int i = 0; i <= oOPCList.Length; i++) { cboServer.Items.Add(oOPCList[i]); }

<em><strong>Edit: Scratch that, changed this to a much nicer foreach loop.</strong></em>

Even though this object now functions as a string array both the oOPCList.Length and (oOPCList[i]) get errors:

<strong>.Length:</strong> Error 1 'object' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

<strong>oOPCList[i]:</strong> Error 2 Cannot apply indexing with [] to an expression of type 'object'

I bet it's the simplest thing but I just can't see it, help is very much appreciated and if there's anything else you need to know be sure to ask :-)

PS. It might be worth mentioning that I have tried some different ways to convert the object to a string array but I continuously get an error telling me that I can not convert system.string[*] to system.string[].

This is the VB.NET code that I am converting:

Friend Class frmPartialMain Inherits System.Windows.Forms.Form Dim oOpcServer As RsiOPCAuto.OPCServer Private Sub frmPartialMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load Dim RsiOPCAuto As Object Dim oOPCList() As Object Dim i As Integer oOpcServer = New RsiOPCAuto.OPCServer oOPCList = oOpcServer.GetOPCServers For i = LBound(oOPCList) To UBound(oOPCList) cboServer.Items.Add(oOPCList(i)) Next i


You need to cast the return value of GetOPCServers to an object first, then to an Array, because this method returns a dynamic type. You can't directly cast to a string[] because strong-typed arrays that are not 0-based are not supported by C#. After the cast, you need to call Cast<string> to get a strong typed enumerable, over which you can iterate:

IEnumerable<string> oOPCList; oOpcServer = new RsiOPCAuto.OPCServer(); oOPCList = ((Array)(object)oOpcServer.GetOPCServers()).Cast<string>();

Furthermore, you would be better off using a foreach loop, because it is a lot more readable:

foreach(var item in oOPCList) cboServer.Items.Add(item); <hr />

The strange cast first to object, then to Array, and then to IEnumerable<string> via Cast<string> is needed because of the following:

GetOPCServers returns a dynamic type. Trying to access that dynamic instance in any way - even via a call to GetType triggers an InvalidCastException. Therefore, it first needs to be cast to object so it no longer is a dynamic type. After that, we can cast it to an Array, the only supported way in C# to work with non-zero-based arrays. But Array is not strong typed, so we append the call to Cast<string> to get a strong typed enumerable.


Firstly you can only access the members of an object available at the reference type. So if you put a string array into a field or variable of type object you'll only be able to access those members defined on object itself (such as ToString)<sup>†</sup>.

string[*] means the array is an array that is indexed at something other than zero, which usually means it's indexed starting at 1. (I can't remember off hand how to convert these but I will look it up.)

Edit: see <a href="https://stackoverflow.com/a/5733008/83264" rel="nofollow">this answer</a> on how to work with a non-zero based array in C#.

Whilst it's possible to create and work with such an array, its usage in C# is exceptional so you will have to refer to it via a variable of type Array as string[*] is not valid C#.

<sup>† It would still be possible to access them indirectly using Reflection.</sup>


You have declared oOPCList as an object which doesnt have a length property and hence cannot be iterated on. What does the oOpcServer.GetOPCServers() return? Is it an Array?

Declare oOPCList as the same type and you can iterate using for or foreach loop


Try this:

foreach(var opc in oOpcServer.GetOPCServers().ToList()) { cboServer.Items.Add(oOPCList[i]); }


You get this errors because oOPCList is an object. It doesn't contains lenght nor [] indexers. Use type returned by oOpcServer.GetOPCServers()


  • XOR logic in JSON schema for a list of boolean items
  • Regular expression : match either of two conditions?
  • Redirect standard input (read-host) to a Powershell script
  • Request BinaryRead in ASP.NET 5 (MVC6)
  • VB.NET 2012 Property Set on Property Get
  • XNU incudes in Kext
  • How to know which Linq statement produced the SQL on hand during runtime?
  • Easiest way to get current unix timestamp via XSL
  • Salesforce Different WSDL files and when to use
  • How to get current document uri in XSLT?
  • How to revert to previous XCode version?
  • d3 v4 drag and drop with TypeScript
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Why does access(2) check for real and not effective UID?
  • Illegal mix of collations for operation for date/time comparison
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Date difference with leap year
  • Where to put my custom functions in Wordpress?
  • Trying to switch camera back to front but getting exception
  • Build own AppleScript numerical error handling
  • Rearranging Cells in UITableView Bug & Saving Changes
  • R: gsub and capture
  • Numpy divide by zero. Why?
  • Benchmarking RAM performance - UWP and C#
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • Why joiner is not used after Sequence generator or Update statergy
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • File not found error Google Drive API
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • Is it possible to post an object from jquery to bottle.py?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Programmatically clearing map cache
  • Binding checkboxes to object values in AngularJs