473,385 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

5
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:
Expand|Select|Wrap|Line Numbers
  1. public partial class FrmPartialMain : Form
  2. {
  3. RsiOPCAuto.OPCServer oOpcServer;
  4. public FrmPartialMain()
  5. {
  6. InitializeComponent();
  7. object RsiOPCAuto;
  8. object oOPCList;
  9.  
  10. oOpcServer = new RsiOPCAuto.OPCServer();
  11. oOPCList = oOpcServer.GetOPCServers();
  12.  
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:
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i <= oOPCList.Length; i++)
  2. {
  3. cboServer.Items.Add(oOPCList[i]);
  4. }
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. Friend Class frmPartialMain
  2. Inherits System.Windows.Forms.Form
  3. Dim oOpcServer As RsiOPCAuto.OPCServer
  4.  Private Sub frmPartialMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
  5.         Dim RsiOPCAuto As Object
  6.         Dim oOPCList() As Object
  7.         Dim i As Integer
  8.  
  9.         oOpcServer = New RsiOPCAuto.OPCServer
  10.         oOPCList = oOpcServer.GetOPCServers
  11.         For i = LBound(oOPCList) To UBound(oOPCList)
  12.             cboServer.Items.Add(oOPCList(i))
  13.         Next i
  14.  
Mar 30 '12 #1

✓ answered by Charp

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

oOPCList = ((Array)(object)oOpcServer.GetOPCServers()).Cast<s tring>();

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

7 3611
Plater
7,872 Expert 4TB
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
Apr 5 '12 #2
Charp
5
I tried that and it did not work, instead i had to use the following code:
IEnumerable<string> oOPCList;

oOPCList = ((Array)(object)oOpcServer.GetOPCServers()).Cast<s tring>();

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<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.
Apr 10 '12 #3
Plater
7,872 Expert 4TB
What does this give you?
string TypeName=oOpcServer.GetOPCServers().GetType().Name ;

That should tell you the actual type being returned(actually it tells you the type then I am using the Name property because it is a quick identifier)
Apr 27 '12 #4
RhysW
70
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.
Apr 30 '12 #5
Charp
5
Yeah, Plater, I know. That line gives me the [][*] error again though.
May 2 '12 #6
Plater
7,872 Expert 4TB
Interesting. I have never seen anything fail on .GetType(), it should be a function on every single thing.
May 11 '12 #7
Charp
5
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.
May 15 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Amy L. | last post by:
I have a class that contains a string array. However, I can't get this object to serialize in the xml output. Is there a trick to get a string to serialize? Thanks Amy.
1
by: psmith | last post by:
I am getting data back from a COM object. In the debugger, I get back an object as follows: result -> system.array -> {length=102} string | |--> ="company 1" string |--> = "company 2"...
8
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of...
6
by: Jozef Jarosciak | last post by:
Quickest way to find the string in 1 dimensional string array! I have a queue 1 dimensional array of strings called 'queue' and I need a fast way to search it. Once there is match, I don't need...
5
by: Garfield | last post by:
Hello I have a function that returns a string array. The string has a name and an ID number, they are separated by a comma. I cannot for the life of me by using the string methods get to...
2
by: Just D. | last post by:
All, Do we have a simple way to Create an object on the fly knowing just an object type? The usual design-time way is to write a code something like this: CObjectType obj = new CObjectType();...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
6
by: Arnshea | last post by:
(apologies for the crosspost) I'm working with an MFC based COM object. From C# I'd like to be able to call a method on the COM object that takes a string array and modifies the contents. Is...
8
by: David Lazos | last post by:
Hi All, I use Contains method of String object to determine if a string variable has another string, like that. *************************** ipAddress.Contains("127.0.0")...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.