Solved

How do I create properties in a model class that are derived from other values in that class?

Posted on 2015-02-20
2
84 Views
Last Modified: 2015-02-20
I have a ViewModel that contains two entities as shown below:

public class ClientMaxActivityViewModel
    {
        public Client clientData { get; set; }
        public Activity activityData { get; set; }

    }

I want to create a property in this model that is dependent on the values of properties in both the Client and Activity entities.   It would be a string with a value of either 'Compliant' or 'Non-compliant'. Here is some pseudo-code for the logic (I  know the syntax is all wrong, but hopefully the principal is evident):

public string Compliant
{
      if(Client.VisitFrequency == "Monthly" && DateDiff(Activity.ActivityDate - Date(Now)) > 31)
           { return "Non-compliant"
             else
          return "Compliant"}
}

Thanks.
0
Comment
Question by:MinirvaTwo
[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
2 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40621791
If you will be using this property in any decision logic, may I recommend using a boolean property instead:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EE_Q28621250
{
	class Program
	{
		static void Main(string[] args)
		{
			ClientMaxActivityViewModel model = new ClientMaxActivityViewModel() { clientData = new Client() { VisitFrequency = VisitFrequency.Monthly }, activityData = new Activity() { ActivityDate = DateTime.Now.AddMonths(1) } };
			Console.WriteLine("Is model compliant for Activity Date of {0} - {1}", model.activityData.ActivityDate.ToString("MM/dd/yyyy"), model.IsCompliant);
			model.activityData.ActivityDate = DateTime.Now.AddMonths(-1);
			Console.WriteLine("Is model compliant for Activity Date of {0} - {1}", model.activityData.ActivityDate.ToString("MM/dd/yyyy"), model.IsCompliant);
			Console.ReadLine();
		}
	}

	class ClientMaxActivityViewModel
	{
		public Client clientData { get; set; }
		public Activity activityData { get; set; }
		public bool IsCompliant
		{
			get 
			{
				if (clientData != null && activityData != null)
				{
					switch (clientData.VisitFrequency)
					{
						case VisitFrequency.Daily:
							return activityData.ActivityDate.AddDays(1) > DateTime.Now;
						case VisitFrequency.Weekly:
							return activityData.ActivityDate.AddDays(7) > DateTime.Now;
						case VisitFrequency.Monthly:
							return activityData.ActivityDate.AddMonths(1) > DateTime.Now;
						case VisitFrequency.Annually:
							return activityData.ActivityDate.AddYears(1) > DateTime.Now;
						case VisitFrequency.None:
						default:
							return false;
					}
				}
				return false;
			}
		}
	}

	class Client
	{
		public VisitFrequency VisitFrequency { get; set; }
	}

	class Activity
	{
		public DateTime ActivityDate { get; set; }
	}

	enum VisitFrequency
	{
		None = 0,
		Daily = 1,
		Weekly = 2,
		Monthly = 3,
		Annually = 4
	}
}

Open in new window

-saige-
0
 

Author Closing Comment

by:MinirvaTwo
ID: 40622001
Thanks it_saige!  You rock!  Worked perfectly.  I used text values in my switch/case statements instead of the enums, but it all worked great.  I could reference all of the properties of the ViewModel in the View, as shown below:

<td>
            @Html.DisplayFor(modelItem => item.clientData.Status)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.activityData.ActivityDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsCompliant)
        </td>

Thanks!
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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