0
  1. How to assign data in arraylist from another class in c#?
  2. Following are my classes.
  3. I want to serialize and deserialize the data, but don't know how to assign data in 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);
    }
 }
meera
  • 1
  • 6
  • I would suggest you use List instead of ArrayList eg: List or what ever then you can do game.DiffAns.Add(result) – Surya Pratap Mar 09 '16 at 12:11
  • 2
    Why are you using an ArrayList, when it's [effectively obsolete](http://stackoverflow.com/a/5063253/4037348) since the release of .NET 2.0, 11 years ago? – Gediminas Masaitis Mar 09 '16 at 12:13
  • Can you plz give example how to use List here..I want different type in list here.. – meera Mar 09 '16 at 12:46

2 Answers2

0

As an ArrayList does not make any assumptions on the data includued you can add any datatype to it. To do so there are several possibilites, e.g. 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:

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. Better use List<T> which gives you compile-time type-security:

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
 };
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Can you help me in the following code, [Route("getjsondata")] public string jsonDataToSerialize(string question, string correctans, bool anstype, int index, ArrayList abc ) //is arraylist as parameter is correct here? { var game = new PlatanoJson { question = question, correctans = correctans, typeofans = anstype, indexofque = index, diffAns = abc //what to do for runtime data }; } – meera Mar 09 '16 at 12:54
0

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>        (......................
}

}