List element by reference

Started by
3 comments, last by Dawoodoz 10 years, 11 months ago

I tried to refactor my dynamic array into a List from System.Collections.Generic in .NET to make the code look cleaner.

The problem is that the List is completely useless by only accessing elements by value so that I can't write to them.

"Expression is a value and therefore cannot be the target of an assignment."

Is there a better collection class template for Visual Basic .NET 2010 that access elements by reference?

Advertisement

Can you give an example of code generating this error?

Pretty sure you're doing something wrong then.


public class Thing
{
   public string Name { get; set; }
}

private static void Main(string[] args)
{
   var list = new List<int>
   { 1, 2, 3 };

   list[2] = 4;

   Debug.Assert(list[2] == 4);

   var thingList = new List<Thing>
   {
      new Thing { Name = "Dave" },
      new Thing { Name = "Bruce" },
   };

   thingList[1].Name = "Batman";

   Debug.Assert(thingList[1].Name == "Batman");
}

Just tested that code and it works fine.

Post some code.

Unless you're actually using a value type (i.e. a structure). In which case, it's hardly surprising that it's returned by value, since that is exactly what it's supposed to do.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

I got it to work when using getters and setters in classes.

Can you give an example of code generating this error?

I deleted that code to get it to work with a dynamic array again because I wanted to be on the safe side.

This topic is closed to new replies.

Advertisement