Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TypeName as parameter

Posted on 2011-03-13
4
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

660 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