Problems implementing a recursive find extension method - asp.net

I am attempting to implement a recursive extension method in VB.net that will find all objects with a certain property set so I can call it like so...
Dim camp As New CampaignStructure 'Populated with a full structure of course
Dim lstFoundItems As List(Of CategoryStructure) = camp.Categories.FindRecursive((Function(c) c.Found = False), 3)
My VB.Net classess and modules currently look like this
Imports System.Runtime.CompilerServices
Namespace MyStructure
Public Class CategoryStructure
Public Property CategoryID As Integer = Nothing
Public Property Name As String
Public Property Rank As Integer
Public Property Found As Boolean = False
Public Property Children As New List(Of CategoryStructure)
End Class
Public Class CampaignStructure
Public Property CampaignID As String = Nothing
Public Property Categories As List(Of CategoryStructure)
End Class
Public Module ControlExtensions
<Extension()> _
Public Function FindRecursive(cs As List(Of CategoryStructure), predicate As Func(Of CategoryStructure, Boolean), depthLimit As Integer) As List(Of CategoryStructure)
If cs Is Nothing OrElse cs.Count = 0 Then
Return New List(Of CategoryStructure)()
End If
If depthLimit = 0 Then
Return cs.OfType(Of CategoryStructure)().Where(predicate).ToList()
Else
'**ERROR IS THROWN HERE**
Return cs.OfType(Of CategoryStructure)().Where(predicate).ToList().Union(cs.Cast(Of CategoryStructure).Select(Of List(Of CategoryStructure))(Function(c) c.Children.FindRecursive(predicate, depthLimit - 1)).ToList())
End If
End Function
End Module
End Namespace
However I'm having casting problems when I'm unioning the recursive result with the current list at the point in the code marked. I can see why that's happening, just have no idea how to resolve it. Please do not send me C# examples as there is no 'yield' alternative in VB.net

That's because both sides of UNION have different signatures.
cs.OfType(Of CategoryStructure)().Where(predicate).ToList()
This returns List(Of CategoryStructure).
cs.Cast(Of CategoryStructure).Select(Of List(Of CategoryStructure))(Function(c) c.Children.FindRecursive(predicate, depthLimit - 1)).ToList()
This one returns List(Of List(Of CategoryStructure))
I think what you're looking for is:
Return cs.OfType(Of CategoryStructure)().Where(predicate).Union(cs.Cast(Of CategoryStructure).SelectMany(Function(c) c.Children.FindRecursive(predicate, depthLimit - 1))).ToList()
SelectMany returns flattened collection typed as IEnumerable(Of CategoryStructure).

Related

Property binding to web controls in asp.net with linq and reflection

I'd like to "bind" properties from a DTO to corresponding controls, for example checkboxlists or textboxes. This is so that I can use a dto (passed through the querystring) to set the initial state of the control, and then have the control set it's value to the dto when it comes time to leave the page. I could do this with two mapping functions (control->DTO, DTO->Control) but instead I'm using Linq and Reflection to maintain a list of property<->control relationships.
I've never done this before and I suspect there may be a better way ... how would you maintain a property<->control relationship?
Here is the code I've used:
Public Class ObjectPropertyMapper
''' <summary>Provides a binding of a control to one or more properties in a DTO.</summary>
Private Class ControlBinding
''' <summary>A reference to the control which will be bound.</summary>
Public Property Control As Control
''' <summary>The property(ies) which the control will be bound to.</summary>
Public Property Props As New List(Of PropertyInfo)
Public Sub New(ctrl As Control, props As IEnumerable(Of PropertyInfo))
Me.Control = ctrl
Me.Props.AddRange(props)
End Sub
End Class
''' <summary>A list of all the control to dto bindings for this page.</summary>
Private Property ControlBindings As New List(Of ControlBinding)
''' <summary>Returns the property info from a linq expression.</summary>
Private Function GetPropertyFromExpression(Of T)(linqExpression As System.Linq.Expressions.Expression(Of Func(Of T))) As PropertyInfo
If TypeOf linqExpression.Body Is MemberExpression Then
Return DirectCast(linqExpression.Body, MemberExpression).Member
Else
Dim op = (CType(linqExpression.Body, UnaryExpression).Operand)
Return DirectCast(op, MemberExpression).Member
End If
End Function
''' <summary>Binds a control to a single property of the DTO.
''' Usage: BindControlToProperty(cblFundTypes, Function() Me.SearchParams.FundTypeIDs) ... </summary>
Protected Overloads Sub BindControlToProperty(Of T)(ctrl As Control, linqExpression As System.Linq.Expressions.Expression(Of Func(Of T)))
' This works by passing through a linq expression, which in turn has a reference to the property.
' We can therefore extract the property from the linq expression, allowing us to store a reference to the property against the reference to the control
Dim prop As PropertyInfo = GetPropertyFromExpression(linqExpression)
ControlBindings.Add(New ControlBinding(ctrl, {prop}))
End Sub
End Class
And the consumer:
Public Class MyPage
Public Sub OnLoad
BindControlToProperty(myCheckBoxList, Function() MyDTO.IntListProperty)
End Sub
End Class
The only alternate method I found was projecting properties onto an anonymous type, as per https://stackoverflow.com/a/1984190/767599
An example would be:
Public Class exampleDTO
Public Property MyProp1 As String
Public Property MyProp2 As String
End Class
Public Function GetVariableName(Of T)(obj As t) As String
Dim properties As System.Reflection.PropertyInfo() = obj.GetType.GetProperties
If properties.Length = 1 Then
Return properties(0).Name
Else : Throw New Exception
End If
End Function
Public Sub BindToControl(Of T)(ctrl As Control, prop As T)
Response.Write(GetVariableName(prop))
End Sub
Public Sub Main
Dim c As New Control
Dim dto As New exampleDTO
BindToControl(c, (New With {dto.MyProp1}))
End Sub
However I have no idea which solution is "better".

Assigning value to item within an arraylist by index

I have this VB.NET ArrayList object which is working quite well. I have built it like this. It's the first one I have used.
Public Class MyObj
Private _str1 As String
Private _str2 As String
Public Property Str1() As String
Get
Return _str1
End Get
Set(ByVal value As String)
_str1 = value
End Set
End Property
Public Property Str2() As String
Get
Return _str2
End Get
Set(ByVal value As String)
_str2 = value
End Set
End Property
Public Sub New(ByRef pStr1 As String)
_str1 = pStr1
End Sub
End Class
Then I initiialise it doing this...
Dim MyObj1 As ArrayList = New ArrayList()
MyObj1.Add(New MyObj("myTestString"))
So this is all working later on. So I pack up the arraylist and store it in a class level variable. Then in a different method I grab my arraylist. I then want to assign a value to _str2. Does anyone have advice on how I would go about this. I keep trying to know avail. This is the sort of thing I mean.
For i = 0 To MyObj1.Items.Count - 1
MyObj1.Item(i)("Str2") = "tesstring2"
Next
As Tim wrote, you will be better off using a List instead of the old ArrayList.
To set it up and then access the property Str2 of the instances of MyObj:
Dim myList As New List(Of MyObj)
myList.Add(New MyObj("Hello"))
For i = 0 To myList.Count - 1
myList(i).Str2 = "World"
Next
Notice how it is .Str2, not ("Str2").

Convert IEnumerable(Of Object) to class that Implements IEnumerable(Of Object)?

Using VB.NET, I have ths class
Public Class MyCollectionClass
Implements IEnumerable(Of MyClass)
Public Property MadeThisClassCuzINeedToSetThis() As String
' code here
End Class
I want to do this, but get an exception saying I can't do this cast.
Dim objColl As MyCollectionClass
objColl = CType(IEnumerable(Of MyClass), MyCollectionClass)
Can anyone tell me how to get this to work. Thanks.
See this VB.NET/C# casting cheat sheet or the documentation on CType. The major problem is that the first parameter should be the instance to convert, not its type. This should work:
Dim myEnumerable As IEnumerable(Of MyObjectClass) = New MyCollectionClass()
Dim objColl = CType(myEnumerable, MyCollectionClass)
' objColl's type is inferred As MyCollectionClass
(note that as MyClass is a keyword, and I assume you actually have a different class name there, I changed it to MyObjectClass in my example)

Sorting for Ajax Combo-Box

I am wondering if there is any other way of sorting ajax combo box apart from this following example;
Public Class ListItemComparer
Implements IComparer(Of ListItem)
Public Function Compare(ByVal x As ListItem, ByVal y As ListItem) As Integer _
Implements IComparer(Of ListItem).Compare
Dim c As New CaseInsensitiveComparer
Return c.Compare(x.Text, y.Text)
End Function
End Class
Public Shared Sub SortDropDown(ByVal cbo As AjaxControlToolkit.ComboBox)
Dim lstListItems As New List(Of ListItem)
For Each li As ListItem In cbo.Items
lstListItems.Add(li)
Next
lstListItems.Sort(New ListItemComparer)
cbo.Items.Clear()
cbo.Items.AddRange(lstListItems.ToArray)
End Sub
----------Binding and Sorting Combo-Box----------
ddlClients.DataTextField = "ClientName"
ddlClients.DataValueField = "ClientID"
ddlClients.DataBind()
SortDropDown(ddlClients)
In the SP, i am using order by clause on Client Name but somehow combox doesn't bind ClientNames in order.
I like to to place this function in CommonFunction Class so i can call it from different places within the project. I am not able to add the above function in the BusinessLayer becuase of 'AjaxControlToolkit.ComboBox'

Properly Defining a Singleton in asp.net

I've the following class which is a singleton implementation:
Imports Microsoft.VisualBasic
Imports System.Xml
Public Class GlobalController
Private Shared instance As GlobalController
Private ControlsXmlDoc As XmlDocument
Private xmldocpath As String
Sub New()
ControlsXmlDoc = New XmlDocument
xmldocpath = HttpContext.Current.Server.MapPath("~/cp/GlobalControl.xml")
ControlsXmlDoc.Load(xmldocpath)
End Sub
Shared Function GetInstance() As GlobalController
If instance Is Nothing Then
Return New GlobalController
Else
Return instance
End If
End Function
Shared Property IsExtracting() As Boolean
Get
Return Boolean.Parse(GetInstance.ControlsXmlDoc.SelectNodes("global/extraction/proceed").Item(0).InnerText)
End Get
Set(ByVal value As Boolean)
HttpContext.Current.Application.Lock()
Dim node As XmlNode = GetInstance.ControlsXmlDoc.SelectNodes("global/extraction/proceed").Item(0)
If Not Boolean.Parse(node.InnerText) = value Then
node.InnerText = value.ToString
node.Normalize()
SaveDocument()
GetInstance.ControlsXmlDoc.Load(GetInstance.xmldocpath)
End If
HttpContext.Current.Application.UnLock()
End Set
End Property
Shared Sub SaveDocument()
GetInstance.ControlsXmlDoc.Save(GetInstance.xmldocpath)
End Sub
End Class
In my page I am doing something like this:
GlobalController.IsExtracting = False
Response.Write(GlobalController.IsExtracting)
I am always getting the output as "true". What is wrong with the code?
According this link Operator precedence and associativity, ! (or vb.net Not) have greater priority than == (= in VB.NET); so, your expression is always evaluated as
Not(True) And False
and never enters that If statement.
Try to use Boolean.Parse(node.InnerText) != value or Not (Boolean.Parse(node.InnerText) = value) in order to get correct result.
All, thanx for ur answers. I apologize for what I am about to say. I found the bug: it was with the way I implemented the singleton. Forgot to assign the newly created object instance to the shared variable.
Shared Function GetInstance() As GlobalController
If instance Is Nothing Then
instance = New GlobalController
End If
Return instance
End Function

Resources