Solved

Access an Object using a Variable

Posted on 2007-11-15
15
240 Views
Last Modified: 2010-04-15
Just starting out on C# from VB and I want to know since there is no Object Arrays in C# that can I access an object by using a String.

Non Working Example:
TheObject("Button4").visible=false

That is the kind of Functionality I am looking for.

Thanks
0
Comment
Question by:scogger1974
  • 8
  • 6
15 Comments
 
LVL 7

Expert Comment

by:dctuck
ID: 20290457
If you're wanting to reference controls in the form you can use:
this.Controls["Button4"].Visible = false;
0
 
LVL 1

Author Comment

by:scogger1974
ID: 20290468
I guess the Second Part of this Question would come in ...
Could I also Name an Object Based on a string....
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20290489

string mystring = "somename";

this.Button1.Location = new System.Drawing.Point(632, 412);
this.Button1.Name = mystring;
this.Button1.Size = new System.Drawing.Size(75, 23);
this.Button1.TabIndex = 41;
this.Button1.Text = "Update db ";
this.Button1.UseVisualStyleBackColor = true;
this.Button1.Visible = false;
this.Controls.Add(this.Button1);
0
 
LVL 1

Author Comment

by:scogger1974
ID: 20290534
Here is the code based on what you said.  but here is whats happening
// Create Button
            Button ButtonA = new Button();
            ButtonA.Location = new Point(7, 146);
            ButtonA.Size = new Size(94, 23);
            ButtonA.Text = "ADD ITEM";
            groupBox4.Controls.Add(ButtonA);
            this.Controls["ButtonA"].Visible = false;

Here is the error it returns
Object reference not set to an instance of an object.
0
 
LVL 1

Author Comment

by:scogger1974
ID: 20290563
vbturbo:
  I know I can chage the Name but I want to be able to use that name to access the Object.
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20290613
       private void Form1_Load(object sender, EventArgs e)
        {
            Button ButtonA = new Button();
            ButtonA.Location = new Point(7, 146);
            ButtonA.Size = new Size(94, 23);
            ButtonA.Text = "ADD ITEM";
            //groupBox4.Controls.Add(ButtonA);
            this.Controls.Add(ButtonA);
        }

vbturbo
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20290632
sorry

        private void Form1_Load(object sender, EventArgs e)
        {
            Button ButtonA = new Button();
            ButtonA.Location = new Point(7, 146);
            ButtonA.Size = new Size(94, 23);
            ButtonA.Text = "ADD ITEM";
            this.groupBox1.Controls.Add(ButtonA);
            //this.Controls.Add(ButtonA);
        }
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 18

Expert Comment

by:vbturbo
ID: 20290672
and use for docking the button to always be inside the groupbox

ButtonA.Location = new Point(this.groupBox1.Top + 10, this.groupBox1.Left + 20);
0
 
LVL 1

Author Comment

by:scogger1974
ID: 20291152
If I set the .Name Property ... the Object name remains the same... I want to do something like this
although this doesnt work

Button "SomeString"= new Button();

and then be able to do

Object("Somestring").Visible = false;

0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20291569
Im not quite sure what you mean

but here you create a button array object and add button objects, where you are able to set the properties of the buttons before adding them into the array

Button[] ButtonArray = new Button[9];

    public void AddButtons() {
        for (int x = 0; (x <= 9); x++) {
            ButtonArray[x] = new Button();
            ButtonArray[x] .Tag = x;
            ButtonArray[x].Text = ("Button " + x);
            ButtonArray[x].Size = new Size(60, 20);
            ButtonArray[x].Location = new Point(20, ((x * 20) + 4));
            ButtonArray[x].Parent = this;
            ButtonArray[x].Visible = true;
            .Click +=  new System.EventHandler(this.ButtonHandler);
           
        }
    }
   
private void Button2_Click(object sender, System.EventArgs e)
{
    MessageBox.Show("Button number " + ((Button)sender).Tag + " was clicked");
}

vbturbo
0
 
LVL 18

Accepted Solution

by:
vbturbo earned 500 total points
ID: 20291720
sorry minor conversion error

        public void AddButtons() {
            for(int x=0;x<9;x++)
   {
            ButtonArray[x] = new Button();
            ButtonArray[x] .Tag = x;
            ButtonArray[x].Text = ("Button " + x);
            ButtonArray[x].Size = new Size(60, 20);
            ButtonArray[x].Location = new Point(20, ((x * 20) + 4));
            ButtonArray[x].Parent = this;
            ButtonArray[x].Visible = true;
            this.ButtonArray[x].Click += new System.EventHandler(this.ButtonHandler);
           
        }
    }

        public void ButtonHandler(object sender, System.EventArgs e)
        {
            MessageBox.Show(("Button number "
                            + (((Button)(sender)).Tag + " was clicked")));
        }
0
 
LVL 1

Author Comment

by:scogger1974
ID: 20291746
Exactly!!
I thought there was not the option to have an object Array in here but I guess I was wrong
Thanks..
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20291767
just glad it suited you, sorry for the many post's but im getting a bit rusty in c#
0
 
LVL 1

Author Comment

by:scogger1974
ID: 20291798
Its one thing to learn how to do something
but its another to learn how ask the right questions
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20291826
i suppose so -;)

thanks for the grade
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

Suggested Solutions

Title # Comments Views Activity
How to measure sizes and angles in scanned images ? 3 51
C# Json POSt to Rest API 4 32
Turn on intranet settings 1 36
Entity Framework 7 27
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

11 Experts available now in Live!

Get 1:1 Help Now