scogger1974
asked on
Access an Object using a Variable
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").visib le=false
That is the kind of Functionality I am looking for.
Thanks
Non Working Example:
TheObject("Button4").visib
That is the kind of Functionality I am looking for.
Thanks
ASKER
I guess the Second Part of this Question would come in ...
Could I also Name an Object Based on a string....
Could I also Name an Object Based on a string....
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.UseVisualStyl
this.Button1.Visible = false;
this.Controls.Add(this.But
ASKER
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(But tonA);
this.Controls["ButtonA"].V isible = false;
Here is the error it returns
Object reference not set to an instance of an object.
// 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(But
this.Controls["ButtonA"].V
Here is the error it returns
Object reference not set to an instance of an object.
ASKER
vbturbo:
I know I can chage the Name but I want to be able to use that name to access the Object.
I know I can chage the Name but I want to be able to use that name to access the Object.
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(B uttonA);
this.Controls.Add(ButtonA) ;
}
vbturbo
{
Button ButtonA = new Button();
ButtonA.Location = new Point(7, 146);
ButtonA.Size = new Size(94, 23);
ButtonA.Text = "ADD ITEM";
//groupBox4.Controls.Add(B
this.Controls.Add(ButtonA)
}
vbturbo
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.Ad d(ButtonA) ;
//this.Controls.Add(Button A);
}
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.Ad
//this.Controls.Add(Button
}
and use for docking the button to always be inside the groupbox
ButtonA.Location = new Point(this.groupBox1.Top + 10, this.groupBox1.Left + 20);
ButtonA.Location = new Point(this.groupBox1.Top + 10, this.groupBox1.Left + 20);
ASKER
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").Visib le = false;
although this doesnt work
Button "SomeString"= new Button();
and then be able to do
Object("Somestring").Visib
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.B uttonHandl er);
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Button number " + ((Button)sender).Tag + " was clicked");
}
vbturbo
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.B
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Button number " + ((Button)sender).Tag + " was clicked");
}
vbturbo
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Exactly!!
I thought there was not the option to have an object Array in here but I guess I was wrong
Thanks..
I thought there was not the option to have an object Array in here but I guess I was wrong
Thanks..
just glad it suited you, sorry for the many post's but im getting a bit rusty in c#
ASKER
Its one thing to learn how to do something
but its another to learn how ask the right questions
but its another to learn how ask the right questions
i suppose so -;)
thanks for the grade
thanks for the grade
this.Controls["Button4"].V