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
Charp
  • 198
  • 1
  • 2
  • 15
  • 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). – Oded Mar 30 '12 at 09:11
  • Is there the option to use ToArray(), e.g. oOpcServer = new RsiOPCAuto.OPCServer().ToArray(); – Paul Grimshaw Mar 30 '12 at 09:11

5 Answers5

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.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Thank you for your answer! I like the foreach loop, looks very nice. Didn't even think of that one! I do still get the error; "Unable to cast object of type 'System.String[*]' to type 'System.String[]'." on "oOPCList = (string[])oOpcServer.GetOPCServers();". – Charp Mar 30 '12 at 09:39
  • 1
    @Charp: string[*] means the array is indexed starting at 1. See the link in my answer on how to work with such an array. – Paul Ruane Mar 30 '12 at 09:51
  • 1
    @Charp: Please see update. It is not necessary to use the complex way described in Paul's link. – Daniel Hilgarth Mar 30 '12 at 09:59
  • Hmm seems like this was harder than i origianlly expected. Again, very much appreciate you helping me, both of you. I do not seem to be able to add .Cast after OPCServer() for some reason, though. – Charp Mar 30 '12 at 11:52
  • 1
    @Charp: Make sure you have a `using System.Linq;` at the top of your C# file. – Daniel Hilgarth Mar 30 '12 at 11:53
  • Yes it is there. I can't add anything after OPCServer() as it seems. I tried making a new variable that equals oOPCList.Cast() but I'm not sure what type it should be, string[] did not work. – Charp Mar 30 '12 at 12:25
  • 1
    @Charp: What is the return type of `GetOPCServers()` according to the obejct browser? – Daniel Hilgarth Mar 30 '12 at 12:34
  • ([object Node = System.Type.Missing]) – Charp Mar 30 '12 at 12:41
  • 1
    @Charp: That doesn't look like a method signature. Maybe you can post a screenshot? – Daniel Hilgarth Mar 30 '12 at 12:45
  • Sorry I just started this account to ask this question, I have never used this forum before. How do I post a screen shot here? – Charp Mar 30 '12 at 12:52
  • 1
    Just put it on imgur.com and post the link or edit your question and choose Image from the toolbar. – Daniel Hilgarth Mar 30 '12 at 12:53
  • This is all the text i get from GetOPCServers() in the Objects Browser: public virtual dynamic GetOPCServers([object Node = System.Type.Missing]) Member of RsiOPCAuto.OPCServerClass – Charp Mar 30 '12 at 12:53
  • Still getting that same error "Unable to cast object of type 'System.String[*]' to type 'System.String[]'." – Charp Mar 30 '12 at 13:07
  • 1
    @Charp: Impossible, in my code there is no such cast... Please make sure you have it exactly like in my answer. – Daniel Hilgarth Mar 30 '12 at 13:31
  • I commented out every other code and wrote your code again from scratch, here's what happens: http://imgur.com/CwZq0 – Charp Mar 30 '12 at 13:52
  • 1
    @Charp: Ah, you forgot to mention that this is a runtime error. Sorry, please try the edited answer ;-) – Daniel Hilgarth Mar 30 '12 at 13:55
  • 1
    If this doesn't work, try this: `foreach(string item in ((Array)oOpcServer.GetOPCServers()))` or `foreach(string item in ((IEnumerable)oOpcServer.GetOPCServers()))` – Daniel Hilgarth Mar 30 '12 at 13:56
  • The updated code gives me two new errors but it fixed the runtime error, the first foreach loop in your comment gave me the same [] error again and the second gave me one error which i fixed buy adding after IEnumerable. But I still get the runtime error. – Charp Mar 30 '12 at 14:09
  • 1
    @Charp: I assume that the returned array isn't one dimensional, is this possible? – Daniel Hilgarth Mar 30 '12 at 14:11
  • I'm not gonna lie, I have never worked with multidimensional arrays, is there any way for me to check this? – Charp Mar 30 '12 at 14:15
  • 1
    @Charp: Wow... OK, the problem is that a dynamic type is involved. Please see updated answer. This finally should work... – Daniel Hilgarth Mar 30 '12 at 14:47
  • IT WORKED! You Sir are amazing. Can't believe you stuck by me for 5 hours. Thank you very, very much. – Charp Mar 30 '12 at 14:58
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.

Community
  • 1
  • 1
Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
  • 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 Mar 30 '12 at 13:15
  • @Charp: you should be able to cast the variable to type Array. – Paul Ruane Mar 30 '12 at 19:21
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

ganeshran
  • 3,512
  • 7
  • 41
  • 69
0

Try this:

foreach(var opc in oOpcServer.GetOPCServers().ToList()) {
    cboServer.Items.Add(oOPCList[i]);
}
Paul Grimshaw
  • 19,894
  • 6
  • 40
  • 59
0

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

Sarrus
  • 586
  • 7
  • 21