Click here to Skip to main content
15,118,017 members
Home / Discussions / C#
   

C#

 
QuestionDisplay Live TV Pin
Jassim Rahma7-Sep-12 23:28
MemberJassim Rahma7-Sep-12 23:28 
AnswerRe: Display Live TV Pin
Eddy Vluggen8-Sep-12 5:41
professionalEddy Vluggen8-Sep-12 5:41 
QuestionHow to retrieve dell drac ip's via c sharp? Pin
turbosupramk37-Sep-12 11:28
Memberturbosupramk37-Sep-12 11:28 
QuestionGenerics Problems Pin
Kevin Marois7-Sep-12 8:11
professionalKevin Marois7-Sep-12 8:11 
AnswerRe: Generics Problems Pin
Clifford Nelson7-Sep-12 8:23
MemberClifford Nelson7-Sep-12 8:23 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 8:27
professionalKevin Marois7-Sep-12 8:27 
AnswerRe: Generics Problems Pin
Clifford Nelson7-Sep-12 8:42
MemberClifford Nelson7-Sep-12 8:42 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 9:02
professionalKevin Marois7-Sep-12 9: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 9:51
MemberEnnis Ray Lynch, Jr.7-Sep-12 9:51 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 9:55
professionalKevin Marois7-Sep-12 9:55 
GeneralRe: Generics Problems Pin
Ennis Ray Lynch, Jr.7-Sep-12 10:05
MemberEnnis Ray Lynch, Jr.7-Sep-12 10:05 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 10:09
professionalKevin Marois7-Sep-12 10:09 
GeneralRe: Generics Problems Pin
Ennis Ray Lynch, Jr.7-Sep-12 10:21
MemberEnnis Ray Lynch, Jr.7-Sep-12 10:21 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 11:43
professionalKevin Marois7-Sep-12 11:43 
AnswerRe: Generics Problems Pin
Clifford Nelson7-Sep-12 10:31
MemberClifford Nelson7-Sep-12 10:31 
AnswerRe: Generics Problems Pin
Clifford Nelson7-Sep-12 10:09
MemberClifford Nelson7-Sep-12 10:09 
AnswerRe: Generics Problems Pin
PIEBALDconsult7-Sep-12 10:12
professionalPIEBALDconsult7-Sep-12 10:12 
AnswerRe: Generics Problems Pin
Eddy Vluggen7-Sep-12 10:46
professionalEddy Vluggen7-Sep-12 10:46 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 11:44
professionalKevin Marois7-Sep-12 11:44 
GeneralRe: Generics Problems Pin
Eddy Vluggen7-Sep-12 11:54
professionalEddy Vluggen7-Sep-12 11:54 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 11:56
professionalKevin Marois7-Sep-12 11:56 
GeneralRe: Generics Problems Pin
Eddy Vluggen7-Sep-12 12:09
professionalEddy Vluggen7-Sep-12 12:09 
AnswerRe: Generics Problems Pin
jschell7-Sep-12 11:39
Memberjschell7-Sep-12 11:39 
GeneralRe: Generics Problems Pin
Kevin Marois7-Sep-12 11:41
professionalKevin Marois7-Sep-12 11:41 
GeneralRe: Generics Problems Pin
jschell8-Sep-12 8:23
Memberjschell8-Sep-12 8: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.