Results 1 to 5 of 5

Thread: pointers in vb ?

  1. #1

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577

    Question pointers in vb ?

    Ive had .net for a few weeks now and only really just started to use it. vb.net has become alot more like visual c++ now which i think is a plus. Some of the new code changes like the returns for functions, try catch, and the new string variables are just plain awsome, but does it support pointers?
    If it dont then that would be a big downside to the program, ok so it would have a very steep learning curve but in the long run i think it would be a more succesful programming language with pointers implemented. ( Imagine vb apps with 'proper' plugin support? wow )

    oh, off topic, does vb7 still require runtimes? i know it requires framenet but so does vc.net, right?

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    No VB.Net doesn't have pointers, and I don't agree with you about the fact that the lack of pointers is a downside to the language. Personally, and I am an old C/C++ programmer, I think pointers is terrible. All modern languages exclude the use of pointer, even C# haven't included them (you don't have to tell me that you actually can use pointers in C# if you mark the project as unmanaged).

    And the CLR is the run-time files. If they are installed you can install your application with XCopy if you like.

  3. #3
    New Member
    Join Date
    Oct 2001
    Location
    Wichita, KS
    Posts
    1
    you might notice that by default when declaring a sub you see a by val in there (or something to that effect) the closest you will get to pointers would be changing it to by ref . of course, those would be by value, and by reference, respectively...it is not really a pointer, but well bah, you are a c/c++ programmer you get the idea

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    You can use pointers in vb.net!!
    I will post an example as soon as I get home.

    There are things to keep in mind though.
    You first need to pin the variable, because otherwise the garbage collector could move the contents to a different location (the garbage collector compacts the memory, a sort of defragmentation)

  5. #5
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    OK, here we go.
    Make sure to import the interopservices:
    Code:
    Option Explicit On 
    
    Imports System.Runtime.InteropServices
    and then wherever you need it something like this

    Code:
            Dim sSample As String = Space(128)  ' this is the variable we get the pointer for
    
            ' pin variable and create
            ' GC handle instance
            Dim gh As GCHandle = GCHandle.Alloc(sSample , GCHandleType.Pinned)
            ' get address of variable
            Dim ptrRet As IntPtr = gh.AddrOfPinnedObject()
    
            ' OK, now ptrRet  contains your pointer. 
            ' Please be aware that an IntPtr is an integer on a 32 bit OS and a Long on a 64 bit OS
            ' So ptrRet.ToInt32 will cause an overflow on a 64 bit OS
    
            ' do not forget to free the handle and unpin variable
            ' so garbage collector can do it's work
            gh.Free()

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