Is it possible to update any property of a class in one function

kiwis

Member
Joined
Jan 26, 2025
Messages
14
Programming Experience
Beginner
I'm thinking about something similar to this, but string is not the right data type....

Might not even be possible, I've googled this but might be googling the wrong terms.

C#:
        public void updateProperty(string propertyName, string value)
        {
            propertyName = value;
        }
 
Solution
If you have a POCO (Plain Old C# Object) like your Player class that you show above, and you don't run into any serialization/deserialization throwing any exceptions, then the deserialization step:
C#:
var playerList = JsonSerializer.Deserialize<List<Classes.Player>>(jsonResponse, serializerOptions);
will construct a list, and call the constructor for each of the objects. If your class doesn't explicitly declare a constructor, the C# compiler provides a default constructor that sets all fields to their default values. In the case of deserialization, after the default values are set, it will take the values from the input stream and assign them to the fields in the instantiated object (via reflection).

The line
C#:
Player...
The word you need to be Googling is "reflection".

Or alternatively, you could just have a giant switch statement if you will only be updating the properties of your current class.
 
Out of curiosity why would you want to use a method in your class to update a property of the same class instance? From my point of view, there are a lot of cons with that approarch:
- You lose the support of the compiler telling you if you try to update a property that doesn't exist anymore because you refactored your code, or you have a typo.
- You pay the performance cost of using reflection to update the property.
- You pay the performance cost of converting the string to the appropriate type for the property.
- Your code now becames "string/y-typed", instead of being "strongly-typed". Being "stringly-typed" is a negative in the object oriented programming world.
- You can't take advantage of the IDE's support for refactoring code in case you want to rename a property, change the type, or move the property to another class.
 
Well I'm very new to C# and I'm playing with a hobby project.

Here's what I've done

Player Class
C#:
    public class Player
    {

        public int PlayerId { get; set; }
        public required string DisplayName { get; set; }
        public required string PlayerSurname { get; set; }
    }

Downloading Players from the web from my winForm. From here I'm loading them into a database and displaying them when required in a listbox.
C#:
using HttpResponseMessage response = await httpClient.GetAsync("");
var jsonResponse = await response.Content.ReadAsStringAsync();

var serializerOptions = new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true
};

var playerList = JsonSerializer.Deserialize<List<Classes.Player>>(jsonResponse, serializerOptions);

I don't have a constructor so I don't think I can call
C#:
Player player = listBox1.SelectedItem as Player;
player.UpdateSurname = "Johnson";

Ready to be told I've done it all wrong. This is my first project so go easy.
 
If you have a POCO (Plain Old C# Object) like your Player class that you show above, and you don't run into any serialization/deserialization throwing any exceptions, then the deserialization step:
C#:
var playerList = JsonSerializer.Deserialize<List<Classes.Player>>(jsonResponse, serializerOptions);
will construct a list, and call the constructor for each of the objects. If your class doesn't explicitly declare a constructor, the C# compiler provides a default constructor that sets all fields to their default values. In the case of deserialization, after the default values are set, it will take the values from the input stream and assign them to the fields in the instantiated object (via reflection).

The line
C#:
Player player = listBox1.SelectedItem as Player;
does not create a new Player object and put it into player. It just gets a reference to the object that is in the playerList.

Therefore, you can simply do this as the next line:
C#:
player.PlayerSurname = "Johnson".
 
Solution
I agree with Skydiver (y)

Important considerations:
Encapsulation:
While you can directly update properties, good practice often involves using setter methods to control how properties are modified and potentially add validation checks.

Private properties:
Some languages allow you to define private properties which can only be accessed within the class itself, limiting direct updates from outside.
 
Last edited:
Back
Top Bottom