Silly question or silly compiler ? 
Author Message
 Silly question or silly compiler ?

Hello !

There's my silly question number 2 why isn't working again ? :-)

Silly Compiler ????? VB 5.0

-----------------------------------------
Sub vec(s() As Integer)
s(1) = 100
End Sub

Private Sub Command1_Click()

Dim vi(5) As Integer

vi(1) = 1
vi(2) = 2
vec (vi())  

'   ^^^^^^^    I got a type mismatch or user-defined type expected error
'
'  if I use call vec( vi() )  everything is ok
'  if I use   vec vi()               it's Ok
'
'  Silly documentation and Silly Compiler ?

Debug.Print vi(0)
Debug.Print vi(1)
Debug.Print vi(2)

End Sub
.....

I got a type mismatch or user-defined type expected error

Thanks !



Mon, 09 Apr 2001 03:00:00 GMT  
 Silly question or silly compiler ?

Quote:
>There's my silly question number 2 why isn't working again ? :-)

Not a silly question ... and not a silly compiler, either.

Quote:
>Sub vec(s() As Integer)
>s(1) = 100
>End Sub
>vec (vi())  

You get an error because "(vi())" is an expression which VB can't
interpret.  The subroutine is expecting an array to be passed by
reference, so:

  vec vi()

works - VB passes the address of the vi array to the subroutine.  But
when you put the parens around vi, VB is trying to evaluate the
expression "(vi())" which it can't do.  Using Call works:

  Call vec (vi())

because the parens are part of the Call statement, not a part of an
expression.

Using parens to force Basic to evaluate an expression was commonly
used in the "old days" before the ByVal keyword was added to VB.  It

  MySub (X)

you were assured that MySub wasn't going to change X because VB
evaluated the expression "(X)", created a temporary variable to hold
the result, and passed the temporary variable to MySub.  So if MySub
changed the value passed to it, X would not be changed.

-- Roger
 * Give Kids the World: http://www.gktw.org/



Mon, 09 Apr 2001 03:00:00 GMT  
 Silly question or silly compiler ?

OK  but Now Why do we have that ?
Stuff generate by the Wizard !

Why GetSetting has  () ?
It's clearly a function call without call keyword.

:-)

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

Private Sub Form_Load()
    Me.Left = GetSetting(App.Title, "Settings", "MainLeft", 1000)
    Me.Top = GetSetting(App.Title, "Settings", "MainTop", 1000)
    Me.Width = GetSetting(App.Title, "Settings", "MainWidth", 6500)
    Me.Height = GetSetting(App.Title, "Settings", "MainHeight", 6500)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim i As Integer

    'close all sub forms
    For i = Forms.Count - 1 To 1 Step -1
        Unload Forms(i)
    Next
    If Me.WindowState <> vbMinimized Then
        SaveSetting App.Title, "Settings", "MainLeft", Me.Left
        SaveSetting App.Title, "Settings", "MainTop", Me.Top
        SaveSetting App.Title, "Settings", "MainWidth", Me.Width
        SaveSetting App.Title, "Settings", "MainHeight", Me.Height
    End If
End Sub

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



Quote:
> >There's my silly question number 2 why isn't working again ? :-)

> Not a silly question ... and not a silly compiler, either.

> >Sub vec(s() As Integer)
> >s(1) = 100
> >End Sub

> >vec (vi())  

> You get an error because "(vi())" is an expression which VB can't
> interpret.  The subroutine is expecting an array to be passed by
> reference, so:

>   vec vi()

> works - VB passes the address of the vi array to the subroutine.  But
> when you put the parens around vi, VB is trying to evaluate the
> expression "(vi())" which it can't do.  Using Call works:

>   Call vec (vi())

> because the parens are part of the Call statement, not a part of an
> expression.

> Using parens to force Basic to evaluate an expression was commonly
> used in the "old days" before the ByVal keyword was added to VB.  It

>   MySub (X)

> you were assured that MySub wasn't going to change X because VB
> evaluated the expression "(X)", created a temporary variable to hold
> the result, and passed the temporary variable to MySub.  So if MySub
> changed the value passed to it, X would not be changed.

> -- Roger
>  * Give Kids the World: http://www.gktw.org/



Mon, 09 Apr 2001 03:00:00 GMT  
 Silly question or silly compiler ?
It's the difference between a call and a function.

A function expects parenthesis as in:
x = SomeFunction(y)
or
x = msgBox("Hello")

While a sub does not as in:
SomeSub y
or
msgBox "Hello"

So your call should read:
vec vi())

Peter S

P.S. Using call is bad karma, and an old format that should not be used. It
is still supported for reasons of backwards comaptibility, but frankly IMHO
should be removed from the language. What it does when used with a sub is
to require the parenthesis around your parameter list (which is why it
worked for you) and force the parameter to be evaluated. This means, among
other things, that byref is broken because you are actually passing in a
copy of the parameter (ie the temporary evaluated result).



Mon, 09 Apr 2001 03:00:00 GMT  
 Silly question or silly compiler ?
Peter -
     Without getting into the value of the "Call" keyword, I'd like to point
out that you're mistaken about its effects.   The use of parens with Call
does not force the parameters to be evaluated, nor does it undo the effects
of ByRef.  That happens only when you use parens -without- using call.

There is absolutely no difference between:

   Call vec (vi())

...and...

   vec vi()

This is dead easy to prove, it takes just a simple experiment.

--
     Jim Mack
     MicroDexterity, Inc

     http://www.microdexterity.com



Quote:

>P.S. Using call is bad karma, and an old format that should not be used. It
>is still supported for reasons of backwards comaptibility, but frankly IMHO
>should be removed from the language. What it does when used with a sub is
>to require the parenthesis around your parameter list (which is why it
>worked for you) and force the parameter to be evaluated. This means, among
>other things, that byref is broken because you are actually passing in a
>copy of the parameter (ie the temporary evaluated result).



Tue, 10 Apr 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. silly question: how to show a form

2. Silly ArrayList Question

3. really silly question on "Builds"

4. VB.net silly tabcontrol questions

5. Just a silly question

6. Some Silly Questions

7. Some Silly Questions

8. Two Silly Questions (It's me again)

9. Potentially way silly question - DBF?

10. Silly question -- Parameter Query using DBGrid.

11. Newbie Silly Question

12. graphics...silly question...but...

 

 
Powered by phpBB® Forum Software