|
-
Jan 28th, 2001, 07:32 PM
#1
Thread Starter
Addicted Member
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
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?
Last edited by Bjwbell; Jan 28th, 2001 at 09:27 PM.
-
Jan 30th, 2001, 06:18 PM
#2
Thread Starter
Addicted Member
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.
-
Feb 1st, 2001, 03:27 AM
#3
New Member
Blazze
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.
-
Feb 1st, 2001, 09:58 PM
#4
Thread Starter
Addicted Member
Thanks. wasn't sure if i was correct or not.
-
Feb 4th, 2001, 02:25 PM
#5
Junior Member
In addition to the variable scope the variable thetext should be a buffer something like
thetext=space$(thelen + 1)
-
Feb 4th, 2001, 04:23 PM
#6
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
-
Feb 6th, 2001, 04:14 AM
#7
New Member
Heheh
I haven't tried it yet
=)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|