PDA

View Full Version : دستورات CreatObject و ... به چه معنا و چه فرقی با هم دارند؟



منصور بزرگمهر
چهارشنبه 04 تیر 1382, 04:57 صبح
دستورات CreatObject و ... به چه معنا و چه فرقی با هم دارند؟

اینجانب تا بحال چندین ActivX شخصاً ساخته ام، ولی طبق نظر کتابهای مذکور از آنها با دستور
Dim NameVar as New NameClass
استفاده نموده ام، ولی بعضی از اکتیو ایکس ها را می توان با دستور CreatObject لود نمود. سئوال بنده این می باشد،‌که فرق دو دستور فوق چیست. و ایا دستور CreatObject معادل معرفی نمودن ActivX در Refrnce خود وی بی می باشد، و یا چیز دیگری می باشد.

موضوع دیگری که نیز وجود دارد این می باشد،(و مساله را قدری پیچیده تر کرده است) که ما شی را که در Refrence خود وی بی معرفی کرده ایم، حال برای استفاده از ان باید دوباره دستور زیر را بکار بریم،

فرضاً Dim Cnn as New ADOBE.CONNECTION
(شی اتصال ADO)

مورد بالا برای چه بکار دوباره رفته است، و چرا تعریف رفرنس را نمی توانیم استفاده کنیم.

حال به مثال زیر توجه کنید :

(Set FS = CreateObject("Scripting.FileSystemObject"
ما در بالا یکباره اکتیوایکس را باز و دیگر نیاز به دستور نیو نمی باشد. علت تفاوت در چیست؟

لطفاً هر چه در مورد موارد بالا یا هرچه که خود مناسب می دانید، توضیح داده و اینجانب را از سرگردانی نجات دهید.
متشکرم

MBeigy
پنج شنبه 05 تیر 1382, 07:19 صبح
سلام دوست عزیز
حقیقتش حال نداشتم ترجمه کل اینها رو که در پایین اومده بنویسم. میدونم اینطوری زیاد خوانا نیست ولی ببخشید. در ضمن اگه دقت کنی اون دستور Dim NameVar as New NameClass که نوشتی جزو هیچکدام از روشهای استاندارد گفته شده نیست . چون تا وقتی یکی از متدهاش call نشه ، به طور مشخص ایجاد نشده و به صورت object در کلی ترین حالت وجود داره و لذا منابع سیستم رو هدر میده. حالا اینها رو بخون امیدوارم مشکلی نباشه.


Declaring Object Variables
Before you create an instance of a component, you must declare an object variable that points to the COM object. You declare an object variable as either specific or generic, depending on how you plan to use the variable. In some cases, you do not know at design time the specific type of object your application will use. In these situations, you can use generic object variables to hold pointers to any type of object. For example, you might want to write a function that acts on any one of several different classes of objects. In this case, you must declare the variable as an Object. The following code uses the Object data type to declare a generic object variable:

Dim MyObj As Object



When you use this type of declaration, no information is known about the object at design time. Visual Basic must do additional work to access the object at run time. This causes a negative impact on the client application's performance. This type of variable declaration is called late binding; use it only if absolutely necessary.

If you know at design time the type of object you will be creating at run time, you should specifically declare the variable. The following code creates a variable that will hold pointers to Microsoft Excel objects only:

Public xlApp As Excel.Application
Private xlChart As Excel.Chart
Static xlSheet As Excel.Worksheet




The code which follows points to other registered objects, such as a Visual Basic project's Class module:

Static obj1 As Project1.Class1



Visual Basic checks the syntax of calls that use specific object variables. The compiler can produce more efficient code to access the object at run time. At design time, Visual Basic also provides the Auto List Members, which presents a drop-down list of available members (such as properties and methods). This type of object binding is referred to as early binding.

Instantiating an Object
When you declare an object variable, Visual Basic allocates sufficient memory to hold a pointer to an object. When an object is instantiated, Visual Basic creates the component in memory, and the object variable points to that instance of the component. As discussed in Lesson 1 of this chapter, you must properly register the component before you instantiate it; also, the component's object library must be referenced in the client application.

In Visual Basic, there are three ways to create an instance of a COM server object:


Use the GetObject function.


Use the CreateObject function.


Use the New keyword with a Set statement.

Using the GetObject Function
Use the GetObject function if there is a current instance of the object or if you want to create the object with a file that already exists. The first argument of the GetObject function, pathname, specifies the full path and name of the file containing the object that you want to retrieve. If you omit the pathname, the second argument, class, is required. The class argument is a string value representing the class of the object. The class acts as the template from which an instance of an object is created at run time. The class defines the properties of the object and the methods used to control the object's behavior. Properties and methods also appear in the Auto List Members drop-down list.

The following example creates an instance of a Microsoft Word document object called MyDocument.doc and displays it in Print Preview mode:

Sub ShowDocument()
'Creating a specific object variable
Static wdDoc As Word.Document
'Opens Microsoft Word and displays MyDocument.doc
Set wdDoc = GetObject("C:\MyDocument.doc", "Word.Document")
'By default new objects are not visible
wdDoc.Parent.Visible = True
wdDoc.PrintPreview
End Sub




The following code creates an instance of the application associated with the .xls extension and activates the object in the specified file:

Dim xl As Object
'Do not specify a class type
Set xl = GetObject("C:\MyFiles\Earnings.xls")




Using the CreateObject Function
If there is no current instance, and you don't want to open a file when the object is started, use the CreateObject function. In addition, if you must use a generic variable in your application because you do not know the specific object type until run time (late binding), use CreateObject to instantiate the class. If an object has registered itself as a single-instance object, Visual Basic will only create one instance of the object, no matter how many times you call CreateObject.

The following example uses the CreateObject function to create an instance of Microsoft Excel. It uses the reference to access the Visible property of Microsoft Excel, and then uses the Quit method to close the object. Finally, the reference itself is released by setting it equal to the Nothing keyword.

Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")

With xlApp
'Set Visible property to True to see the application
.Visible = True
'[statements]
'When you finish, use the Quit method to close the application
.Quit
End With
'Release the reference
Set xlApp = Nothing




Using the New Keyword with a Set Statement
If you have set a reference to the type library for the external component, and can use a specific object variable (early binding), then use the New keyword with the Set statement to create an instance of the class you want to use in your application.

The Dim, Private, Public, ReDim, and Static statements only declare a variable that refers to an object. No actual object is instantiated until you use the Set statement to assign a specific object. The following code uses the New keyword with the Set statement to create an instance of Microsoft Word:

Dim wd As Word.Application
Set wd = New Word.Application
With wd
.Visible = True
'Add a new document to the Word application
.Documents.Add
'Add text to the active document
.Selection.TypeText Text:="This text was added"
'Print the current document
.ActiveDocument.PrintOut
End With