Visual Basic's eval function?

I know that ASP and just about every other language have one, but is it just me, or does Visual Basic not have an eval function?

Basically, I need to add to an array–but I don’t know what the [0] number is. Does anyone know how to combat VB’s limitations and use eval or know a different way?

Thanks in advance…


-Quinn Slack
http:/www.fourthdev.com has webmaster resources!

From Microsoft’s VB Script 5.1 Reference downloadable from their MSDN Website…

Description
Evaluates an expression and returns the result.
Syntax
[result = ]Eval(expression)
The Eval function syntax has these parts:

Part Description
result Optional. Variable to which return value assignment is made. If result is not specified, consider using the Execute statement instead.
expression Required. String containing any legal VBScript expression.

Remarks
In VBScript, x = y can be interpreted two ways. The first is as an assignment statement, where the value of y is assigned to x. The second interpretation is as an expression that tests if x and y have the same value. If they do, result is True; if they are not, result is False. The Eval method always uses the second interpretation, whereas the Execute statement always uses the first.

Note In Microsoft® JScript™, no confusion exists between assignment and comparison, because the assignment operator (=) is different from the comparison operator (==).

The following example illustrates the use of the Eval function:
<BLOCKQUOTE><font size=“1” face=“Verdana, Arial”>code:</font><HR><pre>
Sub GuessANumber
Dim Guess, RndNum
RndNum = Int((100) * Rnd(1) + 1)
Guess = CInt(InputBox(“Enter your guess:”,0))
Do
If Eval(“Guess = RndNum”) Then
MsgBox “Congratulations! You guessed it!”
Exit Sub
Else
Guess = CInt(InputBox(“Sorry! Try again.”,0))
End If
Loop Until Guess = 0
End Sub



--------------------------------------------------------------------------------

Hope this Helps.


------------------
Wayne Luke
WR Moderator
Internet Media Provider

When working with arrays you can always use the LBOUND (low boundary) and UBOUND (Upper Boundary) Functions.

Here is lbound
Description
Returns the smallest available subscript for the indicated dimension of an array.
Syntax
LBound(arrayname[, dimension])
The LBound function syntax has these parts:

Part Description
arrayname Name of the array variable; follows standard variable naming conventions.
dimension Whole number indicating which dimension’s lower bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed.

Remarks
The LBound function is used with the UBound function to determine the size of an array. Use the UBound function to find the upper limit of an array dimension.
The lower bound for any dimension is always 0.

Here is ubound

Description
Returns the largest available subscript for the indicated dimension of an array.
Syntax
UBound(arrayname[, dimension])
The UBound function syntax has these parts:

Part Description
arrayname Required. Name of the array variable; follows standard variable naming conventions.
dimension Optional. Whole number indicating which dimension’s upper bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed.

Remarks
The UBound function is used with the LBound function to determine the size of an array. Use the LBound function to find the lower limit of an array dimension.
The lower bound for any dimension is always 0. As a result, UBound returns the following values for an array with these dimensions:

Dim A(100,3,4)

Statement Return Value
UBound(A, 1) 100
UBound(A, 2) 3
UBound(A, 3) 4

Then to add to it you just use the redim statement with the PRESERVE keyword.

Here is the help for REDIM
Description
Declares dynamic-array variables, and allocates or reallocates storage space at procedure level.
Syntax
ReDim [Preserve] varname(subscripts) [, varname(subscripts)] . . .
The ReDim statement syntax has these parts:

Part Description
Preserve Preserves the data in an existing array when you change the size of the last dimension.
varname Name of the variable; follows standard variable naming conventions.
subscripts Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax:
upper [,upper] . . .

The lower bound of an array is always zero.

Remarks
The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts). You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array.
If you use the Preserve keyword, you can resize only the last array dimension, and you can’t change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array.

The following example shows how you can increase the size of the last dimension of a dynamic array without erasing any existing data contained in the array.

ReDim X(10, 10, 10)
. . .
ReDim Preserve X(10, 10, 15)


Caution If you make an array smaller than it was originally, data in the eliminated elements is lost.


When variables are initialized, a numeric variable is initialized to 0 and a string variable is initialized to a zero-length string (“”). A variable that refers to an object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing.

You can download the complete reference from http://msdn.microsoft.com/scripting/. You might think about grabbing the Jscript reference also. Note that neither of these references expose object declared by either the Browser or ASP as these are not part of the actual scripting languages.


Wayne Luke
WR Moderator
Internet Media Provider

I meant the Win23 Visual Basic, not ASP. Thanks for the info.


-Quinn Slack
http:/www.fourthdev.com has webmaster resources!

The complete reference for Visual Basic 6.0 is available at: http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/devprods/vs6/vbasic/vbcon98/vbstartpage.htm

And no Visual Basic 6.0 doesn’t ship with an Eval function. You can still use Lbound and Ubound though. What exactly are you trying to do? There may be a simple solution your overlooking.


Wayne Luke
WR Moderator
Internet Media Provider

The code is below…

Private Sub mnuOpen_Click()
If txtNotepad = “” Then
On Error GoTo OpenProblem
cdbOpenSave.Filter = “Active Server Pages|.asp|ASP Includes|.inc|ASA Files|.asa|HTML Files|.htm*|All Files|.|”
cdbOpenSave.FilterIndex = 1

        cdbOpenSave.Action = 1
        Open cdbOpenSave.FileName For Input As 1
        txtNotepad.Text = Input$(LOF(1), 1)
        Close 1
        frmMain.Caption = cdbOpenSave.FileName & " - ProASP"
        CurrentFileName = cdbOpenSave.FileName
        tb.TabCaption(0) = cdbOpenSave.FileTitle
        Filenames(1) = cdbOpenSave.FileName
        Exit Sub

OpenProblem:
Exit Sub
Else
strEval = “Documents(” & tb.Tabs + 1 & “) = txtNotepad”
Execute (strEval)

        On Error GoTo OpenProblem
        cdbOpenSave.Filter = "Active Server Pages|*.asp|ASP Includes|*.inc|ASA Files|*.asa|HTML Files|*.htm*|All Files|*.*|"
        cdbOpenSave.FilterIndex = 1
        cdbOpenSave.Action = 1
        Open cdbOpenSave.FileName For Input As 1
        TmpTxt = Input$(LOF(1), 1)
        Close 1
        frmMain.Caption = cdbOpenSave.FileName & " - ProASP"
        CurrentFileName = cdbOpenSave.FileName
        tb.Tabs = tb.Tabs + 1
        tb.TabCaption(tb.Tabs + 1) = cdbOpenSave.FileTitle
        strEval2 = "Filenames(" & tb.Tabs & ") = cdbOpenSave.FileName"
        Execute (strEval2)
        Exit Sub

End If
End Sub

The ‘execute’ function was just me playing around to see what worked. And…it’s not really optimized yet, but that’s the code for one of the subs.

What I’m trying to do is to add the filename and the file’s contents to an array. This is mainly for practice but I plan on giving it away to my site’s visitors.

Does anyone know how to combat the limitations? Thanks!

P.S. Am I the only one who thinks that VB needs an eval function?


-Quinn Slack
http:/www.fourthdev.com has webmaster resources!