Darmendes Posted October 13, 2013 at 11:04 AM Report #528742 Posted October 13, 2013 at 11:04 AM 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
petvetbr Posted October 13, 2013 at 12:04 PM Report #528745 Posted October 13, 2013 at 12:04 PM 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
Darmendes Posted October 13, 2013 at 01:29 PM Author Report #528752 Posted October 13, 2013 at 01:29 PM Entao e qual seria a solucao ? Obrigado.
nelsonr Posted October 13, 2013 at 02:03 PM Report #528760 Posted October 13, 2013 at 02:03 PM 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; } } 1 Report
Darmendes Posted October 13, 2013 at 02:12 PM Author Report #528763 Posted October 13, 2013 at 02:12 PM Obrigado ... Entretanto tinha ja tinha conseguido chegar la mas e isso mesmo ... obrigado 🙂
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now