Solved

TypeName as parameter

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

636 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