Monday, August 28, 2006

Notes on VB.NET

1. System.object is the base class for all data types.
2. Reference types derive directly from system.object
3. value types derive from system.valuetype which in turn derives from system.object
4. system.boolean takes up 1 byte
5. system.char takes up 2 bytes
6. Double takes up 8 bytes
7. system.string is a reference type
8. All reference types or objects are managed by the GC on the GC heap.
9. If you attempt to perform an operation against a null, you'll ordinarily receive a
NullReferenceException in response
10. value types can store on the execution stack or on the heap managed by the GC
11. value types cannot have parameterless constructors
12. use sizeof(T) to find size of a value type
13. datetime is a value type
14. private is the default visibility type for MEMBERS
15. assemblty is the default visibility for TYPES
16. shared members can only access other shared members belonging to the same type and not to any instance members. Not true vice versa.
17. by default int x is 4 bytes
18. constant fields are shared by default since all instances of the class have the same value.
19, shared methods have no me pointer
20. byval is default
21. all methods in vb.net are virtual by default.
22. A virtual method is one which can be overrided
22. methods must declare them to be overridable (not default) to allow base classes to change their implementation
22. shadow variable is a local variable within a function with the same name as a member variable of the type class
23. system.gc.collect() manually triggers garbage collection
24. The Exception is the ultimate base class for any exceptions in VB.NET
25. Environment.Systemdirectory is used to return a string to the windows folder
26. Encoding.ASCII.GetBytes("hi rochak") to convert string to a bytearray.
27. closing the stremwriter causes the data to be flushed, but doing so will also close the underlying stream

Saturday, August 26, 2006

Generics

Excellent article on 15seconds on generics... the new feature in .NET 2.0 Must read.

http://www.15seconds.com/issue/040525.htm




Events And Delegates

Probably one of the best sites

http://abstractvb.com/code.asp?A=1084



Monday, August 21, 2006

Google / MSDN Search within Visual Studio 2005#



Text Copied from http://www.r2musings.com/GoogleMSDNSearchWithinVisualStudio2005.aspx


Lately, I've been deep-diving into some custom control creation in VS2005 and I'm finding that I'm spending ALOT of time on Google and MSDN2.  So, I got really tired of opening the browser and surfing to Google and MSDN2 over and over and searching for things. Then I remembered someone sending me a link sometime last year where someone had created an Add-In that would perform a Google search from within Visual Studio. Well, of course, I've long since lost that link. So, I opened a browser and surfed on over to Google (where else?) and searched for "Visual Studio Add-in Google" and came across lots of links. I decided to click on the Macro for Google Search in VS.NET link as it was on Code Project and I've gotten a lot of great information from there over the years.



I have never really bothered to use a Macro in VS, but thought I’d give this one a try. Right away, I got an error when I ran the macro and decided to attempt to debug through it to see if I could spot the problem. Turns out, it was failing on the following: DTE.ExecuteCommand("View.URL",strUrl).  The original macro was posted to Code Project back in 2003, so I guessed that the View.URL may have been replaced in VS2005.  I tried replacing the call with this: DTE:ExecuteCommand("View.WebBrowser", url) and that seemed to do the trick....sort of.  It definitely fired the search in the VS2005 IDE, but it choked any time I highlighted more than one word.



I would have to say that about 99% of the time that I search for ANYTHING, it involves more than one word….so if this couldn’t be resolved, then this macro was pretty much useless to me.   Anyway, with a little help from my old friend, RegEx, I now have it working exactly like I want.  I have assigned a Keyboard shortcut to it and I can now highlight anything in the VS2005 IDE and press Ctrl-F1 (my custom shortcut) and I have the results for a Google Search WITHIN my IDE.   Ahhh…



Since that went so well, I decided to adapt it to MSDN2.  (Of course, I’m assuming that I’ll have to update the URL in the MSDN2 macro someday soon as Microsoft moves the Library for .NET 2.0 into the standard MSDN library).  However, it will be nothing more than a quick change to the URL in the macro and it should be good to go.



This is very adaptable to any search engine or site that you want.  Just go there and search and see if you can figure out how they are using the QueryString for their searches and create a macro for it in VS2005.



For those not familiar with macros in VS2005, here are the steps to add a macro:



1)      Go to Tools | Macros | Macros IDE

2)      Once in the Macros IDE, you can create a new Module or you can just add this to the default “My Macros” module.

3)      To add to My Macros, just right click on it in the Project Explorer and choose Add | Add New Item.  (if you don’t see the Project Explorer, try View | Project Explorer).

4)      When the Add New Item Dialog shows up, select Code File and enter GoogleSearch and click OK.

5)      Paste the Code for GoogleSearch below into this file and save it.

6)      Click Debug | Build and then close the Macros IDE.

7)      Once back in VS2005, click Tools | Customize and click the Keyboard button at the bottom of the Customize Dialog.

8)      In the Show Commands Containing box, type “macros” and look for an entry that looks like this:  “Macros.MyMacros.GoogleSearch.GoogleSearch”.  (This could be different depending on where you created the macro).

9)      Once you have the correct macro selected, go to the Press Shortcut Keys box and type whatever keys you want to fire the macro (I used Ctrl-F1).

10)  Click the Assign button and then OK and then finally Close back on the Customize Dialog.





Here is the final code for the macro:



Imports EnvDTE

Imports System.Text.RegularExpressions



Public Module GoogleSearch



    Sub GoogleSearch()

        Dim url As String

        Dim selectedText As TextSelection = DTE.ActiveDocument.Selection()



        If Not String.IsNullOrEmpty(selectedText.Text) Then

            url = String.Format("www.google.com/search?q={0}", Regex.Replace(selectedText.Text, "\s{1,}", "+"))

           

            ' for MSDN2 search, just comment out the above url line and uncomment this one

            ' url = String.Format("http://lab.msdn.microsoft.com/searchbeta/Default.aspx?query={0}", Regex.Replace(selectedText.Text, "\s{1,}", "+"))



            DTE.ExecuteCommand("View.WebBrowser", url)

        Else

            MsgBox("No text selected.")

        End If

    End Sub



End Module

Saturday, August 12, 2006

Handling Page Events


Whenever you request an ASP.NET page, a particular set of events is raised in a particular sequence. This sequence of events is called the page execution lifecycle.

For example, we have already used the Page Load event in previous code samples in this chapter. You normally use the Page Load event to initialize the properties of controls contained in a page. However, the Page Load event is only one event supported by the Page class.

Here is the sequence of events that are raised whenever you request a page:

  1. PreInit

  2. Init

  3. InitComplete

  4. PreLoad

  5. Load

  6. LoadComplete

  7. PreRender

  8. PreRenderComplete

  9. SaveStateComplete

  10. Unload

Why so many events? Different things happen and different information is available at different stages in the page execution lifecycle.

For example, View State is not loaded until after the InitComplete event. Data posted to the server from a form control, such as a TextBox control, is also not available until after this event.

Ninety-nine percent of the time, you won't handle any of these events except for the Load and the PreRender events. The difference between these two events is that the Load event happens before any control events and the PreRender event happens after any control events.

Understanding Dynamic Compilation

Strangely enough, when you create an ASP.NET page, you are actually creating the source code for a .NET class. You are creating a new instance of the System.Web.UI.Page class. The entire contents of an ASP.NET page, including all script and HTML content, are compiled into a .NET class.

When you request an ASP.NET page, the ASP.NET Framework checks for a .NET class that corresponds to the page. If a corresponding class does not exist, the Framework automatically compiles the page into a new class and stores the compiled class (the assembly) in the Temporary ASP.NET Files folder located at the following path:

\WINDOWS\Microsoft.NET\Framework\[version]\Temporary ASP.NET Files

The next time anyone requests the same page in the future, the page is not compiled again. The previously compiled class is executed and the results are returned to the browser.

Even if you unplug your web server, move to Borneo for 3 years, and start up your web server again, the next time someone requests the same page, the page does not need to be re-compiled. The compiled class is preserved in the Temporary ASP.NET Files folder until the source code for your application is modified.

When the class is added to the Temporary ASP.NET Files folder, a file dependency is created between the class and the original ASP.NET page. If the ASP.NET page is modified in any way, the corresponding .NET class is automatically deleted. The next time someone requests the page, the Framework automatically compiles the modified page source into a new .NET class.

This process is called dynamic compilation. Dynamic compilation enables ASP.NET applications to support thousands of simultaneous users. Unlike an ASP Classic page, for example, an ASP.NET page does not need to be parsed and compiled each and every time it is requested. An ASP.NET page is compiled only when an application is modified.

Note

You can precompile an entire ASP.NET application by using the aspnet_compiler.exe command-line tool. If you precompile an application, users don't experience the compilation delay resulting from the first page request.

Control State

The ASP.NET Framework version 2.0 includes a new feature called Control State. Control State is similar to View State except that it is used to preserve only critical state information. For example, the GridView control uses Control State to store the selected row. Even if you disable View State, the GridView control remembers which row is selected.

ASP.NET Controls

An ASP.net control is a class that executes on the server and renders the content on the browser.

When the TextBox control is rendered to the browser, it renders the following content:

<input name="TextBox1" type="text" id="TextBox1" />

The first part of the control declaration, the asp: prefix, indicates the namespace for the control. All the standard ASP.NET controls are contained in the System.Web.UI.WebControls namespace. The prefix asp: represents this namespace.

Friday, August 11, 2006

Hyperlink Control - does not post back to server

The HyperLink control enables you to create a link to a page. Unlike the LinkButton control, the HyperLink control does not submit a form to a server.

Understanding Dynamic Compilation


Strangely enough, when you create an ASP.NET page, you are actually creating the source code for a .NET class. You are creating a new instance of the System.Web.UI.Page class. The entire contents of an ASP.NET page, including all script and HTML content, are compiled into a .NET class.

When you request an ASP.NET page, the ASP.NET Framework checks for a .NET class that corresponds to the page. If a corresponding class does not exist, the Framework automatically compiles the page into a new class and stores the compiled class (the assembly) in the Temporary ASP.NET Files folder located at the following path:

\WINDOWS\Microsoft.NET\Framework\[version]\Temporary ASP.NET Files

The next time anyone requests the same page in the future, the page is not compiled again. The previously compiled class is executed and the results are returned to the browser.

Even if you unplug your web server, move to Borneo for 3 years, and start up your web server again, the next time someone requests the same page, the page does not need to be re-compiled. The compiled class is preserved in the Temporary ASP.NET Files folder until the source code for your application is modified.

When the class is added to the Temporary ASP.NET Files folder, a file dependency is created between the class and the original ASP.NET page. If the ASP.NET page is modified in any way, the corresponding .NET class is automatically deleted. The next time someone requests the page, the Framework automatically compiles the modified page source into a new .NET class.

This process is called dynamic compilation. Dynamic compilation enables ASP.NET applications to support thousands of simultaneous users. Unlike an ASP Classic page, for example, an ASP.NET page does not need to be parsed and compiled each and every time it is requested. An ASP.NET page is compiled only when an application is modified.

Note

You can precompile an entire ASP.NET application by using the aspnet_compiler.exe command-line tool. If you precompile an application, users don't experience the compilation delay resulting from the first page request.

Control State

The ASP.NET Framework version 2.0 includes a new feature called Control State. Control State is similar to View State except that it is used to preserve only critical state information. For example, the GridView control uses Control State to store the selected row. Even if you disable View State, the GridView control remembers which row is selected.

Thursday, August 10, 2006

Understanding Assemblies


An assembly is the actual .dll file on your hard drive where the classes in the .NET Framework are stored. For example, all the classes contained in the ASP.NET Framework are located in an assembly named System.Web.dll.

More accurately, an assembly is the primary unit of deployment, security, and version control in the .NET Framework. Because an assembly can span multiple files, an assembly is often referred to as a "logical" dll.

Note

The .NET Framework (version 2.0) includes 51 assemblies.



There are two types of assemblies: private and shared. A private assembly can be used by only a single application. A shared assembly, on the other hand, can be used by all applications located on the same server.

Shared assemblies are located in the Global Assembly Cache (GAC). For example, the System.Web.dll assembly and all the other assemblies included with the .NET Framework are located in the Global Assembly Cache.

Note

The Global Assembly Cache is located physically in your computer's \WINDOWS\Assembly folder. There is a separate copy of every assembly in your \WINDOWS\Microsoft.NET\Framework\v2.0.50727 folder. The first set of assemblies is used at runtime and the second set is used at compile time.



Before you can use a class contained in an assembly in your application, you must add a reference to the assembly. By default, an ASP.NET application references the most common assemblies contained in the Global Assembly Cache:

mscorlib.dll

System.dll

System.Configuration.dll

System.Web.dll

System.Data.dll

System.Web.Services.dll

System.Xml.dll

System.Drawing.dll

System.EnterpriseServices.dll

System.Web.Mobile.dll

To use any particular class in the .NET Framework, you must do two things. First, your application must reference the assembly that contains the class. Second, your application must import the namespace associated with the class.

In most cases, you won't worry about referencing the necessary assembly because the most common assemblies are referenced automatically. However, if you need to use a specialized assembly, you need to add a reference explicitly to the assembly. For example, if you need to interact with Active Directory by using the classes in the System.DirectoryServices namespace then you will need to add a reference to the System.DirectoryServices.dll assembly to your application.

Each class entry in the .NET Framework SDK documentation lists the assembly and namespace associated with the class. For example, if you look up the MessageQueue class in the documentation, you'll discover that this class is located in the System.Messaging namespace located in the System.Messaging.dll assembly.

Adding namespace in web.config

<?xml version="1.0"?>
<configuration>
    <system.web>
      <pages>
        <namespaces>
          <add namespace="System.Net.Mail"/>
        </namespaces>
      </pages>
    </system.web>
</configuration>



Sending email in asp.net 2.0

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    Sub Page_Load()
        Dim client As New SmtpClient()
        client.Host = "localhost"
        client.Port = 25
        client.Send("steve@somewhere", "bob@somewhere.com", _
            "Let's eat lunch!", "Lunch at the Steak House?")
    End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Send Mail</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    Email sent!

    </div>
    </form>
</body>
</html>