-
Aug 18th, 2020, 04:56 AM
#1
Thread Starter
New Member
[RESOLVED] List(Of String()) vs. List(Of mytype())
List(Of String())
Code:
Dim myList As New List(Of String())
myList(0)(1) = ""
= no problem
List(Of mytype())
Code:
Public Structure mytype
Dim mytypestring1 As String
End Structure
Dim myListtest As New List(Of mytype)
myListtest(0).mytypestring1 = ""
= “Expression is a value and therefore cannot be the target of an assignment.”
Anybody who knows why I can't write to the variable in my type??
Last edited by dday9; Aug 26th, 2020 at 08:18 AM.
-
Aug 18th, 2020, 05:57 AM
#2
Addicted Member
Re: List(Of String()) vs. List(Of mytype())
myListtest does not have any members yet, if you add a mytype to the list first, then you can reference that object using its index.
-
Aug 18th, 2020, 06:03 AM
#3
Re: List(Of String()) vs. List(Of mytype())
It's because of the fundamental nature of value types, which structures are. If you have a collection of value types then, when you get an item, you are getting a copy of the object. In your code, myListtest(0) is a copy of the first object in the collection and your code would then set a property of that copy. The copy would then be immediately discarded and the object in the collection would be unaffected so your code would have done absolutely nothing. To stop people writing such code accidentally, the compiler disallows it, as you have just discovered. If you want to set a property of a structure instance in a collection then what you need to do is get a copy of the item and assign it to a variable, edit the copy and then replace the original with a copy of the copy:
vb.net Code:
Dim x = myListtest(0)
x.mytypestring1 = ""
myListtest(0) = x
You don't need to do that with classes because what you get is a reference to the same object rather than a copy of the object. This is one reason that you should generally use classes by default and generally only use structures for immutable types, i.e. types that cannot change once created.
-
Aug 18th, 2020, 06:04 AM
#4
Re: List(Of String()) vs. List(Of mytype())
Originally Posted by bmwpete
myListtest does not have any members yet, if you add a mytype to the list first, then you can reference that object using its index.
The examples are a bit dodgy but that's not the issue being referred to here.
-
Aug 26th, 2020, 01:53 AM
#5
Thread Starter
New Member
Re: List(Of String()) vs. List(Of mytype())
-
Aug 27th, 2020, 04:14 AM
#6
Re: List(Of String()) vs. List(Of mytype())
Just a quick tip:
In "'myList(0)(1) = """ you could replace the "(0)" bit with the .First method. You need to import the System.Linq namespace though. Same goes for the accessing the last item in a list, you use the .Last method. :-)
Also, if your question has been answered you might want to mark this thread as resolved.
Peter
-
Aug 27th, 2020, 04:20 AM
#7
Thread Starter
New Member
Re: List(Of String()) vs. List(Of mytype())
Originally Posted by peter swinkels
just a quick tip:
In "'mylist(0)(1) = """ you could replace the "(0)" bit with the .first method. You need to import the system.linq namespace though. Same goes for the accessing the last item in a list, you use the .last method. :-)
also, if your question has been answered you might want to mark this thread as resolved.
Peter
great! Thnx
-
Aug 27th, 2020, 04:54 AM
#8
Re: List(Of String()) vs. List(Of mytype())
Originally Posted by Peter Swinkels
Just a quick tip:
In "'myList(0)(1) = """ you could replace the "(0)" bit with the .First method. You need to import the System.Linq namespace though. Same goes for the accessing the last item in a list, you use the .Last method. :-)
Also, if your question has been answered you might want to mark this thread as resolved.
Peter
Not that that's wrong but, generally speaking, there's no good reason to call First on a List(Of T) or an array. Let's consider the following methods and why you might call them an why you might not.
First: gets the first item in an enumerable list and throws an exception if there are no items. Calling this method on an IEnumerable(Of T) reference makes sense because it cannot be indexed. Given that a List(Of T) or an array can be indexed and the code to do so is actually more succinct than calling First, there's really no good reason to call First in that case.
FirstOrDefault: gets the first item in an enumerable list or Nothing if there are no items. Calling this method on a List(Of T) or array makes sense if you don't know whether there will be any items as it does make your code more succinct by alleviating the need for a guarding If statement.
Single: gets the only item in an enumerable list and throws an exception if there is not exactly one item. This method should be called if and only if you specifically want an exception thrown for there being more than one item. Otherwise, it makes no sense to call it rather than First.
SingleOrDefault: gets the only item in an enumerable list or Nothing if there are no items and throws an exception if there is more than one item. This method should be called if and only if you specifically want an exception thrown for there being more than one item. Otherwise, it makes no sense to call it rather than FirstOrDefault.
Last: gets the last item in an enumerable list and throws an exception if there are no items. Calling this method on a List(Of T) or array does make sense from the perspective that it makes your code more succinct than using the Count or Length to index, but it does make it less efficient because the entire list must be enumerated. That's no big deal for small lists but it would be for large ones. If you had a list with a million items then it would be far more efficient to get the upper bound and then index.
LastOrDefault: gets the last item in an enumerable list or Nothing if there are no items. Like all the other OrDefault methods, this one has the benefit of not throwing an exception for an empty list. It has the same issue as Last for large lists though, so a guarding If statement and indexing is advised in such cases.
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
|