/Home /Archive /Syndicate /Blog /Support /About /Contact  
All Visual Basic Feeds in one place!





Channel: Jared Parsons

One of the seldom used, and often unknown, features of VB extension methods is that the argument of the extension method (the first parameter) can be passed by reference.  This gives extension methods the power to change the reference that was used to invoke the value!  Obviously this can produce unexpected but often amusing behavior.  The following sample prints “False”. <Extension()> _ Public Sub EnsureNotNull( ByRef str As String ) If str Is ... [ read more ]


The WinForms Timer class allows the user to perform a particular action at a set interval.  Timer objects fire a Tick event at the set time which users can easily respond to.  This is very useful if a developer wants to check for a particular condition say every 2 seconds ( for the remainder of this article I'm going to use 2 seconds as a practical example even though it's really any arbitrary time period). Occasionally users are surprised to find that the Tick event will fire ... [ read more ]


The VB Catch syntax has a particular feature not present in C#: When.  It allows users to filter expressions based on something other than their type.  Any arbitrary code can enter a When block to decide whether or not to handle an Exception Sub Sub1() Try DoSomeAction() Catch ex As Exception When Filter(ex) Stop End Try End Sub Newsgroups often ask, "Why's this so special? I could effectively get the same behav ... [ read more ]


The August issue of MSDN magazine will be carrying an article I wrote this spring.  In it I toy around with using the deferred execution and lazy evaluation properties of LINQ to create more responsive UI code.  You can view the article here .  As usual I appreciate any feedback you have. 


Recently I've done a bit of posting about the difficulties of properly implementing equality in VB (and DotNet in general).  While most of the problems can be fixed with a standard snippet the one really hard to implement issue is GetHashCode().  The rules for GetHashCode() are both simple and seemingly contradictory If two objects are equal (via Equals) their GetHashCode() should be equal GetHashCode() shouldn't ever change These rules imply that GetHashCode() is relate ... [ read more ]


After my recent postings on the rules of Equality , I thought it would be a good idea to post a simple example of equality.  The class in question, Example, has only one field of type Integer name m_field1.  Two instances of Example are equal if m_field1 has the same value.  So the real equality check is just a single Integer comparison.  Unfortunately, as my posts alluded to, even though the check is simple getting it right is not necessarily so.  The equality port ... [ read more ]


This is a bit of a follow up to a previous post we discussed how to properly implement equality in VB.  Several users commented/asked that IEquatable(Of T) could be used in place of overriding Equals().  Since IEquatable(Of T)   doesn't define a GetHashCode() method the user didn't need to define it and hence run into all of the problems associated with GetHashCode() usage.  Unfortunately this is not the case.  Several parts of the framework link IEquatable(Of T ... [ read more ]


Many developers want to implement equality functions for their objects.  DotNet made equality a deep part of the framework and added support all the way up to System.Object with Equals and GetHashCode .   In addition to the strongly enforced method semantics of GetHashCode and Equals there are also several other hard to enforce patterns that developers must follow in order to properly integrate into the rest of the DotNet framework.  We'll explore those rules today. B ... [ read more ]


Recently we had a good discussion on an internal alias about the use of Me, MyClass and MyBase in VB.  Me, MyBase and MyClass are all ways to access instance member data in a VB class or structure.  There was a little bit of confusion on the actual workings and meanings of the keywords in various contexts and I want to use this post to shed light on the different meanings. The Basics The keywords are used to alter the way in which instance members of a class/structure are accesse ... [ read more ]


Previously I discussed a potential missing API in List(Of T).BinaryInsert.  One of the items I mentioned was it had better performance because it was O(Log N) vs Insert and Sort which is O(NLogN).  Several users correctly pointed out this was incorrect and that Insert() had the additional overhead of an Array.Copy() which is O(N)ish.  But most agreed O(N) + O(LogN) was better than O(NLogN).  Given that I already missed a key portion, I decided to write a test program to ... [ read more ]


IComparable(Of T) is an interface saying "I can compare myself to other objects of the same type".  And IComparer(Of T) is an interface saying "I can compare two objects of this type.".  Often API's which need to perform comparisons will take an instance of IComparer(Of T) instead of IComparable(Of T).  Doing the opposite is limiting in a few ways.  The first is it locks developers into the behavior defined by the type author essentially preventin ... [ read more ]


One API that seems to be missing from List(Of T) is a BinaryInsert method.  Especially since there is already a BinarySearch method. Binary insert is a method for inserting a value into an already sorted list.  Since the list is already sorted we can do a binary search to find the appropriate place to insert.  The insert keeps the list sorted so the cost of a binary insert is just the cost of the search which is O(Log(N)).  An alternative method for keeping a sor ... [ read more ]


Besides waiting, the another important issue when dealing with Futures is how to deal with exceptions thrown by the user specified code.  Option 1: Ignore the Exception Don't take any actions in the future code and force users to write exception free code.  IMHO this is not the best way to approach the problem.  The code will be running in the thread pool and unhandled exceptions in the thread pool result in the taking down of an appdomain/process.  In addition Futu ... [ read more ]


A tuple in computer science can be described as a set of name/value pairs.  In some cases it can be described as simply a set of values that are accessible via an index [1].  Previously I discussed how to create a Tuple inside of PowerShell .  This series will focus on the use of Tuples in DotNet and how to use PowerShell to generate DotNet code.  This series will also distinguish between mutable and immutable tuples.  As DotNet is shifting it's focus on parallel pr ... [ read more ]


Fortune is a Unix command that gets a random message from a set of databases and displays it on the screen.  These messages have a wide variety but tend to be funny, quirky or famous quotes (most are indeed geeky).  Nearly all unix systems have a version of Fortune.  Windows doesn't have any version by default.  It provides no real functionality other than humor and amusement but it's something I miss.  Most users add it to their .profile script so they get a new ... [ read more ]


One of the gotchas for Extension Methods is that it's legal to call them on Null References.  This isn't really surprising when you think about the feature.  Boiled down to a fundamental level, extension methods are just syntactic sugar for calling a static method and automatically passing the first parameter [1].  However it can catch the unwary off guard.  The two items that are a little bit different between calling an Instance vs Extension method on a null reference i ... [ read more ]


This is somewhat of a follow up on a previous post I did on the difference between IEnumerable(Of T) and the IEnumerable interfaces.  I've seen several people type in the following code and wonder if there was a fundamental bug in the type inference code. Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .Load For Each cur In Controls cur.Text = "A Value" Next End Sub ... [ read more ]


The title of this post essentially says it all.  AutoSize and DockStyle.Fill don't mix well together.  Both properties exist to describe the size relationship relative to the rest of the control but they do so in conflicting ways. AutoSize is a property describing the size of a control relative to it's contents.  Setting this to True will generally cause a control to resize itself so that it takes up only enough room to display it's contents.  DockStyle.Fill is a prop ... [ read more ]


One item you strive to avoid when you design and implement a feature is unexpected behavior.  Unfortunately there is one case we couldn't avoid with Lambda's in VB9.  I just ran into the this problem when coding up a handler.  I wanted to disable a button when the text of particular TextBox was empty.  I wrote the following code to handle the situation. AddHandler c.TextChanged, Function () okButton.Enabled = ( 0 <> c.Text.Length) This doesn't quite do what I ... [ read more ]


Recently I was asked how can you get a list of anonymous type member names given a query of anonymous types.  The quick answer is that you can use a quick bit of reflection to get back the names.  Public Function GetAnonymousTypeMemberNames( Of T)( ByVal anonymousType As T) As List( Of String ) Dim type = GetType (T) Dim list = New List( Of String ) For Each cur In type.GetProperties(Reflection.BindingFlags.Public Or Reflectio ... [ read more ]


IEnumerable(Of T) is a huge step up in the 2.0 framework from the original IEnumerable interface.  It provides a typed enumeration which eliminates lots of nasty casts.  The best part is that IEnumerable(Of T) is backwards compatible with IEnumerable (it inherits from it).  What's frustrating is that IEnumerable is not forwards compatible with IEnumerable(Of Object).  This prevents you from using IEnumerable anywhere IEnumerable(Of Object) or an inferred IEnumerable&l ... [ read more ]


This discussion is building upon a previous post on how to acquire an anonymous type ... type .  The next question is, how can you cast an arbitrary object into an anonymous type?  At a glance this doesn't seem possible as you cannot directly express the type of an anonymous type in code.  For instance the following code is not legal.  Dim o As Object = SomeCall() Dim x = Directcast (o, New With {.a = 5}) Even though it's not possible to directly exp ... [ read more ]


© 2005 Serge Baranovsky. All rights reserved.
All feed content is property of original publisher. Designated trademarks and brands are the property of their respective owners.

This site is maintained by SubMain(), a division of vbCity.com, LLC