12-12-2005, 23:07
|
|
|
חבר מתאריך: 20.12.01
הודעות: 20,962
|
|
אתה צודק בקטע של תכונת ברירת-המחדל, אבל לא לגבי המשתנים :)
ב-VB6 וב-VBA ניתן להצהיר על משתנים עם טיפוס, בסגנון:
Dim var As type
או על (הפניות ל) אובייקטים:
Dim o As class
ואז ליצור אובייקט דרך:
Set o = New class
ב-VBS העניינים שונים. מילת המפתח As נעלמה, וניתן להצהיר על משתנים, אך ללא טיפוס.כל
המשתנים הם Variant-ים, בלי קשר ל-Option Explicit. כמו-כן, בעזרת New אפשר ליצור רק
אובייקטים ממחלקות שהוגדרו בקוד עצמו(אפשר מ-VBS 5 ומעלה), אך רכיבי ActiveX חיצוניים
חייבים ליצור באמצעות הפונקציה CreateObject(שקיימת גם ב-VB6 וב-VBA).
מה ש-Option Explicit מגדיר זה ששימוש במשתנה ללא הצהרה עליו בעזרת Dim יגרור שגיאה,
דבר די טריויאלי בכל שפת-תכנות נורמלית. אבל בהתחשב בכך שהתחלנו מ-GW-BASIC ו-MS
רצו לשמור על תאימות לאחור, המצב ההזוי הזה די ניתן להבנה. עם זאת, Option Explicit אינו
אומר דבר על טיפוסים. הוא רק דורש הצהרה, איזו שבא לך. ב-VB6/VBA יש לך ברירה אם
להגדיר טיפוס, ב-VBS אין לך.
כדי להיות בטוח, ולרענן את הזכרון, אתה יכול להתסכל בתיעוד של Dim, שבו אין As או טיפוסים,
וכמו-כן בדף Visual Basic for Applications Features Not In VBScript שבו כתוב במפורש
שבמחינת טיפוסי נתונים ב-VBS אין:
All intrinsic data types except Variant
Type...End Type
הדף VBScript Variables מוסיף ומסביר שיש רק Variant ב-VBS כך:
In VBScript, variables are always of one fundamental data type, Variant.
וגם מסביר מה עושה Option Explciti כך:
You can also declare a variable implicitly by simply using its name in
your script. That is not generally a good practice because you could
misspell the variable name in one or more places, causing unexpected
results when your script is run. For that reason, the Option Explicit
statement is available to require explicit declaration of all variables.
The Option Explicit statement should be the first statement in your
script.
העמוד VBScript Data Types מרחיב לגבי העניין:
VBScript has only one data type called a Variant. A Variant is a
special kind of data type that can contain different kinds of information,
depending on how it is used. Because Variant is the only data type in
VBScript, it is also the data type returned by all functions in VBScript.
At its simplest, a Variant can contain either numeric or string
information. A Variant behaves as a number when you use it in a
numeric context and as a string when you use it in a string context.
That is, if you are working with data that looks like numbers, VBScript
assumes that it is numbers and does what is most appropriate for
numbers. Similarly, if you're working with data that can only be string
data, VBScript treats it as string data. You can always make numbers
behave as strings by enclosing them in quotation marks (" ").
וממשיך בהסברים על "תת-הסוגים" של ה-Variant.
טוב, זה יצא די ארוך וחופר, אבל לא נורא
בהתחלה חשבתי שנשאיר את זה כאן לידע כללי, למרות שזה מתאים יותר לפורום תכנות או ASP,
אבל בקריאה חוזרת של השאלה אני רואה שזו שאלה ASP מובהקת, ככה שאני מניח שהעברה
לפורום ASP לא תזיק יותר מדי.
נערך לאחרונה ע"י AlexKarpman בתאריך 12-12-2005 בשעה 23:11.
|