How to enable/disable properties in several instances of the same class ?

c#chris

New member
Joined
Dec 10, 2024
Messages
4
Programming Experience
1-3
Hello,

lets assume we have the following class:

C#:
public class Person

{

public string FirstName { get; set; } = string.Empty; //1

public string LastName { get; set; } = string.Empty; //2

public int Age { get; set; } = 11; //3

public int Heigth { get; set; } = 100; //4


}

lets assume we have the following instances of that class:

C#:
Person _persion1 = new() //all properties available


Person _persion2 = new() //Only properies 1-3 avaibalbe, property 4 not visible


How can I achieve, that different instances based on the same class, show different properties of that same class ?
 
Last edited by a moderator:
You can't. Every instance of the same type has the same members. All you could do is have an additional property that controlled how the other properties behaved, but they would still be accessible in code.

What you could do is make them different types, with one inheriting the other. You could define a base class with three properties and a derived class with one extra property. You could then create an instance of each type and one would have three properties and the other four. Because an instance of the derived type is also an instance of the base type, you could put an instance of the derived anywhere an instance of the base type is expected, e.g. an array of the base type. Not sure whether that would fulfil your actual requirement but then we don't really know what that is.
 
If you don't mind the field existing, but it not having a usable value, an alternative is to use nullable types.
C#:
public class Person
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public int? Age { get; set; }
    public int? Heigth { get; set; }
}
 
Thank you so for the hints so far
The following code works for the properties:

Base Class:
public class BasePerson
{
public string FirstName { get; set; } = string.Empty; //1
public string LastName { get; set; } = string.Empty; //2
public int Age { get; set; } = 11; //3

public void Verify()
{
     //verifies all 3 properties
}

}

C#:
public class Person : BasePerson
{
public int Heigth { get; set; } = 100; //4

public void Verify()
{
     //verifies all 4 properties
}

}

Instantiation:
BasePerson _person1 = new() //first 3 properties available
Person _person2 = new() // all properties availabe

Unfortunately another question popped up.

In the instance _person1 derived from BasePerson the function Verify is used to process calculations for which all 3 properties of that baseclass are necessary.
In the instance _person2 derived from Person a function Verify should process calculations for which all properties of the baseclass are necessary and the property "Height" from the derives class Person.

Any hints, what to do now ?
 
Declare the method virtual in the base class, then override it in the derived class. It will then be treated as the same method by the compiler but which implementation gets invoked at run time depends on the type of the object.
 
And in each of the sub classes you can call the base class implementation:
C#:
class Person : BasePerson
{
    :
    public void override Verify()
    {
        base.Verify();    // let BasePerson.Verify() do it's work
        // code to verify person here
    }
    :
}

Also see the example in the documentation:
 
Back
Top Bottom