1

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

Edit: Scratch that, changed this to a much nicer foreach loop.

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

.Length: 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?)

oOPCList[i]: 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
2
  • 1
    Why convert? You should be able to use the types from the VB.NET assembly in any .NET language (assuming it is CLS compliant). Commented Mar 30, 2012 at 9:11
  • Is there the option to use ToArray(), e.g. oOpcServer = new RsiOPCAuto.OPCServer().ToArray(); Commented Mar 30, 2012 at 9:11

5 Answers 5

3

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

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.

Sign up to request clarification or add additional context in comments.

22 Comments

@Charp: string[*] means the array is indexed starting at 1. See the link in my answer on how to work with such an array.
@Charp: Please see update. It is not necessary to use the complex way described in Paul's link.
@Charp: Make sure you have a using System.Linq; at the top of your C# file.
@Charp: What is the return type of GetOPCServers() according to the obejct browser?
@Charp: That doesn't look like a method signature. Maybe you can post a screenshot?
|
1

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

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 this answer 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#.

† It would still be possible to access them indirectly using Reflection.

2 Comments

Looks like this was alot harder then I thought, since I'm working with an object variable I can not use that code, unfortunately. Not as far as I know anyway. If you think there's a way to use my code that way then I'd love to see it. Thank you for your answer.
@Charp: you should be able to cast the variable to type Array.
0

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

Comments

0

Try this:

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

1 Comment

If GetOPCServers does indeed return an object, that will not help either.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.