help condensing code

sous2817

Member
Joined
Apr 5, 2011
Messages
17
Programming Experience
Beginner
Hello everyone,

Fairly new to vb.net, and even newer to animating an object. What I'm trying to do is adjust the height, width, and opacity all in one go. I've done it like this:

VB.NET:
    Sub AnimationTest()
        Dim myDoubleAnimation As DoubleAnimation = New DoubleAnimation()

        ' Height 
        myDoubleAnimation.From = 2
        myDoubleAnimation.To = StackPanel1.Height
        myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(1))
        myStoryboard = New Storyboard()
        myStoryboard.Children.Add(myDoubleAnimation)
        Storyboard.SetTargetName(myDoubleAnimation, Viewbox1.Name)
        Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Viewbox.HeightProperty))
        myStoryboard.Begin(Me)

        ' Width
        myDoubleAnimation.From = 2
        myDoubleAnimation.To = StackPanel1.Width
        myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(1))
        myStoryboard = New Storyboard()
        myStoryboard.Children.Add(myDoubleAnimation)
        Storyboard.SetTargetName(myDoubleAnimation, Viewbox1.Name)
        Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Viewbox.WidthProperty))
        myStoryboard.Begin(Me)

        'Opacity
        myDoubleAnimation.From = 0
        myDoubleAnimation.To = 1
        myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(1))
        myStoryboard = New Storyboard()
        myStoryboard.Children.Add(myDoubleAnimation)
        Storyboard.SetTargetName(myDoubleAnimation, Viewbox1.Name)
        Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Viewbox.OpacityProperty))
        myStoryboard.Begin(Me)
    End Sub

Surely there's a better way to do it than repeating the same steps over and over. If anyone has the time, I'd greatly appreciate any help you can provide.

Thanks in advance!
 
Yes there is. Write a method that contains all the common code and use parameters for the values that vary. You can then call that method three times and just pass in different arguments each time.
 
Thank you for the advice jmcilhinney! This is what I've come up with:

VB.NET:
Sub Animate_Object(ByVal StartVal As Double, ByVal EndVal As Double, ByVal Span As Double, ByVal cControl As Control, ByVal dProp As DependencyProperty)
        Dim myDoubleAnimation As DoubleAnimation = New DoubleAnimation()
        myDoubleAnimation.From = StartVal
        myDoubleAnimation.To = EndVal
        myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(Span))
        myStoryboard = New Storyboard()
        myStoryboard.Children.Add(myDoubleAnimation)
        Storyboard.SetTargetName(myDoubleAnimation, cControl.Name)
        Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(dProp))
        myStoryboard.Begin(Me)
    End Sub

I'm not sure how to set the 4th parameter properly. I'd like to pass it the specific control that I'd like to manipulate, but when I try to set the arguements:

VB.NET:
Animate_Object(0, StackPanel1.Width, 1, Viewbox1, Viewbox.WidthProperty)

I get an error with Viewbox1 (Value of type 'System.Windows.Controls.Viewbox' cannot be converted to 'System.Windows.Controls.Control'.). Changing the 4th parameter to be ByVal cControl As Viewbox works as expected, but it'd be nice to not have to explicitly state the control type. Is there a way to set the control as a parameter and not have to explicitly declare it in the sub?

Thanks again for any help you can give!
 
ah, think I've got it figured out. I used "as object" instead of "as control" and seems to work as expected:

VB.NET:
Sub Animate_Object(ByVal StartVal As Double, ByVal EndVal As Double, ByVal Span As Double, ByVal cControl As Object, ByVal dProp As DependencyProperty)
        Dim myDoubleAnimation As DoubleAnimation = New DoubleAnimation()
        myDoubleAnimation.From = StartVal
        myDoubleAnimation.To = EndVal
        myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(Span))
        myStoryboard = New Storyboard()
        myStoryboard.Children.Add(myDoubleAnimation)
        Storyboard.SetTargetName(myDoubleAnimation, cControl.Name)
        Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(dProp))
        myStoryboard.Begin(Me)
    End Sub
 
That's really not a great solution. That means that you can now pass anything into the method, even objects that don't have the members you're using.

The first thing to fix is the fact that you are using late-binding. You obviously have Option Strict Off, otherwise that code wouldn't compile. You should turn Option Strict On for this and every other project. If you turn it On in the IDE options then that will be the default for futuren projects. You will then not be allowed to rely on late-binding and implicit conversions.

The next thing to address is the type you're using. A Viewbox is obviously not a control or it could be cast as type Control. I'm on my phone right now so I can't check the doco but you need to determine what is the most common type you want to use and specify your parameter as that type. If there isn't one common type then you'll need to overload the method.
 
Since the method is not using the object, but rather its string Name, the parameter would more naturally be a 'targetName As String' one.
 
Since the method is not using the object, but rather its string Name, the parameter would more naturally be a 'targetName As String' one.
Hmmm... I should have looked at the code more closely. In my defence, I was on a phone at the time.
 
Like this?

VB.NET:
        Sub Animate_Object(ByVal StartVal As Double, ByVal EndVal As Double, ByVal Span As Double, ByVal targetName As String, ByVal dProp As DependencyProperty)
        Dim myDoubleAnimation As DoubleAnimation = New DoubleAnimation()
        Dim mystoryboard As Storyboard
        myDoubleAnimation.From = StartVal
        myDoubleAnimation.To = EndVal
        myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(Span))
        myStoryboard = New Storyboard()
        myStoryboard.Children.Add(myDoubleAnimation)
        Storyboard.SetTargetName(myDoubleAnimation, targetName)
        Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(dProp))
        myStoryboard.Begin(Me)
    End Sub

And I call it like this:

VB.NET:
Animate_Object(2, StackPanel1.Width, 1, Viewbox1.Name, Viewbox.WidthProperty)

Not sure what you mean re: the LateBinding bit. I did move "Private myStoryboard As Storyboard" to be included in the sub as it seemed more appropriate. As recommended, I turned Option Strict On in the IDE and the code compiled and runs without issue.

Am I getting any closer?
 
VB.NET:
        Dim mystoryboard As Storyboard
        '..
        '..
        '..
        myStoryboard = New Storyboard()
You can condense that to
Dim mystoryboard As New Storyboard

VB.NET:
        Dim myDoubleAnimation As DoubleAnimation = New DoubleAnimation()
        myDoubleAnimation.From = StartVal
        myDoubleAnimation.To = EndVal
        myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(Span))
This can be condensed to:
Dim myDoubleAnimation As New DoubleAnimation(StartVal, EndVal, New Duration(TimeSpan.FromSeconds(Span)))
 
Back
Top