简体   繁体   English

如何从C#中的另一个类分配ArrayList中的数据

[英]How to assign data in an ArrayList from another class in c#

  1. How to assign data in arraylist from another class in c#? 如何从C#中的另一个类分配arraylist数据?
  2. Following are my classes. 以下是我的课程。
  3. I want to serialize and deserialize the data, but don't know how to assign data in arraylist. 我想序列化和反序列化数据,但不知道如何在arraylist中分配数据。

 public class PlatanoJson  //this is first class where arraylist is declared  
 {  
    public string question { get; set; }  
    public string correctans { get; set; }  
    public bool typeofans { get; set; }  
    public int indexofque { get; set; }  
    public ArrayList DiffAns = new ArrayList();  
 }  

 public class JsonSerializationController : ApiController  
 {  
    [Route("getjsondata")]  
    public string jsonDataToSerialize()  
    {  
        var game = new PlatanoJson {  
            question = "What is your education?",  
            correctans = "MCA",  
            typeofans = true,  
            indexofque = 3,  
            DiffAns =   //how to add data here??  
        };  

        var result = Newtonsoft.Json.JsonConvert.SerializeObject(game);  

        var dresult = Newtonsoft.Json.JsonConvert.DeserializeObject<PlatanoJson>(result);  

        return (" Question="+dresult.question+" Correct Answer="+dresult.correctans+" Type of ans="+dresult.typeofans+" Index of Que"+dresult.indexofque+" Answers choice="+dresult.DiffAns);
    }
 }

As an ArrayList does not make any assumptions on the data includued you can add any datatype to it. 由于ArrayList不会对包含的数据做任何假设,因此可以向其中添加任何数据类型。 To do so there are several possibilites, eg using an object-initializer: 为此,有几种可能性,例如使用对象初始化程序:

var game = new PlatanoJson {
    question = "What is your education?",
    correctans = "MCA",
    typeofans = true,
    indexofque = 3,
    DiffAns = new ArrayList { 1, 2, 3, "myString" }
 };

Alternatively you can also use the constrcutor expecting an ICollection : 另外,您也可以使用期望ICollection的构造函数:

DiffAns = new ArrayList(new object[] { 1, 2, 3, "myString" })

However I would recommend to go with time and use the egeneric appraoches instead. 但是,我建议您花些时间并改用通用方法。 With ArrayList you have no way to constraint which objects your list may contain, you can (as you can see in the code above) put inetegeres, strings and any different datatype into it. 使用ArrayList您无法限制列表中可能包含的对象,可以(如在上面的代码中看到的)将inetegeres,字符串和任何其他数据类型放入其中。 Better use List<T> which gives you compile-time type-security: 更好地使用List<T> ,它为您提供编译时类型安全性:

var game = new PlatanoJson {
    question = "What is your education?",
    correctans = "MCA",
    typeofans = true,
    indexofque = 3,
    DiffAns = new List<int>{ 1, 2, 3 }  // you van´t put a string into this list
 };

Why don't you change your code to something like this below(if the list contains string). 您为什么不将代码更改为以下内容(如果列表包含字符串)。

public class PlatanoJson  //this is first class where arraylist is     declared{
public string question { get; set; }
public string correctans { get; set; }
public bool typeofans { get; set; }
public int indexofque { get; set; }
public List<string> DiffAns { get; set; }
}


 public class JsonSerializationController : ApiController
  {
[Route("getjsondata")]
public string jsonDataToSerialize()
{
var list= new List<string>();
list.Add("your stuff1");
list.Add("your stuff2");
list.Add("your stuff3");
    var game = new PlatanoJson {
           question = "What is your education?",
        correctans = "MCA",
        typeofans = true,
        indexofque = 3,
        DiffAns = list;  
    };
        var result = Newtonsoft.Json.JsonConvert.SerializeObject(game);
    var dresult = Newtonsoft.Json.JsonConvert.DeserializeObject<PlatanoJson>        (......................
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM