I'm kind of going nuts here. I have a function something like the following. It's failing to return an object. I can pass in a list, I can see in QuickWatch that x.RB = theRb for at least one of the items in the list, yet it doesn't exit the loop (via the Return). The loop continues.
The list I am passing in is a subclass of aXXX.
Property RB on class aXXX is of type RBEnum.
Also, I originally used Linq for this but was getting "no matching items" exceptions.
Private Shared Function GetX(Of T As aXXX)(ByVal a As List(Of T),
ByVal theRb As RBEnum) As T
For Each x As T In a
If (x.RB = theRb) Then Return x
Next
Return Nothing
End Function
Any suggestions or ideas on why this isn't working?
I would recommend trying:
If (x.RB.Equals(theRb)) Then Return x
Can you cast the Enum into an Integer and then compare?
If CInt(x.RB)=CInt(theRb) Then Return x
I'm not sure how your original where statement was written but this should produce the result you're looking for:
Private Shared Function GetX(Of T As aXXX)(ByVal a As List(Of T),
ByVal theRb As RBEnum) As T
Return a.Where(Function(x) x.RB = theRb).FirstOrDefault()
End Function
I have resolution. I can't fully explain it though.
The list of items I'm passing in are a subclass of the class aXXX. The subclass did not properly override the RB property from the base class -- no Overloads / Overrides / Shadows. This kind of gives explanation as to why QuickWatch reports True on the match -- maybe this subclass property was hiding the "real" property value that was in the test?
Anyway, by taking out the property in the subclass all together or adding an Overloads, the For Each behaves as one would expect. I can even go back to the original Linq version I had in the function.
I guess this came down to oversight / sloppy coding on my part. But the issue was masked pretty well by the fact that QuickWatch reported "false positives"!
Thanks to everyone for the suggestions and help.
Related
I'm currently writing a wrapper object for a series of database result sets. In so doing, I've noticed a problem when querying with LINQ and Lambda expressions. Specifically, calling a method within the lambda seems to always make the result set empty and never actually fires the method I'm attempting to filter with. Here's the code:
' Query and filter container results
Public Function [Filter](pFilter As IFilter(Of T)) As System.Linq.IQueryable(Of T)
' THIS YIELDS AN EMPTY SET EVEN WHEN pFilter.Test(o) ALWAYS RETURNS TRUE
Dim lResult As System.Linq.IQueryable(Of T) = mTable.Where(Function(o As T) pFilter.Test(o))
Return lResult
End Function
pFilter implements IFilter with this signature:
Public Interface IFilter(Of T)
Function Test(ByVal pObject As T) As Boolean
End Interface
I've break pointed pFilter.Test(o) and found it is never actually called. Oddly enough, if I replace pFilter.Test(o) with True, I receive the entire table of records as expected. Also, I receive no compile time or run time errors in any case.
I'm pretty new to Lambdas and LINQ so fully recognize I may not understand the limits of what I am attempting to accomplish. Any help is greatly appreciated!
SOLUTION:
I've marked a solution already as the author got me on the right track. The true nature of this problem stems from my attempting to force a .NET function into something LINQ could turn into an SQL statement (which is impossible). To solve this, I've changed my IFilter to return System.Linq.Expressions.Expression(Of Func(Of T, Boolean)) for the Test method. Now I can create strongly-typed filters which return predicates and pass them directly to Where() without the use of a lambda expression. I'm also using LinqKit to make this work more easily for those who may be accomplishing similar tasks.
I think I found what your issue is - understanding the concept of IQueryable. Here is what it says in this article: Lazy Loading With The LazyList
IQueryable is the cornerstone of Linq To Sql, and has (what I think)
is a killer feature: delayed execution. IQueryable essentially creates
an Expression for you that you can treat as an enumerable list - and
only when you iterate or ask for a value will the query be executed.
So you probably never used the result, and that's why it was never called. .NET framework only built the expression tree for you, but did not do any processing.
See also: IQueryable vs. IEnumerable.
I have inheritance structure: Foo implements IGraphNode inherits IGraphItem.
Foo, IGraphItem/IGraphNode, and the implementation for IGraphItem/IGraphNode all reside in separate assemblies. I am using an inversion of control container, so the project I'm working in has a reference to the first two (Foo and IGraphItem/IGraphNode), but not the implementation of IGraphItem/IGraphNode. I also have Option Strict on as it is required for this project (turning if off didn't fix the problem). I'm using .NET 3.5.
I am passing a IGraphItem around and I have code that looks like this:
Public Sub ProcessItem(of IGraphItem)(item As IGraphItem)
If TypeOf item Is Foo Then
Dim f1 = CType(item, Foo) 'Compiler error
Dim f2 = DirectCast(item, Foo) 'Compiler error
'This is what I'm currently having to do. It works.
Dim f = CType(CType(item, IGraphNode), Foo)
'Do stuff
End If
End Sub
Any idea why I'm having to do this? I should add that TryCast works, but since we've just confirmed that item's type is Foo, I don't see why I can't DirectCast it. Shouldn't it just let me and throw an exception if I'm wrong? Is there a better way to accomplish what I'm trying to do?
Your original code compiles without a problem, even when target framework is 3.5.
The problem with your current code is that you've defined a generic method whereas IGraphItem is not the type of your interface but the generic type T which can be any type. But it cannot be another type than T and you're trying to cast it to type Foo.
If you would change your method signature to anything else it would work, for instance:
Public Sub ProcessItem(of IGraphItm)(item As IGraphItem)
I assume that you're somehow "shadowing" the type IGraphItem of your interface with the generic type IGraphItem in this method.
It would also work if you would explicitely tell the compiler that item As IGraphItem actually is a item As YourNamespace.IGraphItem.
I'm sure Jon or Eric could explain it better, but maybe it's helpful anyway ;)
This article might answer your question. If not, well, it's an excellent reading anyway.
http://blogs.msdn.com/b/ericlippert/archive/2009/03/19/representation-and-identity.aspx
In my project (which I inherited from someone) there are a lot of functions like:
Public Function DoSomething(ByVal s As String)
' Do something to public properties
End Function
And they are called like this:
DoSomething(s)
So the return value is ignored (which is object, as I see in the docs). Is it safe to change all these functions to Subs? Could I break something which isn't so obvious?
Should be safe. That's what Subs are for, this is really bad style by your predecessor.
Is there any specific reason you want to convert to Subs? From your code the Function has no return type - in this situation I can't see there being any problem.
(Not really sure if I phrased the question correctly...)
I want to create a lambda expression that would take an Object, attempt to convert it to a passed-in Type, and print to the console whether it was successful or not.
At a glance, the lambda expression may seem a pretty silly way to accomplish this task, but I'd really like to know what I'm doing wrong, so I can better grow my skill set.
VS gives me a designer error about the second "T" in the expression below, telling me it isn't defined)
This is where I left off:
Sub MyMethod(ByVal param as Object)
Dim quickMethod = Sub (Of T)(o as Object)
Console.WriteLine(TryCast(o, T) IsNot Nothing)
End Sub
quickMethod(Of myClass1)(param)
quickMethod(Of myClass2)(param)
quickMethod(Of myClass3)(param)
quickMethod(Of myClass4)(param)
'further logic below... ;)
End Sub
I can't speak for VB specifically, but I'm not aware of any such concept in .NET delegates in general. While a delegate type can be generic, I don't believe you can leave a particular delegate instance "open" in a type parameter, to be provided by the caller. It's an interesting idea though.
Of course, you could easily write a generic method to do this, and that's probably the right way to go. It's an interesting situation where you could have a single-method interface expressing the desired functionality, but you can't express that as a delegate type. Hmm. Just for the sake of discussion, the interface could be something like this:
interface IConverter
{
bool IsConvertible<T>(object input);
}
All
I am currently trying implement something along the lines of
dim l_stuff as List(of Stuff)
dim m_stuff as new Stuff
m_stuff.property1 = 1
m_stuff.property2 = "This"
if not l_stuff.exists(m_stuff) then
l_stuff.add(m_stuff)
end if
This fails obviously as the Exist method is looking for a predicate of Stuff.
Can anyone fully explain the predicate and how i can achieve what I am trying to do here.
I have tried to use
if not l_stuff.contains(m_stuff) then
l_stuff.add(m_stuff)
end if
however this doesn't detect the idenitcal entry and enters a duplicate into the list
Thank
List(Of T).Contains is the method you should be using. Exists, as you say, expects a predicate. Of course, for .Contains to work as expected, you need to override the Equals() method, as well as GetHashCode().
List(Of T).Exists expects a function that will return a Boolean value when passed an item of type T, where T, in your case, is of type Stuff. So, you could write a method that looks like:
If Not l_stuff.Exists(Function(x) x.property1 = m_stuff.property1 And _
x.property2 = m_stuff.property2) Then
and so on.