将对象分配给对象集合时出错 [英] Error assign object to Collection of objects

查看:125
本文介绍了将对象分配给对象集合时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
请帮助我解决问题
我写了这段代码:

Hello
Please help me to resolve the issue
I wrote this code:

ExtrinsicObjectType extnObject = new ExtrinsicObjectType();
 extnObject.id ="Document01";
ClassificationType clsfic02 = new ClassificationType();
clsfic02.id = "Cl02";

extnObject.Classification = clsfic02; ERROR


无法隐式将类型分类类型转换为分类类型[]
这种类型的ClassificationType []是对象的集合
我该怎么办才能解决此问题?
如何声明clsfic02


Cannot implicitly convert type ClassificationType to ClassificationType[]
this type ClassificationType [] is collection of objects
What I have to do resolve this issue?
How I have to declare clsfic02

ClassificationType[]  clsfic02 = new ClassificationType();



谢谢



Thank you

推荐答案

如果要使用此类型初始化数组,则必须提供一个常数,该常数将设置数组的元素数. case ClassificatieType []将按以下方式初始化.

If you want to initialize an array with this type, you have to give a constant number that will set the number of element of the array.In your case the ClassificatieType[] will be initialize as follows.

int classificationSize = 10; // Size of element
ClassificationType[]  clsfic02 = new ClassificationType[classificationSize];


有关更多信息,请查看 C#对象数组 [


For more, take a look atC# Object Array[^]

I hope this will help you well.


显然,extnObject.ClassificationClassificationType[]类型,它是" ClassificationType的数组".您有两个选项可以解决语义问题:

1.代替extnObject.Classification = clsfic02;编写:extnObject.Classification = new ClassificationType[] { clsfic02 };

2.将ExtrinsicObjectType.Classification更改为ClassificationType类型,而不是ClassificationType[]

(第二个选项假定您具有ExtrinsicObjectType类的代码)

这只能解决编译器的问题.我建议检查这些类的逻辑/用途以及应如何使用它们.
Apparently, extnObject.Classification is of type ClassificationType[], which is an "array of ClassificationType". You have two options to solve the semantic issue:

1. Instead of extnObject.Classification = clsfic02; write: extnObject.Classification = new ClassificationType[] { clsfic02 };

2. Change ExtrinsicObjectType.Classification to be of type ClassificationType instead of ClassificationType[]

(The second option assumes you have the code of the ExtrinsicObjectType class)

This only solves the issue with the compiler. I recommend reviewing the logic/purpose of these classes, and how they are supposed to be used.


您好,
谢谢您的快速答复
我为数组分配了10个元素
请查看下面的代码是否正确

ExtrinsicObjectType extnObject = new ExtrinsicObjectType();
extnObject.id ="Document01";

分类类型[] clsfic02 =新的分类类型[10];
clsfic02 [0] = new分类类型();
clsfic02 [0] .id ="Cl02";
clsfic02 [0] .classifiedObject ="Document01";
clsfic02 [0] .nodeRepresentation ="ClassCode";

extnObject.Classification = clsfic02;
我出现了错误;

对象引用未设置为对象的实例
我正在构建SOAP信封
所有这些对象都是节点.

这是分类方法的定义

System.Xml.Serialization.XmlElementAttribute(分类"))
public分类类型[]分类{
得到{
返回this.classificationField;
}
设置{
this.classificationField =值;
}
}
谢谢您的帮助
Hi,
Thank you for quick response
I assigned 10 elements to the array
Please see bellow if it is correct code

ExtrinsicObjectType extnObject = new ExtrinsicObjectType();
extnObject.id ="Document01";

ClassificationType[] clsfic02 = new ClassificationType[10];
clsfic02[0] = new ClassificationType();
clsfic02[0].id = "Cl02";
clsfic02[0].classifiedObject = "Document01";
clsfic02[0].nodeRepresentation = "ClassCode";

extnObject.Classification = clsfic02;
Here I got error;

Object reference not set to an instance of an object
I am building SOAP Envelope
All those objects are nodes.

Here is the definition of Classification method

System.Xml.Serialization.XmlElementAttribute("Classification")]
public ClassificationType[] Classification {
get {
return this.classificationField;
}
set {
this.classificationField = value;
}
}
Thank you for your help


这篇关于将对象分配给对象集合时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆