tags:

views:

363

answers:

6

Hello,

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.

Thanks, Jay

A: 

Reflection maybe...

Tony
+1  A: 

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.

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

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.

EDIT: See code example by Konamiman.

Roman Boiko
+2  A: 

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
Konamiman
A: 

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...
}
Aaron
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
A: 

Jay,

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.

mqbt