Solved

how to override a get in c#

Posted on 2011-02-24
3
314 Views
Last Modified: 2012-05-11
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
Comment
Question by:frosty1
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34968670
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
 

Author Comment

by:frosty1
ID: 34968697
hi carl, well it's just virtual because i'm using nhibernate. it needs that for the lasy loading.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34970575
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question