Click here to Skip to main content
15,305,480 members
Home / Discussions / C#
   

C#

 
QuestionDisplay Live TV Pin
Jassim Rahma7-Sep-12 22:28
MemberJassim Rahma7-Sep-12 22:28 
AnswerRe: Display Live TV Pin
Eddy Vluggen8-Sep-12 4:41
professionalEddy Vluggen8-Sep-12 4:41 
QuestionHow to retrieve dell drac ip's via c sharp? Pin
turbosupramk37-Sep-12 10:28
Memberturbosupramk37-Sep-12 10:28 
QuestionGenerics Problems Pin
Kevin Marois7-Sep-12 7:11
professionalKevin Marois7-Sep-12 7:11 
AnswerRe: Generics Problems Pin
Clifford Nelson7-Sep-12 7:23
MemberClifford Nelson7-Sep-12 7:23 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 7:27
professionalKevin Marois7-Sep-12 7:27 
AnswerRe: Generics Problems Pin
Clifford Nelson7-Sep-12 7:42
MemberClifford Nelson7-Sep-12 7:42 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 8:02
professionalKevin Marois7-Sep-12 8:02 
Can't use an interface because most of this code is already in place. Have to work with what I have.

However, I think this will do it:

public partial class Form1<T> : Form 
{
    T DataItem;
    Type type = typeof(T);

    public Form1()
    {
        InitializeComponent();

        var DistrictId = 10;
        DataItem = (T)Activator.CreateInstance(typeof(T), DistrictId);

        setProperty<string>("Name", "The Name");
        setProperty<bool>("LinkedToBilling", true);
        setProperty<DateTime>("BillingStartDate", new DateTime(2012, 01, 15));
    }

    private void setProperty<T>(string PropertyName, T Value)
    {
        var prop = DataItem.GetType().GetProperty(PropertyName);

        if (prop != null)
        {
            type.GetProperty(PropertyName).SetValue(DataItem, Value, null);
        }
    }
}


This will allow me to attempt to set a property. Remember that in this case most of the properties exist on both objects. But this code allows me to not have multiple IF blocks. I think it's clean, and it solved the issue.
If it's not broken, fix it until it is

GeneralRe: Generics Problems Pin
Ennis Ray Lynch, Jr.7-Sep-12 8:51
MemberEnnis Ray Lynch, Jr.7-Sep-12 8:51 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 8:55
professionalKevin Marois7-Sep-12 8:55 
GeneralRe: Generics Problems Pin
Ennis Ray Lynch, Jr.7-Sep-12 9:05
MemberEnnis Ray Lynch, Jr.7-Sep-12 9:05 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 9:09
professionalKevin Marois7-Sep-12 9:09 
GeneralRe: Generics Problems Pin
Ennis Ray Lynch, Jr.7-Sep-12 9:21
MemberEnnis Ray Lynch, Jr.7-Sep-12 9:21 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 10:43
professionalKevin Marois7-Sep-12 10:43 
AnswerRe: Generics Problems Pin
Clifford Nelson7-Sep-12 9:31
MemberClifford Nelson7-Sep-12 9:31 
AnswerRe: Generics Problems Pin
Clifford Nelson7-Sep-12 9:09
MemberClifford Nelson7-Sep-12 9:09 
AnswerRe: Generics Problems Pin
PIEBALDconsult7-Sep-12 9:12
professionalPIEBALDconsult7-Sep-12 9:12 
AnswerRe: Generics Problems Pin
Eddy Vluggen7-Sep-12 9:46
professionalEddy Vluggen7-Sep-12 9:46 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 10:44
professionalKevin Marois7-Sep-12 10:44 
GeneralRe: Generics Problems Pin
Eddy Vluggen7-Sep-12 10:54
professionalEddy Vluggen7-Sep-12 10:54 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 10:56
professionalKevin Marois7-Sep-12 10:56 
GeneralRe: Generics Problems Pin
Eddy Vluggen7-Sep-12 11:09
professionalEddy Vluggen7-Sep-12 11:09 
AnswerRe: Generics Problems Pin
jschell7-Sep-12 10:39
Memberjschell7-Sep-12 10:39 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 10:41
professionalKevin Marois7-Sep-12 10:41 
GeneralRe: Generics Problems Pin
jschell8-Sep-12 7:23
Memberjschell8-Sep-12 7:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.