Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

pointers???

does VB use an access object similar to pointers in other languages. If so, could I see some code.
0
timpeters
Asked:
timpeters
  • 4
  • 3
1 Solution
 
StapleheadCommented:
tim,

you *can* access pointers to objects by using the (officially) undocumented varptr() call, but that isn't really a kosher vb way to do things.  in addition, when creating objects, your variables are really references, which means that behind the scenes, they're treated like pointers, but to your view, they're data, not pointers to data.

what is it that you're trying to do?  although there are instances when you need to use pointers to data (some API calls, etc), there's "usually" a way to do things in VB without resorting to pointer usage (anathema to a guy who cut his teeth on C, but there: i've said it!).

Larry
0
 
timpetersAuthor Commented:
I am reading in data and I want a way to store  this information at run time, but I have no way of knowing the length of the file I am reading.  Using a Redim statement will not accomplish what I am trying to do.  I am trying to avoid using arrays.
0
 
clifABBCommented:
Hooray Staplehead, I knew you could do it.  :)

Seriously, though, as Staplehead noted, pointers to objects are actually references.  Sort of like pointers to pointers.

There may be a third party control which will actually give you a true pointer to an object.  I'm not sure, but SpyWorks by Desaware (www.desaware.com) might do it.  You also might check out Stamina from MicroDexterity (www.microdexterity.com).
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
timpetersAuthor Commented:
I understand completely what pointers are.  I have just never seen them used in VB.  I am looking for some code that uses them.  I just want to use them to keep a linked list of text strings.
0
 
StapleheadCommented:
tim,

on the other hand, it seems that you're asking about pointers because you're considering building a linked list.

this can be done in vb pretty straightforwardly.  instead of your "next" element being a pointer to your base type, it can be (implicitly) a reference to your base type.  when creating a new link, just set the next = some other element in your list.

vb classes work well here.

if this is what you're trying to do, and need a code sample, let me know...

Larry
0
 
timpetersAuthor Commented:
a code sample would be great.  I am reading info from a textfile and want to create a linked list.
0
 
StapleheadCommented:
ok, i didn't get a chance to jump into VB last night, so let me type some code at you here...

here's your class (use a variant to make it as reusable as possible; it's not wrong (but against the spirit of OOP) to use a more definite type):

Public Data as Variant
Public Next as CLinkList

you'll want to have two private data members in your class (CLinkList):

private llHead as CLinkList
private nbrElements as long

you'll want to have a method to return the first element; since you'll want other classes to be able to call it, make it a friend method (property):

friend property get head() as CLinkList
    set head = llHead
end property

you'll want to be able to add an element:

sub add (llData as variant)
    dim newNode as new CLinkList

    if isobject(vitem) then
        set newNode.Data = llData
    else
        newNode.Data = llData
    end if

    set newNode.Next = llHead
    set llHead = newNode

    count = count + 1
end sub


ok, so there's the meat.  since you said you're familiar with pointers / C, i'm skipping some code... this code inserts at the head of the list; you could also insert at the tail (with a "pointer" to the current tail, or by traversing the list).  deleting from the head is also straightforward (by making llHead refer to its current Next property).  you'll also want to be able to search through the list (by walking through the Next's).  like i said, i'm assuming that you're familiar with that kind of thing, so the code above gives you the flavor of the implementation.  since i'm fresh from playing around with java, i'll also add that in the OO world, there's the opinion that you should have a standard way of moving through a list of things, regardless of how that list of things is implemented (list, stack, etc).  usually, they're called iterators, or enumerations, or some such.  you may want to look at defining a class that knows how to walk through your linked list; of course, that code could also be part of your class, but that sort of thing is usually frowned upon by OO gurus.

i'm pretty sure that "Hardcore VB" has a section on linked lists, stacks, vectors, and collections.  (it's a good book: if it's not on your bookshelf, i recommend it...)

is that what you were looking for?
0
 
StapleheadCommented:
tim,

i just saw the grade you assigned to my answer.  was there something wrong with it?  drop me an e-mail: ldadams@hotmail.com
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now