Weblog Of Nirandas | home

Developer From INDIA

Find matching items from a List - simple example of VB.NET 2008 lambda function

clock November 9, 2009 05:15 by author Nirandas

Here is a very simple example showing how to find all matching items from a generic List(of T) using lambda functions in VB.NET.

Dim MatchedItems As List(Of User) = Members.FindAll(Function(u As User) (u.Age<18))

Here the FindAll() function is accepting a Predicate(Of User) Delegate. And we can pass lambda functions where ever a delegate to a function is accepted. The FindAll() method calls our lambda function for each item in the list for deciding whether the item matches our requirements. If it does (our Predicate(Of T) returns True) the Item is included in the returned list.

Another nice thing is that this feature (lambda functions) are supported even in projects where the target .NET framework version is 2.0. So, we can use these lambda functions in our .NET 2.0 application as wel.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Is it OK to not have explicit return statements in functions?

clock October 21, 2008 04:41 by author
Simple answer: no. There are basically two types of methods in .NET world. First those do not return any values (void methods in C# or subroutines in VB). Second those methods which do return some values (non void methods in C# and functions in VB)So, failing to have a return statement where the calling code expects one, should be reason enough to cause a compilation error. Not so in VB.NET 2005, at least not with the default configuration.Couple of days ago I spend few minutes understanding this same issue. Yes, not explicitly returning any value from a function is a huge programming error. But it becomes even worse when you don’t get to know it until runtime.The value returned to the caller when there isn’t any explicit return statement in the code is the default value for the return type of the function. Or in other words, the result will be as if you have returned nothing. For example, if you have following function in your code:Private Function test() As IntegerEnd FunctionThen somewhere else have this statement:MessageBox.Show(test.ToString())The program will compile normally and produce a message box with 0 (the default value of integer) in it. If we change the return type of the test method to string, the function will be returning nothing (return nothing) the default value for any object.Does anyone know any way around this issue, any changes to configuration? So that VB.NET compiler becomes stricter? Yes, it isn’t such a problem. But still, the compiler should alert us to such silly mistakes which we may make time to time.
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


vb.net: Creating registry keys made simpler with My.Computer.Registry

clock July 2, 2008 06:17 by author

Creating registry keys and keeping it up to date is now very easy in vb.net using the class My.Computer.Registry. The SetValue() method will create the specified keys if it does not exists and set it's value to the value provided. It is certainly a helpful method as now we do not have to worry about creating keys if doesn't exists etc.

A simple example follows:

My.Computer.Registry.SetValue("hkey_classes_root\txtfile\Shell\Process\Command", "", """" & Application.ExecutablePath & """ ""%1""")

This will create a item named "Process" in the context menu for all the text files which will be shown when user right-clicks on any of the text files in the windows explorer. And choosing "Process" from the context menu will start your application with the name of the file on which the user right-clicked as the command-line argument to your program.

The My.* namespaces are in the Microsoft.VisualBasic.Dll which is automatically referenced in any VB project.

Happy hacking!

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Who Am I?

I am Nirandas - a developer from INDIA

Sign in