locked
how to read data in list<object> RRS feed

  • Question

  • User-158363518 posted

    how i can save getDataFa and read and save data :   lst_dataItem_1  , lst_dataItem_2 

    public List<object> getDataFa()
    {

    List<object> iData = new List<object>();

    List<string> lst_dataItem_1 = new List<string>();

    List<int> lst_dataItem_2 = new List<int>();

    // populate lst_dataItem_1 , lst_dataItem_2 and add them to iData

    return iData 

    }

    Thursday, January 31, 2019 10:46 AM

Answers

  • User1120430333 posted

    You can try it I don't know if it's going to work for you.

    var liststring = new List<string>();
    var listint = new List<int>()
    
    for (int i = 0; i < listobject.count;  i++)
    {
        if (i == 0)
        {
            liststring = (List<string>)listobject[0].object;
                
        }  
    
        if (i == 1)
        {
            listint = (List<int>)listobject[1].object;
                
        }  
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 31, 2019 1:36 PM

All replies

  • User1120430333 posted

    Object is the base object for all objects in .NET, which you can use as a container object to hold  such as an int, string,  etc.. and etc. objects.

    But in order to get the object out of Object used as a container, you must know the object type in the container Object and cast it back

    var theint = (int)Object;

    I can't say that what you are doing is an optimal approach if you don't know the object type in an Object container at the time of the cast.

    Thursday, January 31, 2019 12:05 PM
  • User-158363518 posted

    i wrote this 

    List<object> objectList = new List<object>();
    objectList = getDataFa(); // i have a string list and a int list in this object  

    how i can get that list from objectList 

    Thursday, January 31, 2019 12:31 PM
  • User1120430333 posted

    You can try it I don't know if it's going to work for you.

    var liststring = new List<string>();
    var listint = new List<int>()
    
    for (int i = 0; i < listobject.count;  i++)
    {
        if (i == 0)
        {
            liststring = (List<string>)listobject[0].object;
                
        }  
    
        if (i == 1)
        {
            listint = (List<int>)listobject[1].object;
                
        }  
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 31, 2019 1:36 PM
  • User-158363518 posted

    tanks a lot. 

    liststring = (List<string>)listobject[0].object;

    ERROR : Identifier expected; 'object' is a keyword

    i changed it to 

    liststring = (List<string>)listobject[0];

    and work correctly .  

    Thursday, January 31, 2019 1:45 PM