473,799 Members | 3,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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
7 3629
Plater
7,872 Recognized Expert Expert
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 New Member
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.
Apr 10 '12 #3
Plater
7,872 Recognized Expert Expert
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)
Apr 27 '12 #4
RhysW
70 New Member
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 New Member
Yeah, Plater, I know. That line gives me the [][*] error again though.
May 2 '12 #6
Plater
7,872 Recognized Expert Expert
Interesting. I have never seen anything fail on .GetType(), it should be a function on every single thing.
May 11 '12 #7
Charp
5 New Member
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
18821
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
2311
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" string etc.
8
13953
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 the array can vary depending on the parameters passed. What I need to do is loop through the returned array and if applicable write the array element to the screen.
6
8961
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 to search any longer. Currently I am using this code. But I think it's too slow, because it runs through whole dimension. I know this is trivial question, but is there any way to stop this loop, or better way to search? I mean - FASTER?
5
1508
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 separate the two items. Example. The string array looks like, the array is called Trainee :- Joe Bloggs,5678
2
2135
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(); That's simple. But to create any object knowing its object type on the fly is looking like a problem. I'll try to explain the idea.
11
17654
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 s(i)=new string(test) Above line gives - error implicit conversion string to 1-dim array of
4
3196
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 get public members but not protected, private, static members"
6
3871
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 there any way to do this with a variable length array? I've only been able to get it to work with a fixed size array. The relevant code snippets are below. Suggestions are greatly appreciated
8
2716
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") **************************** supposing that, there is a string array like
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.