Results 1 to 6 of 6

Thread: [RESOLVED] Casting an Object into a List of Objects (List of Lists)

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    Resolved [RESOLVED] Casting an Object into a List of Objects (List of Lists)

    In My VB.NET Code, it is apparent that VB.NET will allow an object that is essentially a List to be looped through while C# does not. Below is the VB.NET code:

    Code:
    Dim dicData As New SortedDictionary(Of Integer, List(Of Object))
    For Each kvp As KeyValuePair(Of Integer, List(Of Object)) In dicData
       Dim mylist As New List(Of Object)
       mylist = kvp.Value
       For Each items In mylist  
         Dim x As Double = items(j - 2)   'Here, VB.NET "knows" each items is a List 
         Dim y As Double = items(items.count - 2)
         Dim z As Double = items(items.count - 1)
         '....
       Next
    Next
    The C# conversion is below, and Intellisense seems to have accepted it without errors (I have not run it yet).

    Code:
    foreach (KeyValuePair<int, List<object>> kvp in dicData)
    {
       List<object> mylist = new List<object>();
       mylist = kvp.Value.Cast<object>().ToList();
       foreach (var items in mylist)
         {
           var mylist2 = items as IList; //Will this do the trick?
           double x = Convert.ToDouble(mylist2[j - 2]);
           double y = Convert.ToDouble(mylist2[mylist2.Count - 2]);
           double z = Convert.ToDouble(mylist2[mylist2.Count - 1]);
           //....
       }
    }
    Last edited by pel11; Feb 1st, 2023 at 09:46 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width