Solved

how to override a get in c#

Posted on 2011-02-24
3
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

635 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