I have a member variable which is a list of reference types. In a method I create and item, add it to the list. The item is then updated point to another instance but the instance in the list does not get updated (his is in some unit test code). Example looks like
Item localItem = new Item(arg1, arg2);
this.itemList.Add(localItem);
// Do some testing and assertions
localItem = new Item(arg3, arg4); // This does not update the instance of
// localItem in this.ItemList
// Do some more testing and assertions
I can update my tests to fix this but it still caught me by surprise. I supose the List wants to keep the original instance passed through the Add method and cares not if the local variable which was used to pass it in now points to something else. Can anyone maybe confirm this understanding or explain it more clearly?
Ofcourse not. The localItem is a reference to the actual object (not the object itself). The list contains also a reference to this same object. If you assign another item to localItem it now points to another object but the list has still a reference to the old object.
If you want the object in the list to update automatically you must introduce a new object that holds a reference to the item and add this one to the list
public class ItemReference
{
public Item Item { get; set; }
}
Now you can create a local item like this
ItemReference itemRef = new ItemReference();
itemRef.Item = new Item(arg1, arg2);
this.itemReferenceList.Add(itemRef);
itemRef.Item = new Item(arg3, arg4);
Because the list now has a reference to the same (unchanged) itemRef it "sees" the new item as well.
The list has to be declared as
List<ItemReference> itemReferenceList;
When you add localItem to the list you add a reference to the object instance (assuming Item is a reference type aka a class) - when you subsequently create a new Item instance the result is a new reference (similar to a pointer) - if you want to update the list you have to remove the old item and add the new item - these are two totally different instances. Alternatively you can modify any properties on localItem and these will be reflected when you access the item through the list.
Related
In a for loop, instead of declaring a new object and add it to my List, I just update the value of the old object and every time keep adding this old object to my List, why after a few loops all List elements become the same
foreach (vg_ts_VesselCashflow_CashFlow_Entity item in cashflow)
{
var result = new VslMonthlyCashflow_Record();
result.CapitalCost = item.CapitalCost;
result.CharterRevenue = item.CharterRevenue;
result.Date = item.Period;
result.DryDock = item.DryDock;
resultList.Add(result);
}
if (resultList != null)
return resultList;
//Compared with this:
var result = new VslMonthlyCashflow_Record();
foreach (vg_ts_VesselCashflow_CashFlow_Entity item in cashflow)
{
result.CapitalCost = item.CapitalCost;
result.CharterRevenue = item.CharterRevenue;
result.Date = item.Period;
result.DryDock = item.DryDock;
resultList.Add(result);
}
I expect my resultList to be updated but if I keep using the old object, when it loops 123 times, all elements in the List will be the same.
List#Add doesn't copy the object or anything like that, it just keeps a reference to the object you passed to it. In the second snippet, you keep adding the same object to the list multiple times. Each modification you perform on the object is visible through all the references pointing to it, including the local result variable and all the elements of the list.
I am writing a c# console program.
I have a function that returns a list of objects.
e.g the following will return a list of objects.
p.getList();
If I already know the index of the object I want to reference from the list, then how to I access this?
For example I want to do the following which obviously is incorrect:
p.getList()[Index]
This would give me the item in the list at index.
To get around this I have done the following:
List<MyObject> mylist = p.getList();
mylist[Index];
but the above seems inefficient, to have to create a copy just to reference a value.
Any tips on how I can access?
Thanks.
If you don't want the list, but an item and you know the Index then
var item = p.getList()[Index];
syntax is perfectly correct. Please, notice, that List<T> is a reference type, that's why in case of
var list = p.getList(); // reference copy, not the collection cloning
var item = list[Index];
...
var otherItem = list[otherIndex];
the var list = p.getList(); adds a miniscule overhead: it's reference, not the entire collection is being copied.
I am new to using ViewModels, I have a new list here and am adding items to it by looping though a database table. The issue is that all the records that come back are identical using the same record over and over. What could be the issue and is this a good way to accomplish filling with data and Passing a ViewModel or is there a better way? Right now it returns about 500 records with the same data.
public class DimCustomersController : Controller
{
private AdventureWorks_MBDEV_DW2008Entities db = new AdventureWorks_MBDEV_DW2008Entities();
public ActionResult CustomersIndexVM()
{
List<DimCustomersIndexViewModel> CustomerList = new List<DimCustomersIndexViewModel>();
DimCustomersIndexViewModel CustomerItem = new DimCustomersIndexViewModel();
foreach (var m in db.DimCustomers.ToList())// cold do for loop up to count
{
CustomerItem.Title = m.Title;
CustomerItem.FirstName = m.FirstName;
CustomerItem.MiddleName = m.MiddleName;
CustomerItem.LastName = m.LastName;
CustomerItem.BirthDate = m.BirthDate;
CustomerItem.MaritalStatus = m.MaritalStatus;
CustomerItem.Suffix = m.Suffix;
CustomerItem.Gender = m.Gender;
CustomerItem.EmailAddress = m.EmailAddress;
CustomerItem.AddressLine1 = m.AddressLine1;
CustomerItem.AddressLine2 = m.AddressLine2;
CustomerItem.Phone = m.Phone;
//other columns go here
CustomerList.Add(CustomerItem);
}
return View("CustomersIndexVM", CustomerList);
}
This line needs to be inside the loop:
DimCustomersIndexViewModel CustomerItem = new DimCustomersIndexViewModel();
The reason is that you want a new view model for each customer, but instead you are currently creating only one view model and changing its properties. When you add it to the list, you are not adding a copy; you are adding the same view model you already added.
This code would work if DimCustomersIndexViewModel was a struct, because structs are just a bag of values that have no inherent identity and they are copied rather than referenced. (Technical comparison.) But it's a class (as it should be), with a unique identity, so you're adding a reference to the single view model into the list over and over. Customerlist[0] and CustomerList[1] and all the other items point to the same DimCustomersIndexViewModel object instance, whose properties are then overwritten and left equal to the very last customer.
By moving this line inside the loop, you are creating a separate DimCustomersIndexViewModel for each customer, each with its own set of properties, and CustomerList contains references to many different DimCustomersIndexViewModel object instances.
Once you have solid experience with this concept, a future step could be to use AutoMapper so that you don't have to maintain a list of all properties in your code here.
The problem is you add the same reference object during each iteration of your loop. That object never changes (you never new it up again), but you change the properties on the object. Then you add that object over and over. You need to new up that object each iteration of the loop.
I'm trying to use a property of individual object instances stored within a List<T> object, but I can't seem to access the properties directly.
I have an object (sportsCarVehicle) which stores a user-defined name (strVehicleName) (amongst other properties, but that's not important) within itself. The object is then stored within a List<sportsCarVehicle> object called sportsCarVehicleStorage.
I need to access every instance of sportsCarVehicle in List<sportsCarVehicle> and pass the value of strVehicleName to a combo box on a form.
I assume I'll need some kind to loop to cycle through each instance and pass the name to the combo box, but my main issue is not being able to access the property I need. The sportsCarVehicle instances have no reference-able name.
One more thing I should note: the constructor for sportsCarVehicle is called within the sportsCarVehicleStorage.Add() method.
Any suggestions on how I could do this?
Cant you do this
List<string> lst = new List<string>{"Hello", "World"};
int len = lst[0].Length;
Here .Length is a property of string. As long as that property is public we can access it.
In your case
List<sportsCarVehicle> sportsCarVehicleStorage = new List<sportsCarVehicle>();
// Some code to populate list.
mycombobox.Items = sportsCarVehicleStorage
.Select(x => x.strVehicleName).ToArray();
Make Sure property strVehicleName is public in that class.
You can use foreach to loop through the list, assigning each member of the list to a named variable, like:
foreach (sportsCarVehicle scv in sportsCarVehicleStorage)
{
//scv is the name of the currently looping sportsCarVehicle object
//use scv.strVehicleName to access the property.
myComboBox.Items.Add(scv.strVehicleName);
}
foreach (SportsCarVehicle car in myListName)
{
//do stuff here
}
That's the most basic example, you can use PLINQ etc. to do it in a more streamlined way.
An alternative could be to bind the list of sportsCarVehicle directly to the comboBox, for example:
List<sportCarVehicle> sportsCarVehicleStorage= new List<sportsCarVehicle>;
// Set up list content here
// ...
myComboBox.DataSource = sportsCarVehicleStorage;
myComboBox.DisplayMember = "strVehicleName";
Let's say I have a list of objects and that I'm extracting and modifying an item from the list like this:
List<MyObject> TheListOfObjects = new List<MyObject>();
MyObject TheObject = new MyObject();
TheListOfObjects = //some json deserialization result
TheObject = (from o in TheListOfObject
where o.ID == SomeParameter
select o).SingleOrDefault();
TheObject.SomeProperty = SomeValue;
When I write TheObject.SomeProperty = SomeValue; am I:
modifying the item in the list and in which case I'm done or
modifying a new item and I must replace the original item in the list with the item I just modified.
Depends.
If the list of objects is a list of class instances, then TheObject variable will hold a value that is a reference. This reference will also still exist within the list. Modifications of the object at that reference will be visible in both. Important caveat: Writing over the reference contained in the variable (ie., variable reassignment) would not persist to the list, nor would writing over the reference in the list persist to the variable.
If the list of objects is a list of struct instances, then TheObject simply contains the value, and mutations of that value are not visible inside the list.
That depends on whether the MyObject is a class or a struct.
If it's a class, you're modifying the original object.
If it's a struct, you're modifying a copy.
Items in your list are held by reference, so you're modifying the same object - no need to try to copy it back into the list as it's already there.
You are modifying an item in the list. TheObject is a reference to an unique item in memory.
You can even create multiple lists, all lists will contain references to identical objects in memory.
There is something wrong with your code:
MyObject TheObject = new MyObject();
...
TheObject = (from o in TheListOfObject
where o.ID == SomeParameter
select o).SingleOrDefault();
You are first creating a new instance of the object, the affecting to TheObject a new value.
The = new MyObject(); part of your code is useless. Same for = new List<MyObject>();
You are modifying the item in the list as the call returns a reference to the item in the list, not a copy of it. Also, the object you create with
MyObject TheObject = new MyObject();
is just thrown away, as you change the reference to the newly selected item. You could omit that line and just do:
MyObject theObject = (from o in TheListOfObject
where o.ID == SomeParameter
select o).SingleOrDefault();
I assume that MyObject is a class, and not a struct, because we are mutating it with the operation, and mutable structs are evil