Solved

TypeName as parameter

Posted on 2011-03-13
4
364 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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