Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Creation

Status
Not open for further replies.

rlusk49

Technical User
Nov 7, 2002
18
CA
Simple question. With the following code, what do I have to dim MyFile as to get my IntelliSense to work. Do I dim it as an Object?

Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
 
are you in VB or VBA?

why are you late binding?

if you want intellisence and faster code then

Dim fso As Scripting.FileSystemObject
Dim MyFile As Scripting.TextStream
Set fso = New Scripting.FileSystemObject
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close


I am guessing you just copied this code from somewhere.
 
I'm in VB6.

I guess I am not at your level because I'm not sure what late binding is. I did copy the code from somewhere.

But I still get no intellisense with MyFile with your code.
 
ok, last thing you need to do is under
project menu select references
and scroll down until you see microsoft scripting runtime

the long name for the scripting library

that allows you to early bind your variables.

Early binding not only gives you intellisence but allows vb to use V-Table binding. This basically means that VB knows the exact address in memory of the methods you are using. If you late bind, use CreateObject(), then VB doesn't do this but uses a interface that you don't see in vb for every call and it takes 2 actual calls to do the one method. The first "GetIDsOfNames" and passes it the name of the method you want to use, in this case lets say "CreateTextFile". This basically returns a number then VB will call "Invoke" passing this number with all the other parameters and the COM object will execute the code. Needless to say the 2 calls in late binding are slower than the single call in Early or V-Table binding.

 
rlusk -

Here's a hint that will save you much angst:
In the VB options, check the checkbox that says "Require variable declaration". Afterwards every new code module and form that you create will have "Option Explicit" at the top. Be sure to manually add it to any existing forms.

Without it, you can do this:
Code:
Private Sub MySub
  A = 23
  A = S + 13
End Sub
Q: What is the value of A at the end of this sub?
A: It's not 36. It's 13 because you mis-typed the variable name (S instead of A), and S is unDimmed, so the second line becomes A = 0 + 13, or not what you expected! With Option Explicit, the compiler will flag this error for you, and report an undeclared variable.

Chip H.
 
P.S. Don't use the fso you need a dll for that if you just want to write to a flat file use the "open" command

ex.

Public Function x() As Boolean
x = False
Open "c:\mynewfileimcreating.txt" For Output As #1
Print #1, "Mytextgoeshere"
Close #1
x = True
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top