Results 1 to 6 of 6

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

  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.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,039

    Re: Casting an Object into a List of Objects (List of Lists)

    There's plenty wrong with that VB code, but it looks like one of the issues is that you have Option Strict OFF. Without Option Strict, VB will essentially assume that you know what you are talking about. If you're wrong, you find out at runtime. If you're right, then the code is just somewhat inefficient, but it doesn't crash. After all, that VB code will compile just fine, even if the Objects in the List(of Object) are integers, dates, or a mix of a lot of different things. In fact, if you were to get the right mix of objects, then that code might even run fine, even when those Objects are not Lists at all.

    Also, this line:

    Code:
    Dim mylist As New List(Of Object)
    along with it's equivalent in the C# code, is incorrect. That creates a list, but the list is never used, which is a minor waste of memory. What the line should be is exactly the same, minus the "New". You don't need to create a list, you just need a variable that can hold a list.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    Re: Casting an Object into a List of Objects (List of Lists)

    Okay, got it, will set Strict On.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Casting an Object into a List of Objects (List of Lists)

    Quote Originally Posted by pel11 View Post
    Okay, got it, will set Strict On.
    You should set it On in the current project (all current projects) and also set it On in the VS options, so it will be On by default in all future projects. You should ALWAYS have Option Strict On at the project level and then turn it Off at the file level only in those files that explicitly require late binding. Even then, use partial classes to keep the code in those files to the absolute minimum.

  5. #5
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

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

    Quote Originally Posted by pel11 View Post
    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
    Out of interest why are you using Object with a Generic Collection? One of the main reasons the generic types exist is so you can have strongly typed code, if you are expecting a list of Doubles why not declare it as such?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,039

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

    I've seen that in certain methods of working with JSON data, so it doesn't surprise me all that much, but it could be indicative of a poor design choice.
    My usual boring signature: Nothing

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