|
-
Nov 1st, 2002, 01:56 PM
#1
Hey. look what I found.(.NET FAQ)
for you guys that have been in this forum since the beginning, you may remember a FAQ I had posted. Well after doing some cleanup, I found it on my harddrive. thought I would post it again for the benefit of the newbs.
The VbWorld .NET FAQ was written and is maintained by the posters of the .NET forum. It concentrates
most answers on VB .NET.
What is .NET?
.NET is Microsoft's new initiative to allow applications to run over the internet via Web
Services. That is the common definition, but to developers it is so much more. .NET is a framework
much like Java's Virtual Machine. Instead of code you write running on the Operating System, it
runs ontop of the framework (Common Language Runtime). Because of this, this leaves open the
possibility of .NET being ported to non-Windows platforms and you apps running on them. In
addition, the framework provides mnay powerful classes that makes programmers lives much easier.
A major big advantage of .NET over Java is that you are not locked into one language. You have a
choice of many with more on the way. Currently it supports VB, C#, C++, Python, Perl, COBOL, and
many more through add-ons provide by the company porting the language. See the resource links
at the bottom of this FAQ for links to begin learning more about .NET. You will be very pleased
with the improvements and power .NET provides.
What do I need to start developing for .NET
All you need is the .NET framework SDK. It is a free download from Microsoft. You do not require
Visual Studio .NET as the SDK provides the compilers. Just use any text editor. Check the
Resource links at the end of the FAQ for a link to a great .NET code editor and framework
download links.
It is also required that the users of your applications has the .NET framework isntalled(not the
SDK) it is 21 meg download but smaller than the much bigger 130 meg SDK.
How do I use API calls in .NET?
There seems to be the big myth running around that you cannot use API calls in .NET applications.
This is false. You can use API functions in your .NET apps pretty much the same way you always
have before, BUT, there are some rules you need to know first. First off, the .NET framework
already provides classes to many functions that you used to use the API for previously. So before
you use a call, make sure there is no .NET way to do it as adding API calls makes your app
unmanaged by the framework and it loses some of the benfits(such as security) of it. You can
check http://www.allapi.net for a list of functions that are provided by the framework to replace
using the API. NOTE: As of this time they have not included anything in the list, but bookmark
it for later reference when they do.
If you do end up having to use and API call, here is what is different.
a) When calling a function, you can specify if it will return Ansi or Unicode. VB6 by default
returned Ansi.
VB Code:
Public Declare Ansi functionname .....
b) You will need to change return value and parameter datatypes. Dataypes have changed, so you
will need to change them as appropriate. For instance:
If it was Long in VB6, change it to Integer
If it was Integer, change it to Short
If it was Any, you will need to find out what the correct data type will be.
How do I compile?
You are provided with more than one way to compile your .NET apps.
a) You can use the command line compiler's. For VB just type vbc.exe at the command line
EX:
Code:
vbc.exe myApp.vb /t:exe
That will compile the myApp.vb file to an executable named myApp.exe
b) In Visual Studio .NET, right click on the project name in the Project Explorer and select
Build or Rebuild.
c) There are even classes provided by the .NET framework that will allow you to compile an
application from your own program. But we will not get into this.
The command line compiler is great as fledgling programmers wont need to dish out mucho cash for
Visual Studio .NET(or search Morpheus and download 3 gigs :P).
OK I compiled my app in Visual Studio .NET by selecting Build. Where is it?
Check the \bin\ directory in the projects directory and you will find your compiled
assmebly.
Namespaces? Say what?
Namepaces provide a sructured way to organize and access code. This can help in readability and
decrease the problems of class naming conflicts.
All classes built into the .NET framework uses Namespaces and you may have seen them used. For
instance there is the System Namespace. Within this one resides other Namespaces and classes
that you can use to access certain functions like:
System.Data - This Namespace set gives access to the Database and XML classes for reading and
writing.
You may have also seen them used with the Imports statement. Imports allows you to predefine a
Namespace so you only need to access the inner levels of it. For example:
Instead having to perhaps type this multiple times:
VB Code:
a = New System.Threading.Thread(blah)
b = New System.Threading.Thread(blah2)
you can shorten this using Imports
VB Code:
Imports System.Threading
a = New Thread(blah)
b = New Thread(blah2)
You can also easily create your own Namespaces in your own apps to sort multiple classes using
the Namespace declaration statement
VB Code:
Namespace mySpace1
Class myClass1
' Class code
End Class
Class myClass2
' Class code
End Class
End Namespace
You will see alot of Namespaces as you learn and use Namespaces, so it is important you
understand them.
Whoa, no Open statement? How do I read and write a text file?
I have not seen anyone ask this yet, but I know that File writing and reading is big time asked
question for VB6 so since it has changed dramatically, I think it is appropriate to include some
information on this. There really is not a whole lot to explain so we will just go straight to the
code for reading and writing a text file.
Writing:
VB Code:
Imports System
Imports System.IO
dim myFileStream As FileStream
Try
myFileStream = New FileStream("C:\mytextfile.txt", FileMode.OpenOrCreate, FileAccess.Write)
myFileStream.Write("Some text")
Finally
If Not (myFileStream Is Nothing) Then myFileStream.Close()
End Try
Reading:
VB Code:
Imports System
Imports System.IO
' declare the StreamReader, for accessing the file
Dim strWriter As StringWriter = New StringWriter()
Dim TextFile As String = "C:\textfile.txt"
Console.SetOut(strWriter)
Try
Dim din As StreamReader= File.OpenText(TextFile)
Dim str As String
Dim al As ArrayList = New ArrayList()
Console.WriteLine("Reading data and inserting into an ArrayList...")
Console.WriteLine()
Do
str = din.ReadLine()
If str <> Nothing Then
al.Add(str)
End If
Loop Until str = Nothing
Console.WriteLine("Printing out the ArrayList.")
Console.WriteLine("---------------------------")
Dim s As String
For Each s in al
Console.WriteLine (s)
Next s
Catch E As Exception
Console.WriteLine("There was an error reading the file . Please ensure it is in the right
directory")
End Try
The FileStream classes provide functionality for writing bytes, binary files, and goes back to using
Seek for Random Access.
Can I use a COM component in my VB .NET app?
Yes you can, although keep in mind that doing so has the same downsides as using the API as your
application becomes unmanaged.
To add a COM component, just right click in the Project Explorer in Visual Studio .NET and select
Add Reference. The goto the COM tab and find your component. What happens behind the scenes is
that VS .NET creates a wrapper that makes that component usable in .NET.
But this does add much overhead to your app, so please make sure before using a COM component
that there is no .NET class/object to do what you need.
I heared that dynamic arrays are gone? Is that true?
Absolutly not. Dim and Redim arrays as you always hve before. It is quite possible those that say
this maybe confused with dynamic control arrays as control arrays ARE gone.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|