Results 1 to 7 of 7

Thread: Pointers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    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.
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154

    Unhappy

    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.
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  3. #3
    New Member
    Join Date
    May 2000
    Location
    Sweden
    Posts
    6

    Thumbs up 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.
    Frunkenstein

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154

    Cool

    Thanks. wasn't sure if i was correct or not.
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  5. #5
    Junior Member
    Join Date
    Mar 2000
    Location
    India, Visakhapatnam
    Posts
    19
    In addition to the variable scope the variable thetext should be a buffer something like

    thetext=space$(thelen + 1)
    Dinesh
    reply to
    <[email protected]>

  6. #6
    Guest
    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

  7. #7
    New Member
    Join Date
    May 2000
    Location
    Sweden
    Posts
    6

    Heheh

    I haven't tried it yet
    =)
    Frunkenstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width