Having trouble with getter/setter...
Page 1 of 1
Rayne01




Posts: 39

PostPosted: Thu, 31st Jul 2014 23:59    Post subject: Having trouble with getter/setter...
2x forms.

1x class.

Any suggestions as to why this doesn't show the name/age in form 2?

Code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Form_Class_Test
{
class Variables
{
private string name;
private int age;

public Variables()
{
}

public string Name
{
get { return this.name; }
set { this.name = value; }
}

public int Age
{
get { return this.age; }
set { this.age = value; }
}
}
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Form_Class_Test
{
public partial class Form1 : Form
{
Variables V1 = new Variables();
Form2 F2 = new Form2();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
V1.Name = textBox1.Text;
V1.Age = int.Parse(textBox2.Text);
this.Hide();
F2.ShowDialog();
}

private void button2_Click(object sender, EventArgs e)
{

}

private void button3_Click(object sender, EventArgs e)
{

}
}
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Form_Class_Test
{
public partial class Form2 : Form
{
Variables V1 = new Variables();

public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
label1.Text = V1.Name;

label2.Text = V1.Age.ToString();
}

private void label1_Click(object sender, EventArgs e)
{

}
}
}
Back to top
Rayne01




Posts: 39

PostPosted: Fri, 1st Aug 2014 00:30    Post subject:
Just to add, I can do this via constructor but not via properties as it happens.

EDIT: Using static variables within the variable class fixed my issue; due to the fact I was only wanting to refer to single instances of an object; this turned out perfectly.

I'm aware of the being thread-unsafe; I just don't know how to lock the thread.
Back to top
Lukxxx




Posts: 726

PostPosted: Fri, 1st Aug 2014 13:38    Post subject:
You use one instance of Variable class in Form1 and other instance in Form2.

You should pass same instance for it to work.

Like:

Variables V1 = new Variables();
Form2 F2;

public Form1()
{
InitializeComponent();
F2 = new Form2(V1);
}


public partial class Form2 : Form
{
Variables V1;

public Form2(Variables v)
{
InitializeComponent();
V1 = v;
}
}
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Fri, 1st Aug 2014 14:16    Post subject:
That is basically the same problem you had before where you insisted, that you were using the same instance, right?


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
dingo_d
VIP Member



Posts: 14555

PostPosted: Fri, 1st Aug 2014 17:23    Post subject:
Y u no use [code] tag?? Mad


"Quantum mechanics is actually, contrary to it's reputation, unbeliveably simple, once you take the physics out."
Scott Aaronson
chiv wrote:
thats true you know. newton didnt discover gravity. the apple told him about it, and then he killed it. the core was never found.

Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group