PDA

Click to See Complete Forum and Search --> : Should we compile a FAQ?


Cander
Feb 25th, 2002, 10:57 AM
With the popularity on the rise for .NET, do you guys think we should start compiling a FAQ that one of the mods could keep at the top of the forum?

Things that should be in there:

API
File I/O. I see this as a big one since it is completely changed.

Bananafish
Feb 26th, 2002, 04:50 AM
Well I think it's a good idea.

As most vb.NET users will previously have used vb6, I can forsee a lot of questions like - "I knew how to do it in VB6 - but how is it done [properly :) ] in .NET...

Anyone using .NET has at some point had to find things out - it makes sense to try and share that information with new users.

BrianHawley
Feb 26th, 2002, 07:18 AM
YES!!

ender_pete
Feb 26th, 2002, 08:26 AM
that is a great idea
ill contribute anything that would be helpfull.

Cander
Feb 26th, 2002, 08:30 AM
Here is some uestions ive thought of and I will start working on answers for them soon. If you have anything to add, just reply.

What is .NET?
How do I use API calls in .NET?
How do I compile?
OK I compiled my app in Visual Studio by selecting Build. Where is it?
Namespaces? Say what?
Whoa, no Open statement? How do I read and write a text file?
Can I use a COM component in my VB .NET app?
No control arrays? I need control arrays, what do I do?
Catch..Try...Finally? Whazzup wit dat?

kleptos
Feb 26th, 2002, 08:34 AM
Great! Awsome Idea... What can i do to help?

ender_pete
Feb 26th, 2002, 08:43 AM
thoise are some good ones to start with.. then there can be an a section for OOP.

since any programmer who has always done vb has never gotten the full OOP advantage.

Inheritance
Implements
Overrides
Overloads
Polymorphism
Encapsulation
Everything is an object in vb.NET, so what do i do with them?

that kind of stuff

Cander
Feb 26th, 2002, 08:44 AM
Well if you can think of any questions that you think would be asked alot, add it to this post.

kleptos
Feb 26th, 2002, 09:21 AM
Sockets! As i flip thru many VB sites, i see winsock apps out the wazoo, .NET = No Winsock (i know, i was devistated). So maybe something about that? I dunno if thats too general for the FAQ.

Cander
Feb 26th, 2002, 09:26 AM
no i think a uick explanation would be good for the socket classes as Winsock questions are very common and we can expect the same for .NET

Cander
Feb 26th, 2002, 09:31 AM
Here is what I have for the API question. Anyone see anything I need to add or made a mistake on?

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.

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.

Bananafish
Feb 26th, 2002, 09:59 AM
Sounds good to me Cander.

Does this mean you are volunteering to be the FAQ Controller?

Cander
Feb 26th, 2002, 10:11 AM
I guess so. :D

Here is an update of what I have gotten so far.
Let me know if you see any mistakes guys. Ill keep working on it. Please reply with any other FAQ worthy questions. If you can provide a good answer for it please do, if not I or soemone else will find it.


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?

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.

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 wya 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:

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.

OK I compiled my app in Visual Studio .NET by selecting Build. Where is it?
Check the \bin\ directory in your projects directory and you will find your compiled
assmebly.

Namespaces? Say what?

Whoa, no Open statement? How do I read and write a text file?

Can I use a COM component in my VB .NET app?

No control arrays? I need control arrays, what do I do?

Catch..Try...Finally? Whazzup wit dat?

.NET Resources:
http://www.gotdotnet.com
http://www.asp.net
http://www.dotnet101.com/
Need a great editor and cannot afford Visual Studio .NET? Go here: http://www.icsharpcode.net/OpenSource/SD/default.asp

Cander
Feb 26th, 2002, 12:46 PM
ok here is another update. Check for any errors more coming as I get to it. Once again, any additions feel free to post the question and answer if you have the answer


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 moer 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.

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.

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 wya 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:

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.

OK I compiled my app in Visual Studio .NET by selecting Build. Where is it?
Check the \bin\ directory in your 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:


a = New System.Threading.Thread(blah)
b = New System.Threading.Thread(blah2)


you can shorten this using Imports


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


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?

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.

No control arrays? I need control arrays, what do I do?

Catch..Try...Finally? Whazzup wit dat?

ASP .NET? What is so great about it over ASP 3.0?[/B}

[B].NET Resources:
http://www.gotdotnet.com
http://www.asp.net
http://www.dotnet101.com/
Need a great editor and cannot afford Visual Studio .NET? Go here: http://www.icsharpcode.net/OpenSource/SD/default.asp

kleptos
Feb 26th, 2002, 12:59 PM
Oooh, very nice, I like your style of writing, clear, concise and to the point... Keep this up... Its very well done.

Jop
Feb 27th, 2002, 03:10 PM
Sweet idea to begin a FAQ!
And I'm willing to contribute... I want to change the Topic Title to "File Access in VB.NET" if you're ok with that ofcourse :)

File Access in VB.NET
You might be wondering why the Open statement doesn't work in VB.NET, and you're wondering how to handle files? You came at the right spot!
The thing is, the File functions are given a more logical name, for example the Open statement is changed to FileOpen, and yes, Close has been changed to FileClose... easy right?

What to expect in this small tutorial?

A short introduction to the different file modes in VB.NET
Sequential mode
Random mode
Binary mode
FreeFile
Other File Functions
The System.IO Namespace



Short Introduction
First of all, let me explain what types of file modes exist in VB.NET, well actually the same as in VB6:

Sequential access (Input, Output, and Append modes) is used for writing text files, such as error logs and reports.
Random access (Random mode) is used to read and write data to a file without closing it. Random-access files keep data in records, which makes it easy to locate information quickly.
Binary access (Binary mode) is used to read or write to any byte position in a file, such as storing or displaying a bitmap image.



Sequential Files
Here's an example on how to Open a textfile and write to it, and later read the textfile in and show the contents of the file in a MessageBox


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sTxtFile As String = "c:\test.txt"
Dim sTmp As String

FileOpen(1, sTxtFile, OpenMode.Output)
Print(1, "This is a file test!")
FileClose(1)

FileOpen(1, sTxtFile, OpenMode.Input)
Input(1, sTmp)
FileClose(1)

MessageBox.Show(sTmp) 'Returns "This is a file test!"
End Sub


So essentialy it's quite the same as in VB6 and shouldn't give too much trouble finding out what all the parameters mean.

Also the LineInput statement still works as expected:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sTxtFile As String = "c:\test.txt"
Dim sTmp As String

FileOpen(1, sTxtFile, OpenMode.Input)
Do While Not EOF(1)
MessageBox.Show(LineInput(1))
Loop
FileClose(1)
End Sub



Random Access
Random mode can be quite handy to handle structures in files for example.
In this example I used a Structure that represents a Member of the Vbworldforums, how they would be stored in a file using Random Access.
You might notice the <VBFixedString(15)>, this is how the current VB.NET handles Fixed strings, but I believe that is subject to change, and this tutorial will be updated as neccesary.


Structure VbWorldMember
Public lNumberOfPosts As Long
<VBFixedString(15)> Public sFirstName As String 'This sample won't work if the name is longer than 15, change as needed...
<VBFixedString(15)> Public sLastName As String
<VBFixedString(15)> Public sNickname As String
<VBFixedString(15)> Public sRank As String
End Structure


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sTxtFile As String = "c:\testrandom.dat"
Dim sTmp As String

Dim JohnDoe As VbWorldMember 'Declare a new VbWorldMember called JohnDoe
JohnDoe.lNumberOfPosts = 4
JohnDoe.sFirstName = "John"
JohnDoe.sLastName = "Doe"
JohnDoe.sNickname = "JD"
JohnDoe.sRank = "Newbie"

FileOpen(1, sTxtFile, OpenMode.Random, , , Len(JohnDoe))
FilePut(1, JohnDoe, 1) 'Put the JohnDoe Structure in the first record of the file
FileClose(1)

Dim dummyJohnDoe As VbWorldMember 'Make a dummy JohnDoe to test if we can get the old JohnDoe
FileOpen(1, sTxtFile, OpenMode.Random)
FileGet(1, dummyJohnDoe, 1) 'Get the first record of the file in the NewJohnDoe structure
FileClose(1)

'Show the number of posts for the dummyJohnDoe which we imported from the Random File.
MessageBox.Show(dummyJohnDoe.lNumberOfPosts) 'Returns "4"
End Sub



Binary Access
Binary Access is used for handling files at byte-level, this is used for example Bmp files, but can you be used for text also.
It also can be used as in the Random Access, but this file mode doesn't require fixed-length strings, so keeps the filesize smaller than with Random Mode.

This is an example how to read write text to a file using binary access.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sTxtFile As String = "c:\testbinary.dat"

FileOpen(1, sTxtFile, OpenMode.Binary)
FilePut(1, "Binary file test!")
FileClose(1)

FileOpen(1, sTxtFile, OpenMode.Binary)
Dim sTmp As New String(" ", LOF(1)) 'Make the buffer as large as the file to make sure the contents of the file fit in the buffer.
FileGet(1, sTmp)
FileClose(1)

MessageBox.Show(sTmp) 'returns "Binary file test!"
End Sub


This is an example if you want to get, for example, only the first 3 bytes of a file.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sTxtFile As String = "c:\testbinary.dat"

FileOpen(1, sTxtFile, OpenMode.Binary)
FilePut(1, "Binary file test!")
FileClose(1)

FileOpen(1, sTxtFile, OpenMode.Binary)
Dim sTmp As New String(" ", 3) 'Make the 3 bytes long, so we'll only get 3 bytes...
FileGet(1, sTmp) 'with the Byte Number omitted we'll start reading from the beginning
FileClose(1)

MessageBox.Show(sTmp) 'returns "Bin"
End Sub


Now what if you want only byte 4 to 6?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sTxtFile As String = "c:\testbinary.dat"

FileOpen(1, sTxtFile, OpenMode.Binary)
FilePut(1, "Binary file test!")
FileClose(1)

FileOpen(1, sTxtFile, OpenMode.Binary)
Dim sTmp As New String(" ", 3) 'Make the 3 bytes long, so we'll only get 3 bytes...
FileGet(1, sTmp, 4) 'By setting the Byte number to 4, we'll start from the 4th byte instead of the 1st.
FileClose(1)

MessageBox.Show(sTmp) 'Returns "ary"
End Sub



FreeFile
The use of FreeFile still is the same as in VB6:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sTxtFile As String = "e:\jop\test.txt"
Dim iFF As Integer = FreeFile()
Dim sTmp As String

FileOpen(iFF, sTxtFile, OpenMode.Input)
Input(iFF, sTmp)
FileClose(iFF)

MessageBox.Show(sTmp)
End Sub



Other File Functions
All of the old functions still exist in VB.NET:

Dir
EOF
FileCopy
FileDateTime
FileLen
FreeFile
GetAttr
Loc
LOF
Seek
SetAttr


What? Where is the Move Function!!!
(jeez I was wondering that too!)

There is a function called: Rename, which actually works the same like the old Move function:

Rename("c:\oldfile.txt", "c:\newfile.txt")

But I found out that it returns an error when you try to change the extension of the file;

Rename("c:\oldfile.txt", "c:\newfile.html") 'Returns an error!

So what to do? Well I'll discuss that in the next topic.

The System.IO Namespace
The System.IO contains loads of classes that will make solving your Input/Output problems a lot easier!
In the example above we discussed the use of Rename, it won't let us rename the file extension, here is where the System.IO comes into action!

'To import the System.IO namespace:
Imports System.IO

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'The following code will is actually in the System.IO namespace:
File.Move("c:\oldfile.txt", "c:\newfile.html") 'will now work!!!
End Sub


There are a lot more functions in the System.IO.File namespace that you'll want to explore, check MSDN and look on the Internet for more examples!

Cander
Feb 27th, 2002, 03:31 PM
I actually had it written(refer to the my other post), but I like yours better. Ill replace it.

kovan
Feb 27th, 2002, 03:37 PM
i DEMAND c-sharpcorner.com to be on top of the resource list
its by FAR superior to any other site i have seen on .NET
they have a pretty good secion on vb.net as well