Monday, October 6, 2008

Protected, Friend and Protected Friend

[These Definitions may not be exact definitions but can give a brief
idea]
Protected --> Accessible ONLY by 1.Derived classes 2.Within
the same class
Or
1. Code within the class
2. Code in derived classes within the same assemblies
3. Code in derived classes in other assemblies

A Protected procedure may only be declared within a class.
Procedures declared within the same class can call it, as
can other methods in inherited classes.[From MSDN]

methods in inherited classes.
Protected variables can be used within the class as well as
the classes that inherites this class.

Friend --> Accessible ONLY by 1.Derived classes 2.Classes
in the same assembly 3.Within the same class
Or
1. Code within the class
2. Code in derived classes within the same assembly
3. Other code in the same assembly

Friends can be accessed by all classes within assembely but
not from outside the assembely.

A variable declared with the Friend access specifier can only
be seen by other classes within the same program. This means
that if a class has a Friend variable and that class resides
in a DLL, then only other classes within the same DLL can see
that Friend variable. Any other program that uses that DLL
cannot see that variable.[From MSDN]


Protected Friend --> Accessible ONLY by 1.Derived classes
2.Classes in the same assembly 3.Within the same class
Or
1. Code within the class
2. Code in derived classes within the same assembly
3. Other code in the same assembly
4. Code in derived classes in other assemblies

The Protected Friend can be accessed by Members of the
Assembely (Friend) or the inheriting Assembely class
(Protected).

Friday, September 26, 2008

Move/Rename File

 Dim fi As New FileInfo("myOldName.txt")
fi.MoveTo("myNewName.txt")

Calculate File Size

Imports System.IO

Private Function GetFileSize(ByVal MyFilePath As String) As Long
Dim MyFile As New FileInfo(MyFilePath)
Dim FileSize As Long = MyFile.Length
Return FileSize
End Function

DateTime Formats

d - Numeric day of the month without a leading zero.
dd - Numeric day of the month with a leading zero.
ddd - Abbreviated name of the day of the week.
dddd - Full name of the day of the week.

f,ff,fff,ffff,fffff,ffffff,fffffff - Fraction of a second. The more F's the higher the presision.

h - 12 Hour clock, no leading zero.
hh - 12 Hour clock with leading zero.
H - 24 Hour clock, no leading zero.
HH - 24 Hour clock with leading zero.

m - Minutes with no leading zero.
mm - Minutes with leading zero.

M - Numeric month with no leading zero.
MM - Numeric month with a leading zero.
MMM - Abbreviated name of month.
MMMM - Full month name.

s - Seconds with no leading zero.
ss - Seconds with leading zero.

t - AM/PM but only the first letter.
tt - AM/PM

y - Year with out century and leading zero.
yy - Year with out century, with leading zero.
yyyy - Year with century.

zz - Time zone off set with +/-.

String to DateTime conversion and Vice Versa

'String to DateTime

VB.Net
Dim dateString As String
dateString = "1999-09-01 21:34 PM"
Dim datetimeStamp As New DateTime
datetimeStamp = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm tt", Nothing)

C#
//
String to DateTime
String
MyString;
MyString = "1999-09-01 21:34 PM";
DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null);

VB.Net
'DateTime to String
datetimeStamp = New DateTime(1999, 9, 1, 21, 34, 0)
dateString = datetimeStamp.ToString("yyyy-MM-dd HH:mm tt")

C#
//
DateTime to String
MyDateTime = new DateTime(1999, 09, 01, 21, 34, 00);
String
MyString;
MyString = MyDateTime.ToString("yyyy-MM-dd HH:mm tt");

Thursday, August 21, 2008

VB.Net Usefull Code Snippets

From: http://www.dreamincode.net/code/snippet870.htm

Change bitmaps certain color transparency


  1. 'Written by Margus Martsepp AKA m2s87
  2. Private Function v2bista(ByRef OnPict As System.Drawing.Bitmap, _
  3. ByVal xColor As System.Drawing.Color, _
  4. ByVal How_Tr As Integer) As Boolean
  5. Dim x%, y%, R%, G%, B%
  6. v2bista = True
  7. With OnPict
  8. For x = 1 To .Width - 1
  9. For y = 1 To .Height - 1
  10. R = .GetPixel(x, y).R
  11. G = .GetPixel(x, y).G
  12. B = .GetPixel(x, y).B
  13. If xColor.R = R And xColor.G = G And xColor.B = B Then
  14. .SetPixel(x, y, System.Drawing.Color.FromArgb(How_Tr, R, G, B))
  15. End If
  16. Next
  17. Next
  18. End With
  19. End Function

Tuesday, July 1, 2008

Early vs. Late Binding

Early vs. Late Binding

One of the strengths of VB has long been that we had access to both early and late binding when interacting with objects.

Early binding means that our code directly interacts with the object – knowing its data type ahead of time and thus being able to very efficiently interact with the object. Early binding allows the IDE to use IntelliSense to aid our development efforts and it allows the compiler to ensure that we are referencing methods that do exist and that we are providing the proper parameter values.

Late binding means that our code interacts with an object dynamically at run-time. This provides a great deal of flexibility since our code literally doesn’t care what type of object it is interacting with as long as the object supports the methods we want to call. Because the type of the object isn’t known by the IDE or compiler, neither IntelliSense nor compile-time syntax checking is possible – but in exchange we get unprecedented flexibility.

VB.NET continues this tradition, providing support for both early and late binding as we work with
our objects.

By default, all objects are early bound. The IDE and compiler enforce this as long as Option Strict On is set, and this is the default. However, if we set Option Strict Off at the top of a source file (as discussed in Chapter 3), we open the door for late binding throughout the code in that file.

Use of the Object Type

Late binding occurs when the compiler can’t determine the type of object we’ll be calling. This level of ambiguity is achieved through the use of the Object data type. A variable of data type Object can hold virtually any value – including a reference to any type of object. Thus, code such as the following could be run against any object that implements a MyMethod method that accepts no parameters:

Option Strict Off
Module LateBind
Public Sub DoSomething(obj As Object)
obj.MyMethod()
End Sub
End Module

If the object passed into this routine doesn’t have a MyMethod method that accepts no parameters, then a run-time error will result. Thus, it is recommended that any code that uses late binding you should always provide error trapping:

Option Strict Off
Module LateBind
Public Sub DoSomething(obj As Object)
Try
obj.MyMethod()
Catch
‘ do something appropriate given failure to call the method
End Try
End Sub
End Module

While late binding is flexible, it can be error prone and it is slower than early bound code. To make a late bound method call, the .NET runtime must dynamically determine if the target object actually has a method that matches the one we’re calling, and then it must invoke that method on our behalf. This takes more time and effort than an early bound call where the compiler knows ahead of time that the method exists and can compile our code to make the call directly.

Late Binding and Reflection

The .NET Framework supports a concept known as reflection. This is the ability to write code that examines other .NET code to determine its composition. Reflection is supported by the System.Reflection namespace.

Reflection allows us to write code that discovers the classes within an assembly and the methods, properties and events exposed by those classes. We can then use reflection to create instances of those classes and call those methods. This entire process can be very dynamic – much like late binding.

In fact, VB.NET uses reflection to implement late binding on our behalf. Rather than forcing us to write the code that uses reflection to find and invoke a method, VB.NET handles this for us when we use late binding coding techniques.

We could implement a limited form of reflection within VB6 by using the typelib DLL. The functions in this DLL allowed us to dynamically discover the classes and methods in a COM DLL, and then invoke them. Of course COM components were described with IDL – a rather inaccurate description of the component. In .NET, assemblies are described by metadata that accurately describes each assembly, making reflection a much more robust solution.