Jump to content

C# 2012 Get and Set de e para TxtBox


Recommended Posts

Posted

Boas ,

Sou novata em C# , e tenho a seguinte duvida relativamente ao seguinte problema :

Preciso de preencher as txtbox e ao clicar no botao Set ele ira quardar as valiaveis na minha Class > na propriedade depois tenho de clicar em outro botao Get e voltar a colocar os valores na txtbox(o que estao na class) ,

Tenho em Mainwindow.xaml.cs


Student newstudent;

	private void btnset_Click(object sender, RoutedEventArgs e)
	{
		 newstudent = new Student();

		txtstname.Text = newstudent.FirstName;
	}
//---->>>

private void btnget_Click(object sender, RoutedEventArgs e)
	{
		//Get Values

		newstudent.FirstName = txtstname.Text;
	 }


//E na Class Student.cs


	public String FirstName //property for manipulating First Name
	{
		get { return firstname; }
		set { firstname = value; }
	}

E nao esta a funcionar , alguem me pode ajudar a dizer que estou a fazer mal ...

Obrigado,

Darmendes

Posted

No codigo acima o resultado vira sempre vazio/nulo, pois no metodo get, um novo student é instanciado antes de ler o valor, então mesmo que um valor tenha sido atribuido anteriormente, ao criar uma nova instancia usando o "new Student" o valor atribuido anteriormente será perdido.

Fernando Lage Bastos - MCP/MCTS/MCPD

Posted

Boas,

seria algo assim?

public partial class MainWindow : Window
{
public class Student
{
	private string firstname;
	//
	public String FirstName
	{
		get { return firstname; }
		set { firstname = value; }
	}
}
Student student = new Student();


public MainWindow()
{
	InitializeComponent();
}

private void btnSet_Click_1(object sender, RoutedEventArgs e)
{
	student.FirstName = txtstname.Text;
}

private void btnGet_Click(object sender, RoutedEventArgs e)
{
	txtstname.Text = student.FirstName;
}
}
  • Vote 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.