Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# optimization
Hi, I am sure following code doesn't look professional so please give some suggestions to make it short.
 
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 14-Jan-13 6:23am
Edited 14-Jan-13 7:36am
v2
Comments
jibesh at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
Adam R Harris at 14-Jan-13 12:34pm
   
Would make it much cleaner for sure.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 469
1 CHill60 465
2 OriginalGriff 325
3 PIEBALDconsult 285
4 BillWoodruff 193


Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100