我不确定我是否以正确的方式使用struct。无论如何,我需要一个结构,其中一个属性是由另一个结构组成的数组。不知何故,我无法使用嵌套结构中的属性之一。
struct Clients
{
int client_number;
public ArrayList accounts;
}
struct JsonStruct
{
public ArrayList clients;
}
var obj = new JsonStruct();
obj.clients[0] = new Clients();
obj.clients[0].client_number = 100; // this last line does not work..
/* it throws this kind of error:
"object does not contain a definition for client_number.."
*/
obj.clients [0]现在应该具有客户的结构,但似乎没有。
答案 0 :(得分:0)
在阅读了OP的评论和要求之后,我得出了以下解决方案:
using System;
using System.Collections.Generic;
public class structCheck
{
public static void Main(string[] args)
{
var obj = new JsonStruct();
obj.clients.Add(new Clients());
obj.clients[0].client_number = 100;
Console.WriteLine(obj.clients[0].client_number);
}
}
class Clients
{
public int client_number;
public List<int> accounts = new List<int>();
}
class JsonStruct
{
public List<Clients> clients = new List<Clients>() ;
}
输出:
100
如果要添加内容,请告诉我。
答案 1 :(得分:-1)
您必须更改4件事:
client_number
设为公开,否则您的用户将无法访问它
代码((Clients) obj.clients[0]).client_number = 100;
。由于ArrayList包含类型为object
的项目,因此您必须将它们强制转换回Clients
制作新代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
class Clients
{
public int client_number;
public ArrayList accounts; // maybe you ma want to change this to a list<T> as well
}
class JsonStruct
{
public List<Clients> clients;
}
var obj = new JsonStruct();
obj.clients[0] = new Clients();
obj.clients[0].client_number = 100;