-2

I have a button that by clicking it, a new object is constructed by the name of c1:

private void addbtn_Click(object sender, EventArgs e)
{
  Contact c1 = new Contact(add_name_txtbx.Text,add_num_txtbx.Text);
}

I want to set the name of the new object (c1) using the text entered in a textbox.

something like:

private void addbtn_Click(object sender, EventArgs e)
{
  Contact add_name_txtbx.Text = new Contact(add_name_txtbx.Text,add_num_txtbx.Text);
}

Is it possible? How should I do it?

this is the class:

class Contact
{
    public string _name;
    public string _number;

    public void setName(string Name)
    {
        _name = Name;
    }

    public string getName()
    {
        return _name;
    }

    public void setNumber(string Number)
    {
        _number = Number;
    }

    public string getNumber()
    {
        return _number;
    }

    public Contact(string Name, string Number)
    {
        _name = Name;
        _number = Number;
    }
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • 1
    please share the source code of Contact class – ashin Jun 28 '17 at 05:24
  • You want to create an object of class with the same name as the text of text box? Or your object have a name property and you want to set it to the text of textbox? First one you can't do. Can you tell what is use case which forces you to do something like this ? – Chetan Jun 28 '17 at 05:27
  • 2
    Your first code snippet appears to pass the `add_name_txtbx.Text` value to the `Contact` constructor already, which presumably sets the name as you want. Your second code snippet is meaningless and impossible to comprehend. Please fix your question so it includes a good [mcve] that shows clearly what you're trying to do, with a detailed explanation of what the code does now and what you want it to do instead. This will be especially useful, given that you seem to be having trouble communicating your goal in English. – Peter Duniho Jun 28 '17 at 05:31
  • You cannot give dynamic variable name to instance of object. It is syntax error – sangram parmar Jun 28 '17 at 05:31
  • i want to do the first thing you mentioned. the problem is that i want to add objects to a combobox. if i add the object itself combobox shows Contacts.Contact but i want the name of the contact ... so i added c1.name to the combobox. but i could not reach the object of name added to the combobox. i wanted to name the object as the entered name so i could have them in combobox... – Ehsan Jafari Jun 28 '17 at 05:36
  • 1
    You need to read about how to bind combobox and set its valuemember and displaymember properties of it. You don't use get and set methods for properties in C#. That's java syntax. – Chetan Jun 28 '17 at 05:43
  • the problem is solved. it is impossible to do name the object using text in the textbox. thanks everyone. – Ehsan Jafari Jun 28 '17 at 05:43
  • This one can help you..https://stackoverflow.com/q/752/1660178 – sangram parmar Jun 28 '17 at 05:44
  • You're welcome. Don't forget to mark the question as answered. – John Wu Jun 28 '17 at 06:17

1 Answers1

1

I am not quite clear what the issue is. In this example I create a Contact and set its name to the contents of a textbox named add_name_txbx.

Contact c = new Contact();
c.setName(add_name_txbx.Text);

Are you asking if somehow "c" can be replaced with the name in the textbox? The answer is no, c is a symbol that the developer (you) chooses at compile time, and the contents of the textbox are not known till runtime.

John Wu
  • 50,556
  • 8
  • 44
  • 80