C# - Use Or Not Lambda Expressions?
Nov 11, 2009
I see lambda expressions have become a very useful tool at some points in the language. I've been using them a lot and most of the time they fit really nice and make the code shorter and perhaps clearer.
Now.. I've seen some , I would say excessive use of them. Some people like them so much that try to use them everywhere they can.. Some times the C# code looks like a functional language. Other factors against are the cost using reflection by lambda and that not friendly to debugging.I would like to hear opinions about how good and how code clear it is to use more or less the lambda expressions. (this is not the better example, but let's say it was the trigger)I was writing the following code. The use of the delegate { return null; } helps me avoid having to ask if the event is null or not every time I have to use it.
public delegate ContactCellInfo.Guest AddGuest();
public event AddGuest GuestRequest = delegate { return null;}
Im using resharper and the wise resharper( even it some times literaly eats the memory) made me the following suggestion
public delegate ContactCellInfo.Guest AddGuest();
public event AddGuest GuestRequest = () => null;
At my point of view the code using the delegate looks clearer. I am not against the Lamdba expression just would like to hear some advices on how and when to use them.
View 1 Replies
ADVERTISEMENT
Jan 20, 2010
Functionally, is there any difference (apart from syntax onbviously) between lambda expressions in C# and VB.Net?EDIT: following up on CraigTP's answer: any references to the situation in .Net 4?EDIT: I'm asking because I'm used to C#, but for a next project the customer asks VB.Net. We're not a priori against that. We realize that most language constructs are supported in both languages. However, we're particularly fond of the way C# implements lambda expressions. We would like to have an overview of the differences with VB.Net
EDIT: accepted CraigTP's answer for pointing out what I currently consider the most important difference.
So to summarize: VB.Net 9 does not support multiline statements in a lambda expression, and a lambda must always return a value. Both of these issues are addressed in VB.Net 10
View 5 Replies
Apr 20, 2012
Async Sub like this: Dim f As Func(Of Task) = Async Sub() End Sub Produces compiler error: error BC36670: Nested sub does not have a signature that is compatible with delegate 'System.Func(Of System.Threading.Tasks.Task)'.Equivalent C# code compiles fine:
Func<Task> f = async () => { };Rewriting Async Sub into Async Function make code works.
Why does Async Sub() is not convertible to delegate types with return value of type Task?
View 1 Replies
Jul 4, 2010
how to convert this code to VB.net, can anybody help?
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached(
"Command",
[code]...
View 1 Replies
Feb 3, 2012
I have a List(of T) where T has a property that is a list of checkboxes, what I need is a Lambda expression that will count all the checked checkboxes in the list. I tried with:
[Code]...
View 5 Replies
Jul 19, 2011
I just came across a bug in NHibernate which happens to already be raised: [url] I'm not sure if this applies to anything else other than enums but when using a Lambda from VB, it looks different to the same Lambda from C#.
C#:
Where(x => x.Status == EmployeeStatus.Active)
VB
Where(Function(x) x.Status = EmployeeStatus.Active)
They are the same as far as I'm aware? (My VB isn't great) If I put a break point on the same line of code, where the above code is passed into. In C# I get: On the same line when VB version is passed in, I get: Is this something I'm doing wrong? Is the result's the same, just displayed different between C#/VB?
Edit: Ok so they are displayed different, but they can't be the same because NHibernate cannot handle it. The C# version is handled perfectly fine by NHibernate, the VB version resolves in the following exception being thrown: [Code]
View 3 Replies
Jul 25, 2011
I have an issue with building a project using the MSBuild (ver 4) from the command line when declaring lambda expression like this:
Private Sub Foo(ByVal s As String)
Dim WL = Sub(str As String)
If Not String.IsNullOrEmpty(str) Then[code]......
View 1 Replies
Mar 6, 2009
I'm trying to expand my programming horizons, and LINQ and Lambda Expressions have popped up as some apparently very important topics. As I began reading articles on Lambda Expressions, it appears that Lambda Expressions are built heavily on the concept of Delegates. As I began reading articles on Delegates, it appears that Delegates are built heavily on the concept of callback functions. As I began reading articles on callback functions, .[code]Can anyone out there give me a simple, clear, concise definition of callback functions? Do I need to learn C or C++ to fully understand these concepts? Maybe a better question is: What do I need to know to be able to understand Delegates (they've always somewhat mystified me) and LINQ/Lambda Expressions (I haven't looked into these too much, but they're mystifying, too)?I don't mind doing research, but google searches are giving me way too much information.
View 4 Replies
Jul 7, 2010
what are delegates?How are the lambda expressions related to the delegates?
reading the msdn about the delegates is really confusing me ;so i need a start off from here before moving on to the msdn.
View 27 Replies
Jul 1, 2011
To streamline code, I increasingly rely on lambda expressions. But I may have hit against the wall in the attempt of removing an event handler set by a lambda expression. This really eludes me. The code below shows two samples: Alt1 uses a plain lambda expression as delegate and works fine: the object size increases as the numeric control varies. However, the drawback is that the handler cannot be removed:
RemoveHandler Me.upDnScale.ValueChanged, Sub(sender As Object, e As Object) ScaleObject1(sender, e, objTgt)
According to this MSDN article, a variable should be used to set/remove lambda based handlers. Alt2 shows a couple of attempts, but none works.
[Code]...
View 1 Replies
Feb 22, 2010
I have a type lets call it "MyType". I have a List(Of MyType). Here is what i'm doing: MyList.Sum(Function(x) x.MyFieldToTotal) "MyFieldToTotal" is a decimal. For the life of me i can't figure out why x above is an object rather than a type of "MyType". Shouldn't Type Inferencing be working in this case? Even in intellisense i get "selector as System.Func(Of MyType) as Decimal"
[Code]...
View 1 Replies
May 21, 2012
Currently I am searching through my list to find Customers that match on Address.
I need to match on both address and city. How do I rewrite my lambda Expression to match both criteria?
CustomerList.FindAll(Function(c) c.Address = addressToMatch)
View 1 Replies
Jul 2, 2011
How to remove a handler set up by a lambda expression? According to this MSDN article, lambda expression should be assigned to variables which should be used to define handler operators. So far, I have used lambda expessions w/o variables.
For instance, the following works:
Private Sub ScaleObjectInit1(ByVal objSrc As NumericUpDown, ByVal objTgt As FrameworkElement) ...
AddHandler Me.upDnScale.ValueChanged, Sub (sender As Object, e As Object) ScaleObject1 (sender, e, objTgt)
End Sub
Private Sub ScaleObject1(ByVal sender As Object, ByVal e As Object, ByVal objTgt As FrameworkElement)
[Code] .....
View 3 Replies
Jun 30, 2011
Can someone who is using a 64 bit machine help me pintpoint this crash to either the VS2010 IDE itself or one of the plugins I have installed?
It's the code fragment below. When I copy & paste it into the Main() function of a new VB.Net console app on my Win7 64 bit machine, the VS IDE crashes and dies onthe spot, every time I try it.
Doing exactly the same on a 32 bit XP machine, nothing abnormal happens.
The 64 bit machine does have some IDE plug-ins installed, the biggest of which is DevExpress (the free version), so i think either one of those or the fact that the IDE is running on 64 bit must be the culprit.
This is the code. The static variable can also be made a module-level variable, with the same result.
View 2 Replies
Nov 16, 2009
I'm still trying to get my head around the whole "Lambda Expressions" thing.
Can anyone here give me an example ordering by multiple columns using VB.Net and Linq-to-SQL using a lambda expression?
Here is my existing code, which returns an ordered list using a single-column to order the results:
Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).ToList
Note: The WebCategory object has a child WebPage object (based on a foreign key). I'd like to order by WebPage.DisplayOrder first, then by WebCategory.DisplayOrder.
I tried chaining the order bys, like below, and though it compiled and ran, it didn't seem to return the data in the order I wanted.
Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).OrderBy(Function(c As WebCategory) c.WebPage.DisplayOrder).ToList
View 2 Replies
Mar 16, 2009
I have an untyped dataset returned from my WebService. The user wants to dynamically construct a query referencing tables and columns and specifying values to test for.I have looked at Lambda Expressions and the Expressions.Expression Namespace. I think these provide the answer I'm looking for, but I'm not certain how to go about contructing the linq expressions to extract the datarows I'm looking for.
I plan on limiting the result to one datatable that the query will result in and i"ll provide the joins from the user's constructs.For a simple example I have a DataTable with (n) rows called "Table", and in it there is a computed column called "b_IsActive" that has the expression "Convert(IsActive, 'System.Boolean')". The user wants to retrieve all rows in "Table" where field "b_IsActive" is true.
View 5 Replies
May 20, 2009
I'm looking for an advanced level VB.NET book which covers LINQ and Lambda Expressions.Generally I read C# .NET books due to lack of good VB.NET books when it comes to generic .NET Framework related subjects. However Lambda and LINQ is quite different in C# and VB.NET I'm looking for an advanced level VB.NET book on this subjec
View 1 Replies
Jun 21, 2011
Im hearing about this, can someone explain it to me or please show some basic code of Lambda with explanation.
View 2 Replies
Dec 14, 2009
Is it possible to shorten the following function to a lambda expression?Or (to do the trick by myself) what is the best and most understandable for beginners tutorial for lambda in vb.net?[code]
View 2 Replies
Jan 4, 2010
if i do not need to evaluate all the expressions, am i right to say that there are benefits and no disadvantages of using AndAlso compared to using And
example:
if a=b And f=g And y=k
can be better done with:
if a=b AndAlso f=g AndAlso y=k
View 22 Replies
Apr 25, 2012
I wish to incorporate general expressions in my Visual Basic work.Other than the basic inputting "Imports System.Text.RegularExpressions", I have no idea how to do put general expressions in my VB project.However, I can create the expressions (so, all is not lost).
View 1 Replies
Feb 20, 2011
The expression 3>6 AndAlso 7>4 evaluates to
A. True B. False
The expression 4>6 OrElse 10<2*6 evaluates to
A. True B. False
The expression 7>=3+4 OrElse 6<4 AndAlso 2<5 evaluates to
A. True B. False
View 4 Replies
Jan 15, 2011
i need a regular expressions string to get all anchor tags in a page with a specific css class name, in c#/vb.netthis is what i got so far"<a.*?href=""(.*?)"".*?>(.*?)</a>"ut my attempts to add "class=name" isnt working, also is it possible to find links where the class name appears either before or after the href with one expression
View 4 Replies
Mar 30, 2011
I am looking for a way to simplify algebraic expressions in VB.NET. It is preferred if you can give me a link to a pre-written library or class.
View 1 Replies
Feb 13, 2011
I am new to VisualBasic and trying to learn it online. For my first assignment I have to make a program that allows someone to input a time in AM/PM and select the time zone and automatically output a picture and three other time zones. I think I have it for the most part but I am having an issue with one part in particular. Since the time is input using AM or PM if the original time is 11:00 AM and I add an hour it should switch to PM. But I'm not quite sure what I'm doing wrong. I basically need to say If the time is equal to 12 and the meridiem is equal to am then it should switch to pm but I keep getting errors inidicating they are trying to switch to a boolean value.
[Code]...
View 1 Replies
Nov 14, 2009
I'm in a Collage VB.Net Class and I was wondering if it is possible to make an array of expressions like : Dim exps() = { 5*4,2+3,5*2}.
View 3 Replies
Nov 11, 2011
The following works in vb.net, and basically only allows characters on a standard US Keyboard. Any other character pasted gets deleted. I use the following regular expression code:
"[^A-Za-z0-9[{}]`~!@#$%^&*()_-+=\/:;'""<>,.|? ]", "")
View 2 Replies
Jun 24, 2011
I'm trying to do is some password validation. It needs to have at least 1 lower case character, 1 uppercase character, 1 number and at least 6 characters in length. I figured I could do this using "RegularExpressions" however I can't figure out how to determine the "at least one occurance" of each. For example I thought the following would work but it only validates that what is input is a lower case, uppser case or numeric character, not that the input has at least one of each. The user should be able to enter any characters but at least one of lower case, upper case and numeric characters.
Dim
myMatch
As
Match
[code].....
View 2 Replies
Mar 5, 2011
I have a string list which has hundrens of elements. Now I want to find a rule to distinguish them according to their similarity. There are these kinds of pattern.
Group 1: "ABC 20", "ABC 20 Dup". There is a substring "DUP" in the last position.
Group 2: "saliva 4", "saliva 4_2","siliva 4_3", etc. There is a substring "_" plus a number in the last position.
Group 3: "sal_1", "sal_1b","sal_1c", etc. There is a character in the last popsition.
Group 4: "NA222", "NA222b", there is a character in the last position.
Group 5: "1","1_2","10","10_2" etc.
I want to put them into a dictionary if they are similar.
View 1 Replies
Jul 12, 2011
I have a number that always will be in this format
1815131217 Lets say I want to parse out all the 1's in between leaving only 85327
How can I do that?
What if my number is 1310111512
The number needs to be 30152 How can I do that?
View 6 Replies