2

I have windows form with one textbox(Pname) and one button. I have one class by name Player with no constructor.

When user enters text inside textbox (e.g. John) and clicks button, system will create and an instance of a class Player with name of an object as John.

4
  • 1
    What is your question, exactly? How to make a new object based on a value in a String object? Commented Nov 26, 2009 at 19:06
  • 3
    What do you mean by a class with no constructor? How would you ever be able to instantiate an object of that type? Even using reflection, you still need to call a constructor. Commented Nov 26, 2009 at 19:10
  • 1
    Compiler will create a default constructor, if there are no other constructors Commented Nov 26, 2009 at 19:16
  • 3
    Well All classes have at least one constructor. So is it correct to assume you mean a class with only the default constructor? (parameterless) Commented Nov 26, 2009 at 19:16

6 Answers 6

3

Your question is not very clear but I interpret that you want your Player class to store a name, that will be supplied by the user.

In that case, you need to add a property of string type named Name to your Player class, and when the user clicks the button, you create a new instance of Player (you can even if you don't explicitly define a constructor), and set the property from the value of the textbox. For example, you would define the class like this:

class Player
{
    public string Name {get;set;}
    //Other members (if any)
}

and in the event handler for the button click (assuming your textbox is named theTextBox):

var player = new Player();
player.Name = theTextBox.Text;
//Do whatever you need with the instance of Player
Sign up to request clarification or add additional context in comments.

Comments

3

If there is no constructor at all, the compiler will create a default constructor (without parameters). You only need to create an instance of your class Player p = new Player(); and then assign the string with name you need to its property or field.

See code example by Konamiman.

Comments

1

Reflection maybe...

Comments

1

Why does the user care what the object is named internally? He's never going to use it.

One of the right ways of going about this would be to place the instance in a Dictionary<String, Object> so you can look it up later by the given name. There is no end-user reason to care what the variable name is internally.

2 Comments

I guess Jay meant not name of variable, but name stored inside object in a variable-the name of player.
Yeah, I was reading it more like he thought c# was a scripting language and he wanted the variable itself to have that name.
0

The Class:

public class Player
{
    public string Name { get; set;}
}

Button Click event:

private void button1_Click(object sender, EventArgs e)
{
    var player = new Player() {Name = Pname.Text};
    //do some thing with player...
}

1 Comment

Hello, Much appreciate all the answers. This is how used your suggestions- I created Dictionary<string,player> and used string to store user entered text and new object. I also learn that creating pbject name dynamically is not achievable. Yes, alternatively I could have set up the name property in Player class too. Thanks, Jay
0

In C#, unless the class is static, classes without constructors are given a public default constructor by the C# compiler in order to enable class instantiation.

So, although your class does not have a constructor, you can still instantiate it.

For example: Player myPlayer = new Player();.

Back to your case, you can declare a variable of type Player in your Form class and instantiate it as the above example, or just declare the variable and instantiate it in the Form's constructor, or Load event.

In the event handler for the button click, you then set the name of the Player instance.

myPlayer.Name = Pname.Text;

It depends on what you need, but I think newing the Player every time the button is clicked is kind of wasteful.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.