2

I stored in an array the types of 2 objects that I'm passing as arguments. I´m trying to cast them in a loop but it doesn't seem to work.
I noticed when I debug that the value of the object's Type (returned by key.GetType()), it shows Name=RunTimeType FullName=System.RuntimeType instead of the expected Name=Label.
I'm not sure what am I doing wrong. Any suggestions?

public static void GetUserGUIDandSID(string username, Object b, Object c) { 
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
       "domainName.com");  
        UserPrincipal user = (UserPrincipal.FindByIdentity(ctx, username));
        var empIdNum = user.Guid.Value;
        var empSID = user.Sid.Value;

        List<object> types = new List<object>();
        types.Add(b.GetType());
        types.Add(c.GetType());

        foreach(var key in types) {
            if (key.GetType() == typeof(Label)) {
                ((Label)b).FontSize = 10;
                ((Label)b).Content = empIdNum;
            }
            if (key.GetType() == typeof(TextBox)) {
                ((TextBox)b).FontSize = 10;
                ((TextBox)b).Text = empIdNum.ToString();
            }
            if (key.GetType() == typeof(TextBlock)) {
                ((TextBlock)b).FontSize = 10;
                ((TextBlock)b).Text = empIdNum.ToString();
            }
        }
    }
help-info.de
  • 6,695
  • 16
  • 39
  • 41
wavestone
  • 79
  • 6
  • you can just do "b is Label", "c is Label" – Steve Nov 09 '16 at 17:17
  • Could you please show us how are you calling your own function? I suspect that you are passing the type of the objects as parameter instead of their instances, as [shown here](http://stackoverflow.com/a/5737947/4905310). Something like calling `GetUserGUIDandSID("user1", tb1.GetType(), lb1.GetType());` instead of `GetUserGUIDandSID("user1", tb1, lb1);` – Gabriel Rainha Nov 09 '16 at 17:50

2 Answers2

3

You could directly check the type of each object by using the is operator:

public static void GetUserGUIDandSID(string username, object b, object c)
{
    ...

    foreach (var o in new object[] { b, c })
    {
        if (o is Label)
        {
            ((Label)o).FontSize = 10;
            ((Label)o).Content = empIdNum;
        }
        else if (o is TextBox)
        {
            ((TextBox)o).FontSize = 10;
            ((TextBox)o).Text = empIdNum.ToString();
        }
        else if (o is TextBlock)
        {
            ((TextBlock)o).FontSize = 10;
            ((TextBlock)o).Text = empIdNum.ToString();
        }
    }
}

In addition, you could make the method accept an arbitrary number of objects by declaring a params object[] argument:

public static void GetUserGUIDandSID(string username, params object[] objects)
{
    ...

    foreach (var o in objects)
    {
        ...
    }
}

You may now call it with different naumbers of object parameters, like

GetUserGUIDandSID("user", a);
GetUserGUIDandSID("user", a, b);
GetUserGUIDandSID("user", a, b, c);
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I noticed same, but then I saw, that inside loop OP tried to cast ```b``` to every type :) – tym32167 Nov 09 '16 at 17:18
  • Hi Clamens thx for the insight foreach (var o in new object[] { b, c }), made my code shorter :) my problem with typeoff() was the addition GetType() in the if statement, the 'is' operator is doing the job nice, thx again – wavestone Nov 10 '16 at 17:48
  • Clamens, could you please elaborate about how to make a method to accept an arbitrary number of objects bu using 'params', this way i can generalize functions to be used in many events, lets say one time i want to pass 10 controls of different kind, other time only 3 etc ... – wavestone Nov 10 '16 at 17:59
1

Think that you wanted do this

List<object> types = new List<object>();
types.Add(b);
types.Add(c);

foreach (var key in types)
{
    if (key is Label)
    {
        ((Label)key).FontSize = 10;
        ((Label)key).Content = empIdNum;
    }
    if (key is TextBox)
    {
        ((TextBox)key).FontSize = 10;
        ((TextBox)key).Text = empIdNum.ToString();
    }
    if (key is TextBlock)
    {
        ((TextBlock)key).FontSize = 10;
        ((TextBlock)key).Text = empIdNum.ToString();
    }
}

For OP:

Why I cnahged this if (key.GetType() == typeof(Label)) to this if (key is Label)

In first case we are checking exactly Label type. I mean, if key will be instance of class, which derived from Label - first case will be false.

As example, we have custom class:

public class MyAwesomeLabel : Label
{
}

The following code

var myLabel = new MyAwesomeLabel();
Console.WriteLine(myLabel.GetType() == typeof(Label));
Console.WriteLine(myLabel is Label);

Will write False for first case and True for second. Operator is in this case does not check, whether types are equal. It checks possibility to cast given instance to target type. More info here https://msdn.microsoft.com/en-us/library/scekt9xw.aspx .

From my perspective, in certain your case is is better.

tym32167
  • 4,741
  • 2
  • 28
  • 32
  • Hi Tym, yes this is exactly what i wanted to do :) btw what is the difference between 'is' and 'typeoff' in this case, both of them achieve the same result ? i made my code working with each on of them so i wonder which one of them is better – wavestone Nov 10 '16 at 17:50