[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Access object programmatically

Posted on 2009-04-27
10
Medium Priority
?
276 Views
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.).
0
Comment
Question by:jonathan_mccoy
9 Comments
 
LVL 10

Expert Comment

by:ALaRiva
ID: 24245621
For an example, let's say you wanted to set the visible property to false.

See code.

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

Open in new window

0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 24245841
ALaRiva, I believe you didn't try your code?
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 24246042
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:
http://stackoverflow.com/questions/314008/c-programatically-using-a-string-as-object-name-when-instantiating-an-object.

Of course, one more option is to use reflection. Do you want to use reflection?
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 10

Expert Comment

by:ALaRiva
ID: 24246084
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.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 24246402
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! :)
0
 
LVL 13

Expert Comment

by:crazyman
ID: 24249786
Why not just Create a function...

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

DoSomething(Thing1);
DoSomething(Thing2);
0
 
LVL 2

Author Comment

by:jonathan_mccoy
ID: 24253104
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.
0
 
LVL 10

Expert Comment

by:ALaRiva
ID: 24253193
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
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 24255391
Hmmm...  I believe it's not achievable what yiou want. See the discussion:
http://www.eggheadcafe.com/community/aspnet/2/10009046/get-reference-to-an-objec.aspx

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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Integration Management Part 2
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 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