-
Feb 1st, 2023, 09:20 AM
#1
Thread Starter
Lively Member
[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.
-
Feb 1st, 2023, 10:13 AM
#2
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
-
Feb 1st, 2023, 09:44 PM
#3
Thread Starter
Lively Member
Re: Casting an Object into a List of Objects (List of Lists)
Okay, got it, will set Strict On.
-
Feb 1st, 2023, 10:30 PM
#4
Re: Casting an Object into a List of Objects (List of Lists)
Originally Posted by pel11
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.
-
Feb 2nd, 2023, 08:07 AM
#5
Re: [RESOLVED] Casting an Object into a List of Objects (List of Lists)
Originally Posted by pel11
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?
-
Feb 2nd, 2023, 04:08 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|