Help, I m Lost


Help, I'm Lost!

So far in this chapter we've looked at debugging and tracing, and the sort of things you can do when you've got problems. However, one of the problems that comes from working with .NET is the breadth of what it provides. It's never easy to find things, and knowing where things are takes time. For example, let's consider you're writing an application that requires some math functions. You're hoping that Microsoft has included this sort of thing, but you don't know where these functions might be found if they've been included. You have two options:

  • Use the Web Matrix Class Browser

  • Use the documentation

We don't need to teach you how to use the documentation, as it's got great search features. Let's concentrate on the Class Browser, and then after that we'll tell you why you might need the documentation after all.

Try It Out —Using the Class Browser

  1. In Web Matrix, click on the Classes tab in the bottom right of the screen.

  2. In the text entry area above the list of classes, enter Math and press return. Notice how only one entry is found – that's the Math class.

  3. Double-click this to show the details for the class. Expand the Methods to see what
    it provides:

    click to expand

Where this comes in really useful is finding out the Namespace of a class. Namespaces are logical separations, allowing classes to be grouped together. The Math class is in the System namespace, so you don't have to do anything to use it. You could just do something like:

 Label1.Text = Math.Cos(Angle) 

There are, however, plenty of other classes that you can't use directly without telling ASP.NET that you're going to use them. For example, consider the case of accessing databases. You want to use some of the data classes, and you code your page. When running the page however, you get:

click to expand

Hmm, why can't it find the SqlConnection object? You know it exists. The reason is that SqlConnection is not in the default namespaces that ASP.NET automatically searches, so ASP.NET doesn't know where it is – you have to tell ASP.NET where to find it. However, you don't know where it is either, so you resort to the class browser:

click to expand

Aha. You can now see it's in the System.Data.SqlClient namespace, so you can add the reference accordingly:

 <%@ Import Namespace="System.Data.SqlClient" %> 

This is a special page directive, that you can add in the All tab. All it does is tell ASP.NET where to look for classes it can't find. You'll see this in action in Chapter 10.

An alternative to using Import is just using the full namespace for a type. For example:

 Dim conn As New System.Data.SqlClient.SqlConnection(" ... ") 

Here we put the full namespace in front of the type. It has just the same effect, but when declaring lots of types it becomes much harder to read. Using the Import statement is the preferred option.

Where the Class Browser Falls Down

The class browser is great, but what if you don't know the class name? How do you then search for things? This is where you have to resort to the documentation, not that there's anything wrong with that. In fact, .NET is one product where the documentation is essential, since there are so many classes, and so many properties and methods.

The documentation contains a complete reference of all namespaces, classes, methods, and properties, as well as sections on building applications, data access, debugging, and so on. There's also a whole host of samples with lots of code for you to examine.

The search features in the documentation also allow you to search for anything, not just a class name. It's really worth spending time getting to know your way around the documentation, as there's no way you'll be able to remember everything. Knowing where to go to find it is, I think, more important.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net