Solved

TypeName as parameter

Posted on 2011-03-13
4
363 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
ID: 35125047
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
ID: 35161520
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
ID: 35162126
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
ID: 35162757
beautiful, thanks mate :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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