Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

List<T> C#

Tdata

Sponsor

Simple enough question I would suppose. How do you edit an Item in a List>

Code:
 

List<String> Str = new List<String>();

Str.Add("a"); 

Str.Add("b");

 

Str[1] = "c"; <---  Tells me it isn't a variable...

 

 

So, how would I do it?
 
Tdata,

The code you provided is accurate; that is: for me, it compiles.

If it says the name 'Str' is undeclared, then you're trying to use it out of scope.

Please give a more detailed example of what isn't working.
 

Tdata

Sponsor

Hmmm...

Code:
 

*note re is a List<Research> object.  Research is a struct I use to format my data.

while (v < data.Count)

            {

                int val = Convert.ToInt32(data[v]);

                re[val].have = true;

                //MessageBox.Show(re[val].have.ToString() + " " + val.ToString());

                v++;

 

            }

 
When I try to debug this, I am told that re[val].have can not be assigned because it is not a variable.
 
Data structures are aptly named, that is, they are simple structures of data. As such, when you pass them around, you're passing around a copy of that data.

So, assuming you have a List<Research> called re, if you go:
re[val].have = true;

While the statement itself is valid, its effect isn't quite what is intended, which is why it's viewed as an error. Since it's always passed around as a copy, and indexing in C# is basically a parameterized property named 'Item' with a single parameter named 'index', when you assign 'have' you're assigning the value 'have' to a copy of the Research data structure that is stored in the list.

The reason for this error is due to the .NET Common Language Infrastructure. All types can be classified into two major categories: Value types and Reference types. Value types, like Enums and Structs, are always passed around by their 'value'. So when you pass a value type (say a struct named Research) via a parameter, if someone changes a value on that value-type instance, the original value remains unchanged. This is because it's simply copied every time you pass it around.

Take the following example:
Research a = new Research();
a.have = true;
Research b = a;
b.have = false;
Console.WriteLine(a.have); //Is still true!

If 'Research' was a class, the value printed to the console would be false, as intended.

This is why assigning a value to a member of an element of a List of Research structs is not valid. You're basically assigning the value to a copy of your intended target; thus, a wasted operation.
 
This looks like its just become WAAAAAY too over complicated to simply say "there is nothing wrong with your code".

The code you showed at the top is perfectly valid and the correct way to edit an element in a list, so i would hazard a guess as to say either where you declared your reference in your real code was out of scope, or it would have said Null Reference Exception if it was not initilized correctly...

One thing to be weary of though, is as im sure someone mentioned you are using a generic list, but some value type objects (structs) can not be updated they can only be overwritten... i can bore you with that another day if needed... but if you are still having problems post the real code and we can try and help... but here is what i mean:

Code:
 

public class SomeEntity

{

   // Point is a struct (valuetype)

   public Point Position {get;set;}

 

   public SomeEntity()

   { Position = new Point(); }

} 

 

SomeEntity Entity = new SomeEntity();

Entity.Position.X = 10; // Error it is not a variable (this is because you would have to overwrite that memory due to it being valuetype)

 

Entity.Position = new Point(10, Entity.Position.Y); // Correct as you are overwriting the memory

 
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top