Jump to content
  • Advertisement
Sign in to follow this  
Dawoodoz

List element by reference

This topic is 2025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!