Share via


How to modify struct field

Question

Friday, October 30, 2009 2:30 AM

OK, I have looked EVERYWHERE for what I'm trying to find out and can't find an answer, so please forgive me if my question is already answered somewhere.

How do you modify a C# struct field? Can struct field values be set only by a constructor?

struct Car
{
   int mileage, speed;
   public Car(int mileage, int speed)
   {
      this.mileage = mileage;
      this.speed = speed;
   }
}

I create a car like so:

Car driversCar = new Car(0, 0);

If I need to change the mileage, I want to do it like so:

driversCar.mileage = 14000;

This doesn't work.

Can a value be assigned to a struct field only by using the struct's constructor? Would I have to do this to change mileage to 14,000:

driversCar = new Car(14000, driversCar.speed);

Please do not tell me why I can't assign a new value to a struct field by direct assignment (driversCar.mileage = 14000). I know it's because driversCar.mileage is only returning a copy of the field. I just want to know if, outside of what may be possible by using reflection, struct fields can be changed at all, or if I have to always use the constructor and create a new struct with the values I need.

All replies (8)

Friday, October 30, 2009 2:42 AM ✅Answered | 2 votes

The reason you cannot reassign the variable .mileage is because you haven't declared it as public.  If you exclude a modifier, it defaults to private.

You have to declare it like...

public int mileage;

I hope this helps.

Trecius

P.S.  You might also want to make the struct public as well.  In the case you use the struct outside your defined scope.

EXA:  public struct Car
         {
                ...
         }


Friday, October 30, 2009 3:18 AM ✅Answered | 2 votes

Short answer: It has to do with value type and reference type.  Because a struct is ALWAYS a value type, you're returning a copy of the value stored in the collection.

Solution:  You should make your Car a REFERENCE type; to do this you just change...

public struct Car

to...

public class Car

 

Trecius


Friday, October 30, 2009 3:20 AM ✅Answered | 2 votes

Once you come to think about it, you could also just replace the value in your collection.  For example, if you have a List<Car>, you could do the following...

Car car = lstCars[32];
car.mileage = 1024;
lstCars[32] = car;

Hope it helps.

Trecius


Friday, October 30, 2009 3:00 AM

The reason you cannot reassign the variable .mileage is because you haven't declared it as public.  If you exclude a modifier, it defaults to private.

You have to declare it like...

public int mileage;

I hope this helps.

Trecius

Thank you, Trecius. That was just a mistake in posting. I do have them as public in my own code. I believe the problem is that I'm getting a Car returned to me from a List<Car> and trying to modify the returned Car's fields expecting the original Car to be modified. I'm just getting a copy of the Car that's in the List. So maybe the question is, how can I modify the members of a struct when the struct is within a collection? Can I modify the fields of elements in a collection only if the elements are reference types?


Friday, October 30, 2009 3:33 AM

Short answer: It has to do with value type and reference type.  Because a struct is ALWAYS a value type, you're returning a copy of the value stored in the collection.

Solution:  You should make your Car a REFERENCE type; to do this you just change...

public struct Car

to...

public class Car

 

Trecius

That's what I'll do. Thanks, Trecius!


Friday, October 30, 2009 3:38 AM

Once you come to think about it, you could also just replace the value in your collection.  For example, if you have a List<Car>, you could do the following...

Car car = lstCars[32];
car.mileage = 1024;
lstCars[32] = car;

Hope it helps.

Trecius

Wish I had thought of that. I may still just make the type a class. Thanks for all your help!


Friday, October 30, 2009 3:58 AM

Once you come to think about it, you could also just replace the value in your collection.  For example, if you have a List<Car>, you could do the following...

Car car = lstCars[32];
car.mileage = 1024;
lstCars[32] = car;

Hope it helps.

Trecius

Wish I had thought of that. I may still just make the type a class. Thanks for all your help!

I don't really understand why this works. Seems like you'd still be getting just a temporary copy on the left-hand side of the assignment operator when you do lstCars[32] = car. But I tested it, and it does work.


Friday, October 30, 2009 4:19 AM

You are correct; you are getting a copy.  Remember, when you have a struct, it's a value-type.  Also, when putting it back, the receiving end is also receiving a copy of it.

Trecius