Showing posts with label VB 9. Show all posts
Showing posts with label VB 9. Show all posts

Thursday, November 29, 2007

VB 9 Series - Friend Assemblies

Welcome to the 3rd Article in the Series of VB 9. In this article we'll look at a new feature added to the VB language i.e. InternalsVisibleTo Attribute - 'Friend Assemblies'.

You might have had some problem in the past where you want to share methods of your 'Friend Assembly' to a particular assembly or assemblies. Now in VB 9.0 you have a way! for this you just have to use an assembly level attribute 'InternalsVisibleTo' (can be found under System.Runtime.ComplierServies) which takes the name of the assembly to which you want to share your methods without extension.

To understand this suppose you have made a Class Library Project say 'testFriend' and write the following code for the class:

Friend Class FriendofDemoClass

.......Function SomeFunction() As Integer

..............Return 0

.......End Function

End Class

Now create a 'Console Application' 'Demo' and after adding the refrence of 'testFriend' make an object of 'FriendofDemoClass' when you make an object of this class outside its scope ('Friend' can only be accessed within the scope of its assembly) you get this error.



Now go back to your 'testFriend' Library and add the 'InternalsVisibleTo' attribute which can be found under System.Runtime.CompilarServices.

Imports System.Runtime.CompilarServices

< InternalsVisibleTo.("Demo") > _
Friend Class FriendofDemoClass

.......Function SomeFunction() as Integer

..............Return 0

.......End Function

End Class

Now everything works fine !

Wednesday, November 28, 2007

VB 9 Series - Object Initializers

Welcome to the second article of VB 9 Series. In this article I'll try to explain the new functionality introduced in Visual Basic 9 i.e. Object Initialization... with the help of this technology you can very well separate object Initialization logic, you may had some problem or have to write long code to initialize certain values in your object and can't find those in the constructor consider the following example code:

Public Class
Customer

.........Private _name As String
.........Private _address As String
.........Private _City As String

.........Public Sub New()
..................'default constructor
.........End Sub

.........Public Sub New(ByVal Name As String)
.................._name = Name
.........End Sub

.........Public Property CustomerName() As String
.........Get
..................Return _name
.........End Get
.........Set(ByVal value As String)
.................._name = value
.........End Set
.........End Property

......... Public Property Address() As String
.........Get
..................Return _address
.........End Get
.........Set(ByVal value As String)
.................._address = value
.........End Set
.........End Property

.........Public Property City() As String
.........Get
..................Return _City
.........End Get
.........Set(ByVal value As String)
.................._City = value
.........End Set
.........End Property

End Class

And then you Instantiate the object of this class like this:

.........Dim MyCustomer As New Customer("Ali")
.........MyCustomer.Address = "xxx"
.........MyCustomer.City = "Lahore"

Or can also use the bellow code to initialize the values:

.........Dim MyCustomer As New Customer("Ali")
.........With MyCustomer
...................Address = "xxx"
...................City = "Lahore"
.........End With

But to keep the initialization logic togather VB 9 has introduce new functionality now you can use the bellow code to set the values while instantiating an object:

.........Dim myCustomer As New Customer("Ali") With {.Address = "xxx", .City = "Lahore"}

IntelliSense will help you to set the different properties related with the object.

I hope you get the idea of this yet new powerful feature of VB 9.

Good Luck !

Tuesday, November 27, 2007

If Ternary Operator

This Article is first in the series of VB 9 articles. In this article I'll introduce you to the If Ternary Operator which the VB language lacks in its previous versions. You as a VB Programmer some how familiar with the Iif function or 'Immediate If'. The syntax of 'If Ternary Operator' is the same as compared to Iif i.e. Iif(Boolean,True,False) and you use it this way:

Dim
Amount As Integer = 100
Dim
ASign As Integer = Iif(Amount < 0, -1, 1)

When you execute the code the Iif function checks the condition, if the value of 'Amount' is greater than 'zero' it will return '1' otherwise '-1' pretty basic but what's the problem? actually the problem is that as it is a function which belongs to some library not a built-in operator or function of VB language therefore in VB all of the arguments evaluates before execution. Look at the following example:

Sub Main()

Dim Amount As Integer = 100
Dim ASign As Integer = IIf(Amount < 0, Negative, Positive)
Console.WriteLine(ASign)
Console.ReadLine()

End Sub

Function Negative() As Integer
MsgBox("In Negative Function")
Return -1
End Function

Function Positive() As Integer
Return 1
End Function

Although we are only interested in the Positive Funtion but as it's a function it will evaluate all the arguments here is the output of this code:



Therefor, Iif is not equal to the following code:

Dim Amount As Integer = 100
If i < 0 Then
Negative()
Else
Positive()
End If

But now we have the solution in VB in spite of writing the above code the same objective can be accomplished with the help of 'If Ternary Operator':

Dim Amount As Integer = 100
Dim Asign As Integer = If(Amount < 0, Negative, Positive)

And also look at the following code example:

Dim CustomerName As String = Nothing
Dim Value As String = If(CustomerName, "Not Set")

Actually the above code is equivalent to the following Code:

Dim CustomerName As String = Nothing
Dim Value As String = If(CustomerName IsNot Nothing, name, "Not Set")

Hope you get the idea of this powerful operator.

Good Luck !

Visual Basic 9 released !!!

Productivity that is ideal for first time or casual Windows programming. Get the new Version of Visual Basic from here. Following are the new features added in this Language I'll soon write a series of articles in which I'll try to explain each of these feature.

  • A true ternary operator If(boolean, value, value) to replace the IIF function.
  • Support for LINQ
  • Lambda expressions
  • XML Literals
  • Type Inference
  • Improved IntelliSense
  • Extension Methods
And many others....