Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:40:00+00:00 2026-06-01T04:40:00+00:00

I have been assigned to Convert a VB.NET project to C# and I got

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:40:01+00:00Added an answer on June 1, 2026 at 4:40 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help with a work project I have been assigned. At the
I have recently been assigned a CSS & design project that's in a CakePHP
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
Have been searching how to convert a dictionary to a string. But the results
I have been assigned to work on a Java/Flash/BlazeDS project. When I pull down
I have been assigned a project in which I need to send commands to
I have been assigned a project to develop a set of classes that act
I have been assigned to integrate a CMS to our .NET based website. The
I have been assigned to a project with no documentation and lots of unmanaged
I have been assigned a project with a lot of poorly written code that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.