Dynamic casting of objects in C# [Resolved]

Posted by Bhupentiwari under C# on 10/19/2016 | Points: 10 | Views : 350 | Status : [Member] | Replies : 2
Guys pls suggest better way to remove three if statement from below code


 if (schemes != null && schemes.FormFieldslist.Count > 0)
{
GetAllFormControl(vForm, Controllist);
schemes.FormFieldslist.RemoveAll(x => !x.PresentInReport.Contains(vFormReport.ToString()));

foreach (Clipper.CFPS.BusinessEntities.CustomField cf in schemes.FormFieldslist)
{

if (cf.ControlType == "TextBox" || cf.ControlType == "DateTimePicker" || cf.ControlType == "RadioButton")
{
List<Panel> targetControl = CommonFunctions.GetControlByNameFromList<Panel>(Controllist, cf.PanelName);
if (targetControl.Count > 0) targetControl[0].Hide();
continue;
}
if (cf.ControlType == "GroupBox")
{
List<GroupBox> targetControl = CommonFunctions.GetControlByNameFromList<GroupBox>(Controllist, cf.GroupBoxName);
if (targetControl.Count > 0) targetControl[0].Hide();
continue;
}
if (cf.ControlType == "Panel")
{
List<Panel> targetControl = CommonFunctions.GetControlByNameFromList<Panel>(Controllist, cf.GroupBoxName);
if (targetControl.Count > 0) targetControl[0].Hide();
continue;
}
}
}


Thanks n Regards
Bhupendra Tiwari



Responses

Posted by: Rajnilari2015 on: 10/19/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Bhupentiwari Sir,

You can use a Dictionary of type string and Type like

private static Dictionary<string, Type> SetControlTypes()
{
var dicControlTypes = new Dictionary<string, Type>();
dicControlTypes.Add("TextBox", typeof(Panel));
dicControlTypes.Add("DateTimePicker", typeof(Panel));
dicControlTypes.Add("RadioButton", typeof(Panel));
dicControlTypes.Add("Panel", typeof(Panel));
dicControlTypes.Add("GroupBox", typeof(GroupBox));

return dicControlTypes;
}


and search by the key(which is string) and return the Type . An example follows

using System;
using System.Collections.Generic;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(SearchControlTypes("GroupBox").SomeMethod());
Console.ReadKey();

}

private static dynamic SearchControlTypes(string key)
{
return Activator.CreateInstance(SetControlTypes()[key]) as dynamic;
}

private static Dictionary<String, Type> SetControlTypes()
{

var dicControlTypes = new Dictionary<string, Type>();

dicControlTypes.Add("TextBox", typeof(Panel));
dicControlTypes.Add("DateTimePicker", typeof(Panel));
dicControlTypes.Add("RadioButton", typeof(Panel));
dicControlTypes.Add("Panel", typeof(Panel));
dicControlTypes.Add("GroupBox", typeof(GroupBox));

return dicControlTypes;
}
}

internal class Panel
{
public int SomeMethod()
{
return 10;
}
}

internal class GroupBox
{
public int SomeMethod()
{
return 20;
}
}
}


So your implementation can be changed to

if (schemes != null && schemes.FormFieldslist.Count > 0)
{
GetAllFormControl(vForm, Controllist);
schemes.FormFieldslist.RemoveAll(x => !x.PresentInReport.Contains(vFormReport.ToString()));

foreach (Clipper.CFPS.BusinessEntities.CustomField cf in schemes.FormFieldslist)
{
dynamic T = SearchControlTypes(cf.ControlType);

..............
................
}

}


Hope that will help.



--
Thanks & Regards,
RNA Team

Bhupentiwari, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupentiwari on: 10/20/2016 [Member] Starter | Points: 25

Up
0
Down
@Rajnilari2015 Sir

Thanks for quick response. But i forgot to mention i am having limitation of dot net framwork version. i can use only framwork 2.0.

I think dynamic keyword is not available in that.

Thanks




Thanks n Regards
Bhupendra Tiwari

Bhupentiwari, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response