Access object programmatically

Posted on 2009-04-27
Last Modified: 2012-05-06
Let's say I've got two objects that are named Thing1 and Thing2. I would like to be able to loop through these objects and apply the same logic to each. Obviously, the loop logic would have to construct the name on the fly (like "Thing" + x, where x = 1 during the first iteration, x = 2 during the second iteration, etc.).
Question by:jonathan_mccoy
    LVL 10

    Expert Comment

    For an example, let's say you wanted to set the visible property to false.

    See code.

    Dim i as Integer
    For i = 1 to 2
    ("Thing" & i).visible = False

    Open in new window

    LVL 29

    Expert Comment

    ALaRiva, I believe you didn't try your code?
    LVL 29

    Expert Comment

    jonathan_mccoy, what's exactly your task? I believe in many cases it is not necessary to construct a name on the fly. For example, just put all your objects toan array and access these objects as memebrs of the array:

    Object[] myObjects = {Thing1, Thing2};

    Now you may loop through the array.

    One more option is to use a dictionary:

    Of course, one more option is to use reflection. Do you want to use reflection?
    LVL 10

    Expert Comment

    bah, anarki, you are correct.

    However, the code would work in MS Access VBA.  I was simply lost on which forum I was in.  I switch between .Net (VB.Net mostly, not C#), Access, SQL Server, and a few others.  I read the title "Access object programmatically" and didn't realize I wasn't in the Access Forum.

    My apologies.
    LVL 29

    Expert Comment

    Wow! It's working in VBA!? I didn't know. Unfortunately it does not work even in VB (I work in VB.Net mostly). Yeah, it would be cool! :)
    LVL 13

    Expert Comment

    Why not just Create a function...

    public static void DoSomething(MyObjectType obj)
        obj.Whatever = 1;

    LVL 2

    Author Comment

    so say I don't know the names of thing 1 & 2 how could I get the names and then use them.
    yes I know most I would never need to do this. but say I did not have the names of my objects and needed to access them.

    I really know what I'm asking for, I would like to dynamically access object with out coding a reference to them, but instead make a reference to them at run time.
    LVL 10

    Expert Comment

    do you at least have some logic to figure out the naming?

    Like, you have them all prefixed with "THING" but the ending number changes?

    Do you want this done to all controls?

    You have to have some logic first in order for someone to provide an example/solution.

    - Anthony
    LVL 29

    Accepted Solution

    Hmmm...  I believe it's not achievable what yiou want. See the discussion:

    The thing is that the problem has nothing to do with reflection, I'd say.

    You are trying to create a reference from a string. However these reference names are not features of objects in a memory.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Dependencies in Software Design In software development, the idea of dependencies ( is an issue of some importance. This article seeks to explain what dependencies are and where they …
    This video discusses moving either the default database or any database to a new volume.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    737 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

    18 Experts available now in Live!

    Get 1:1 Help Now