Solved

TypeName as parameter

Posted on 2011-03-13
4
360 Views
Last Modified: 2012-05-11
hey guys,

im having some troubles with trying to pass in a class type name into a method as a parameter. so far this is the solution im able to get working:

Private Function GetContentElementFromRTB(ByVal targetElement As System.Type) As Object

        ' Create FCE variable
        Dim fce As FrameworkContentElement = TryCast(docRTB.CaretPosition.Parent, FrameworkContentElement)

        ' Loop while var isnt nothing
        While fce IsNot Nothing

            ' If the type of the var is what we're looking for, return it
            If fce.GetType() = targetElement Then Return fce

            ' Climb the list of parent objects
            fce = TryCast(fce.Parent, FrameworkContentElement)
        End While

        ' If object type wasnt found, return nothing
        Return Nothing

    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
        Dim tc As TableCell = CType(GetContentElementFromRTB(GetType(TableCell)), TableCell)
        Dim tr As TextRange = New TextRange(tc.ContentStart, tc.ContentEnd)
        Button2.Content = tr.Text
        'tr.Text = CStr(Button2.Content)
    End Sub

Open in new window


basically i want to be able to return whatever object im looking for (tablecell, tablerow, table), however at the moment as you can see i have to use a bunch of GetType() and CType() methods within my calling function (Button2_Click). is there a more elegant way of doing this whereby i dont have to use as many GetType or CType calls?

cheers
0
Comment
Question by:gem56
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
You should use Generics.  It should be something like this:

    Private Function GetContentElementFromRTB(Of T)() As T

		' Create FCE variable
		Dim fce As FrameworkContentElement = TryCast(docRTB.CaretPosition.Parent, FrameworkContentElement)

		' Loop while var isnt nothing
		While fce IsNot Nothing

			' If the type of the var is what we're looking for, return it
			If fce.GetType() = GetType(T) Then Return CType(fce, T)

			' Climb the list of parent objects
			fce = TryCast(fce.Parent, FrameworkContentElement)
		End While

		' If object type wasnt found, return nothing
		Return Nothing

	End Function

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
		Dim tc As TableCell = GetContentElementFromRTB(Of TableCell)()
		Dim tr As TextRange = New TextRange(tc.ContentStart, tc.ContentEnd)
		Button2.Content = tr.Text
		'tr.Text = CStr(Button2.Content)
	End Sub

Open in new window

0
 

Author Comment

by:gem56
Comment Utility
hi wdosanjos, thanks for your reply.

ok that looks much better, however im getting an error on line #13 in the return statement CType(fce, T) => "Value of type 'System.Windows.FrameworkContentElement' cannot be converted to 'T'."
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
Comment Utility
Please try the following:

    Private Function GetContentElementFromRTB(Of T As FrameworkContentElement)() As T

		' Create FCE variable
		Dim fce As FrameworkContentElement = TryCast(docRTB.CaretPosition.Parent, FrameworkContentElement)

		' Loop while var isnt nothing
		While fce IsNot Nothing

			' If the type of the var is what we're looking for, return it
			If fce.GetType() = GetType(T) Then Return CType(fce, T)

			' Climb the list of parent objects
			fce = TryCast(fce.Parent, FrameworkContentElement)
		End While

		' If object type wasnt found, return nothing
		Return Nothing

	End Function

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
		Dim tc As TableCell = GetContentElementFromRTB(Of TableCell)()
		Dim tr As TextRange = New TextRange(tc.ContentStart, tc.ContentEnd)
		Button2.Content = tr.Text
		'tr.Text = CStr(Button2.Content)
	End Sub

Open in new window

0
 

Author Comment

by:gem56
Comment Utility
beautiful, thanks mate :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now