PDA

Click to See Complete Forum and Search --> : Pointers


Bjwbell
Jan 28th, 2001, 06:32 PM
Why can't you use CopyMemory to copy a dimed variable in module using a pointer to the variable?


Here's My code

Form Code

Private Sub Command3_Click()
Dim i As Integer
Dim ThePointer As Long
Dim TheLen As Integer
Dim TheText As String
Dim thetext1 As String

ThePointer = test2(TheLen)
CopyMemory TheText, ThePointer, TheLen
For i = 1 To Len(TheText) Step 2
thetext1 = thetext1 & Mid(TheText, i, 1)
Next
Text4.Text = thetext1
End Sub

here's the Module Code

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, ByVal Source As Any, ByVal Length As Long)






Public Function test2(TheLen As Integer) As Long

Dim MyStr as String
MyStr = "Hope this works"
TheLen = Len(MyStr)
test2 = VarPtr(MyStr)
End Function

When i Click The Button I get a illegal operation error.
If i Change the Dim MyStr as String to Private MyStr as String It works.
Why doesn't it work when i use the dim statement?

Bjwbell
Jan 30th, 2001, 05:18 PM
well since i didn't get any replys i guess no wanted to answer or i didn't write a clear message.

Anyways The answer to my question is I think, that when you make a module variable private the variable doesn't get destroyed until you close the app,

if you dim a variable in a function when the function finishes the variable gets detroyed.

Please Tell me if i'm wrong.

frunkenstein
Feb 1st, 2001, 02:27 AM
You're right. it get's destroyed.

if you dim it in a module and lay the functions in a module, you'll not need to use private.

and it won't get destroyed.

Bjwbell
Feb 1st, 2001, 08:58 PM
Thanks. wasn't sure if i was correct or not.

Dinesh_MS
Feb 4th, 2001, 01:25 PM
In addition to the variable scope the variable thetext should be a buffer something like

thetext=space$(thelen + 1)

Feb 4th, 2001, 03:23 PM
Just some correction on terminology here... ;-]

CopyMemory to copy a dimed variable

CopyMemory to copy a local variable.


if you dim a variable in a function

if you declare a variable in a function


if you dim it in a module

if you declare it in a module

frunkenstein
Feb 6th, 2001, 03:14 AM
I haven't tried it yet
=)