Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

how to override a get in c#

I have situation in my application where i need to ensure

if (OrderItemStatusId == 0)
{
   OrderItemStatusId =1;
}

so i'm wondering how would i add this override to

 public virtual int OrderItemStatusId { get; set; }
0
frosty1
Asked:
frosty1
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
If that virtual version is coming from a base class you can do something like:
    public class DerivedClass : BaseClass
    {
        public override int OrderItemStatusId
        {
            get
            {
                return base.OrderItemStatusId;
            }
            set
            {
                if (value == 0)
                    base.OrderItemStatusId = 1;
                else
                    base.OrderItemStatusId = value;
            }
        }
    }

Open in new window

0
 
frosty1Author Commented:
hi carl, well it's just virtual because i'm using nhibernate. it needs that for the lasy loading.
0
 
Todd GerbertIT ConsultantCommented:
The "virtual" keyword is applied to indicate that derived types (that is classes that inherit from the class where the virtual keyword is applied) are able to override the behavior for the given method/property.  Note that in carl_tawn's example he changes the logic of OrderItemStatusId's "set" slightly, but by calling "base.OrderItemStatusId" he is allowing the behavior defined in BaseClass's "set" for this property to do the actual work; he does this because he may not know what should happen when this property is set, i.e. for all he knows when the OrderItemStatusId is set to a particular number certain other properties need to be set to specific values.  He is able to do that because in BaseClass the property OrderItemStatusId was declared with the "virtual" keyword.

class Program
{
	static void Main(string[] args)
	{
		Car c = new Car();
		NormalCar nc = new NormalCar();
		OtherNormalCar onc = new OtherNormalCar();
		FunnyCar fc = new FunnyCar();

		Console.WriteLine("Car wheel count: " + c.Wheels.ToString());
		Console.WriteLine("NormalCar wheel count: " + nc.Wheels.ToString());
		Console.WriteLine("OtherNormalCar wheel count: " + onc.Wheels.ToString());
		Console.WriteLine("FunnyCar wheel count: " + fc.Wheels.ToString());

		Console.ReadKey();
	}
}

class Car
{
	public virtual int Wheels
	{
		get
		{
			return 4;
		}
	}
}

class NormalCar : Car
{
	// Does not alter Wheels property at all
}

class OtherNormalCar : Car
{
	public override int Wheels
	{
		get
		{
			return base.Wheels; // Just does whatever the "get" for the base class, which is Car, does
		}
	}
}

class FunnyCar : Car
{
	public override int Wheels
	{
		get
		{
			return 3; // Overrides the behavior of Car's Wheels "get" for this property
		}
	}
}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now