locked
Is it possibe to change/call the object propery but the object is on string? RRS feed

  • Question

  • For my app optimization, I want to optimize my codes by using switch statements to return a string, and that string is an object.

    Is it possible to do so, technically the program will say there is an error because the IDE is only detecting a string and he does not know it is a object.

    For Example: (I want to change my opacity of rectangle, instead of making 2 different function, I just made 1)

    /* The code may have slight errors for it is made on the spot and Let us say the name of your Rectangle are "Rectangle1" and "Rectangle2" */ private void RectangeOpacity (string ThisRectangle) { ThisRectangle.Opacity = 30; }

    main()
    {
    RectangleOpacity(Rectangle1);
    }

     
    Saturday, March 9, 2013 12:23 PM

Answers

  • Why not make your method accept a Rectangle object?

    private void RectangleOpacity(Rectangle ThisRectangle) { ThisRectangle.Opacity = 30; }

    //then call it like this....

    RectangleOpacity(Rectangle1);

    //or

    RectangleOpacity(Rectangle2);


    Or if you want to have a method that will change the opacity of any UIElement:

    private void ObjectOpacity(UIElement element) { element.Opacity = 30; }

    //will accept calls like this...

    ObjectOpacity(Rectangle1);

    //or even

    ObjectOpacity(Ellipse3);




    • Edited by jrboddie Saturday, March 9, 2013 1:46 PM
    • Marked as answer by Franz_Justin Saturday, March 9, 2013 1:49 PM
    Saturday, March 9, 2013 1:44 PM

All replies

  • Hmmm....Convert.ChangeType() isn't available in Win8, so maybe you can get at this way....

    If you start with an instance of the type:

        Type t = typeof(Rect);

    You can get an instance of TypeInfo using the GetTypeInfo() extension method:

        TypeInfo ti = t.GetTypeInfo()

    You can get an instance of PropertyInfo using GetDeclaredProperty():

        PropertyInfo pi = ti.GetDeclaredProperty("Opacity");

    Now, PropertyInfo has a SetValue() method that I think you can call. Something like:

        pi.SetValue(ThisRectangle, 30);

    or maybe

       pi.SetValue((Rect)ThisRectangle, 30);

    But I am soooo not an expert on Reflection, and I only have experience up through the "get the PropertyInfo" part of that agorith. I've never tried calling SetValue() like that, and it may not work. The method might spit about the casting.


    Rebecca M. Riordan

    Saturday, March 9, 2013 1:31 PM
  • Why not make your method accept a Rectangle object?

    private void RectangleOpacity(Rectangle ThisRectangle) { ThisRectangle.Opacity = 30; }

    //then call it like this....

    RectangleOpacity(Rectangle1);

    //or

    RectangleOpacity(Rectangle2);


    Or if you want to have a method that will change the opacity of any UIElement:

    private void ObjectOpacity(UIElement element) { element.Opacity = 30; }

    //will accept calls like this...

    ObjectOpacity(Rectangle1);

    //or even

    ObjectOpacity(Ellipse3);




    • Edited by jrboddie Saturday, March 9, 2013 1:46 PM
    • Marked as answer by Franz_Justin Saturday, March 9, 2013 1:49 PM
    Saturday, March 9, 2013 1:44 PM