Results 1 to 8 of 8

Thread: [RESOLVED] List(Of String()) vs. List(Of mytype())

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2019
    Posts
    10

    Resolved [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.

  2. #2
    Addicted Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    180

    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.

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

    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:
    1. Dim x = myListtest(0)
    2.  
    3. x.mytypestring1 = ""
    4. 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.

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

    Re: List(Of String()) vs. List(Of mytype())

    Quote Originally Posted by bmwpete View Post
    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2019
    Posts
    10

    Re: List(Of String()) vs. List(Of mytype())

    Quote Originally Posted by jmcilhinney View Post
    The examples are a bit dodgy but that's not the issue being referred to here.



    Thanks for fast reply. I'm now using a class for it.

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2019
    Posts
    10

    Thumbs up Re: List(Of String()) vs. List(Of mytype())

    Quote Originally Posted by peter swinkels View Post
    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

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

    Re: List(Of String()) vs. List(Of mytype())

    Quote Originally Posted by Peter Swinkels View Post
    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
  •  



Click Here to Expand Forum to Full Width