RotatorScrollDirection - convert the string to object

2 posts, 0 answers
  1. Bader
    Bader avatar
    215 posts
    Member since:
    Aug 2008

    Posted 05 Aug 2011 Link to this post

    Hello,

    I have a problem in changing the RotatorScrollDirection using c# code

    Here is the code, I'm using:

    switch (scrollDirection)
                    {
                        case "Down": rotator1.ScrollDirection = RotatorScrollDirection.Down; break;
                        case "Left": rotator1.ScrollDirection = RotatorScrollDirection.Left; break;
                        case "Right": rotator1.ScrollDirection = RotatorScrollDirection.Right; break;
                        case "Up": rotator1.ScrollDirection = RotatorScrollDirection.Up; break;
                    }

    Is there any way to convert the string to object in order to prevent using the "switch" or "if" statements? for example, convert the string "Right" to the object "RotatorScrollDirection.Right"

    Please, I need your help,
    It is appreciated to send me the modified code.

    Regards,
    Bader

  2. Niko
    Admin
    Niko avatar
    387 posts

    Posted 08 Aug 2011 Link to this post

    Hello Bader,

    Please, refer to the MSDN site for more information on the Enum class and its parsing methods - Parse and TryParse. Here is a simple implementation for the RotatorScrollDirection:
    private RotatorScrollDirection ParseDirection(string directionName)
    {
        RotatorScrollDirection direction = (RotatorScrollDirection)Enum.Parse(typeof(RotatorScrollDirection), directionName, true);
      
        return direction;
    }
    private RotatorScrollDirection ParseDirection_DotNET4Only(string directionName)
    {
        RotatorScrollDirection direction;
        //this method works in .NET 4 only
        Enum.TryParse<RotatorScrollDirection>(directionName, true, out direction);
      
        return direction;
    }

    Hope you will find this helpful.

    Regards,
    Niko
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top