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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Charp
    New Member
    • Mar 2012
    • 5

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

    Hi, 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:
    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:
    Code:
    for (int i = 0; i <= oOPCList.Length; i++)
    {
    cboServer.Items.Add(oOPCList[i]);
    }
    Event 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 just 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[], which I guess is pretty obvious if it means what I think it means.

    This is the VB.NET code that I am converting:
    Code:
    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
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You declared oOPCList as an object. You should declare it as a string[] (if that is really the type)

    string[] oOPCList;

    Or you can typecast it at point of usage, but I recomend declaring it to be string[] from the start

    Comment

    • Charp
      New Member
      • Mar 2012
      • 5

      #3
      I tried that and it did not work, instead i had to use the following code:
      IEnumerable<str ing> oOPCList;

      oOPCList = ((Array)(object )oOpcServer.Get OPCServers()).C ast<string>();

      I also had to change the for loop to a foreach loop:
      foreach (var item in oOPCList)
      cboServer.Items .Add(item);

      The strange cast first to object, then to Array, and then to IEnumerable<str ing> 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 InvalidCastExce ption. 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.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        What does this give you?
        string TypeName=oOpcSe rver.GetOPCServ ers().GetType() .Name;

        That should tell you the actual type being returned(actual ly it tells you the type then I am using the Name property because it is a quick identifier)

        Comment

        • RhysW
          New Member
          • Mar 2012
          • 70

          #5
          Additionally it also didnt have the parameter of length, usually the problem if something doesnt have length it will have count (a 1 based index not a 0 based index) just leaving this explanation here incase someone gets directed here from searching a problem with the same exception.

          Comment

          • Charp
            New Member
            • Mar 2012
            • 5

            #6
            Yeah, Plater, I know. That line gives me the [][*] error again though.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Interesting. I have never seen anything fail on .GetType(), it should be a function on every single thing.

              Comment

              • Charp
                New Member
                • Mar 2012
                • 5

                #8
                I was using RsiOPCAuto.dll library if you are curious an want to mess around with it yourself. I'm sure you can find it somewhere on the net.

                Comment

                Working...