Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am sure following code doesn't look professional so please give some suggestions to make it short.

C#
if (((DevExpress.Xpf.LayoutControl.GroupBox)sender).State == DevExpress.Xpf.LayoutControl.GroupBoxState.Maximized)
{
   Employee emp = (((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content) as Employee;
   if (emp != null)
   {
      User user = emp.user;
      ((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content = new Employee(user);
   }
   else
   {
      EmployeeMin empMin = (((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content) as EmployeeMin;
      User user = empMin.user;
      ((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content = new Employee(user);
   }
}
else
{
   Employee emp = (((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content) as Employee;
   if (emp != null)
   {
      User user = emp.user;
      ((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content = new EmployeeMin(user);
   }
   else
   {
      EmployeeMin empMin = (((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content) as EmployeeMin;
      User user = empMin.user;
      ((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content = new EmployeeMin(user);
   }
}
Posted
Updated 14-Jan-13 7:36am
v2
Comments
Jibesh 14-Jan-13 12:29pm    
You know its doesnt look good they why cant you re-arrange that by closely looking at the variables and its usages

my suggestions are

Keep this '((DevExpress.Xpf.LayoutControl.GroupBox)sender) in a location variable say mySender

replace all the occurrence of '((DevExpress.Xpf.LayoutControl.GroupBox)sender)' with mySender

if you want go further define another variable for mySender.Content as myContent and replace all the occurrence of '((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content' with myContent

arh.. I just noticed the tag 'Optimization' if you are re-arranging this code block to improve the program optimization then you are wrong, your compiler already doing this for you.
 
Share this answer
 
v2
Comments
Adam R Harris 14-Jan-13 12:34pm    
Would make it much cleaner for sure.
As Adam R Harris you can use local variables to store data the code access more than once; in your code the variable most used is sender cast to DevExpress.Xpf.LayoutControl.GroupBox; the second one is its Content property.

The followin is a possible arrangement:

C#
var box = sender as DevExpress.Xpf.LayoutControl.GroupBox;
if (box != null)
{
    var oldContent = box.Content;
    /* the following would be better as ...
    ContentType newContent = null;
    */
    var newContent = oldContent;

    var emp = oldContent as Employee;
    if (emp == null)
        emp = oldContent as EmployeeMin;

    if (box.State == DevExpress.Xpf.LayoutControl.GroupBoxState.Maximized)
    {
        newContent = new Employee(emp.user);
    }
    else
    {
        newContent = new EmployeeMin(emp.user);
    }

    if (newContent != oldContent)
        box.Content = newContent;
}


I used var to reduce type name appearance. There is only one instruction where you should explicitly put the type name, but I don't know it so I can't do it for you; I had sorrounded it with a block comment with ContentType as the name of the type I don't know (maybe object, or Employee or a base type).

Regards,
Daniele.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900