C#:对象到字符串数组。 [*]为[]错误(懂两种VB.NET和C#帮助) [英] C#: Object to string array. [*] to [] error (Knowing both VB.NET as well as C# helps)
问题描述
我被分配到一个转换VB.NET工程到C#和我被困。我使用了一个名为RsiOPCAuto类,但我不认为我会进入到太多细节到解释它是如何工作的。让我们只得到与我的问题。
那么basicly我做什么用这个code从我的类被抓的对象:
公共部分类FrmPartialMain:表单
{
RsiOPCAuto.OPCServer oOpcServer;
公共FrmPartialMain()
{
的InitializeComponent();
反对RsiOPCAuto;
反对oOPCList; oOpcServer =新RsiOPCAuto.OPCServer();
oOPCList = oOpcServer.GetOPCServers();
到目前为止,一切都很好。通过添加一块手表,我可以看到oOPCList现在有值{字符串[1..4]}。
现在我想把这四根弦成一个组合框。我做一个简单的for循环:
的for(int i = 0; I< = oOPCList.Length;我++)
{
cboServer.Items.Add(oOPCList [I]);
}
的 修改:从头开始,改变了这一个好得多的foreach循环的
虽然这个对象,现在作为一个字符串数组都oOPCList.Length和(oOPCList [I])得到错误:
。长度:
错误1'对象'不包含'长度',没有扩展方法长度接受型对象的第一个参数的定义可以找到(是否缺少using指令或程序集引用?)
oOPCList [我]:
错误2无法应用与[]索引到类型的前pression对象
我敢打赌,这是最简单的事情,但我就是不能看到它,帮助是非常AP preciated,以及是否有其他任何你需要知道的一定要问: - )
PS。这可能是值得一提,我已经尝试了一些不同的方法将对象转换为字符串数组,但我不断地得到一个错误,告诉我,我不能转换system.string [*]到system.string []。
这是VB.NET code,我转换:
友元类frmPartialMain
继承System.Windows.Forms.Form中
昏暗oOpcServer作为RsiOPCAuto.OPCServer
私人小组frmPartialMain_Load(BYVAL eventSender作为System.Object的,EventArgs的BYVAL作为System.EventArgs)把手MyBase.Load
昏暗RsiOPCAuto作为对象
昏暗oOPCList()作为对象
昏暗我作为整数 oOpcServer =新RsiOPCAuto.OPCServer
oOPCList = oOpcServer.GetOPCServers
对于i = LBOUND(oOPCList)为UBound函数(oOPCList)
cboServer.Items.Add(oOPCList(I))
接下来,我
您需要 GetOPCServers
的返回值强制转换为对象
第一,然后再切换到阵列
,因为这个方法返回一个动态
键入。你不能直接转换为的String []
,因为强类型的数组,其不为0,基于不被C#支持。中投后,您需要调用演员LT;串>
来得到一个强类型枚举的,在这你可以迭代:
的IEnumerable<串GT; oOPCList;oOpcServer =新RsiOPCAuto.OPCServer();
oOPCList =((阵列)(对象)oOpcServer.GetOPCServers())演员LT;串>();
此外,你会过得更好使用的foreach
循环,因为它是一个很大的可读性:
的foreach(在oOPCList VAR项)
cboServer.Items.Add(项目);
奇怪投率先对象
,然后阵列
,然后的IEnumerable&LT ;串>
通过演员LT;串>
,是因为以下的需要:
GetOPCServers
返回动态
键入。试图访问动态实例以任何方式 - 甚至通过调用的GetType
触发 InvalidCastException的
。因此,首先需要将其转换为对象
,使其不再是一个动态的类型。在此之后,我们就可以将其转换为阵列
,在C#中唯一支持的方式与非基于零阵列工作。但阵列
不强类型化的,所以我们追加调用演员LT;串>
来得到一个强类型枚举。
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
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.
这篇关于C#:对象到字符串数组。 [*]为[]错误(懂两种VB.NET和C#帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!