![C#: Object to string array. [*] to [] error (Knowing both VB.NET as well as C# helps)](https://www.xszz.org/skin/wt/rpic/t11.jpg)
Question:
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
Answer1: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>
Answer3: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
Answer4:Try this:
foreach(var opc in oOpcServer.GetOPCServers().ToList()) {
cboServer.Items.Add(oOPCList[i]);
}
Answer5:You get this errors because oOPCList is an object. It doesn't contains lenght nor [] indexers. Use type returned by oOpcServer.GetOPCServers()