Solved

pointers???

Posted on 1998-07-14
8
251 Views
Last Modified: 2010-05-03
does VB use an access object similar to pointers in other languages. If so, could I see some code.
0
Comment
Question by:timpeters
  • 4
  • 3
8 Comments
 
LVL 1

Expert Comment

by:Staplehead
ID: 1465716
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
 

Author Comment

by:timpeters
ID: 1465717
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
 
LVL 6

Expert Comment

by:clifABB
ID: 1465718
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
 

Author Comment

by:timpeters
ID: 1465719
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:Staplehead
ID: 1465720
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
 

Author Comment

by:timpeters
ID: 1465721
a code sample would be great.  I am reading info from a textfile and want to create a linked list.
0
 
LVL 1

Accepted Solution

by:
Staplehead earned 50 total points
ID: 1465722
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
 
LVL 1

Expert Comment

by:Staplehead
ID: 1465723
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now