I have one question about how to get and set in c #
I enter data in Page1 through Class1 and appears in Page2 but unachievable set values in Page1.
Page1
namespace textMuc
{
public partial class Page1 : PhoneApplicationPage
{
public Page1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Class1 cl = new Class1();
cl.Muc = 123;
}
}
Page2
namespace textMuc
{
public partial class Page2 : PhoneApplicationPage
{
public Page2()
{
InitializeComponent();
}
Class1 cl = new Class1();
private void Button_Click(object sender, RoutedEventArgs e)
{
txthien.Text = cl.Muc.ToString();
}
}
}
Class1
namespace textMuc
{
public class Class1
{
private int capdo = 100;
public int Muc
{
get { return capdo; }
set { capdo = value; }
}
}
}
What should I do?
Thank!!!
I have found your problem with static
namespace textMuc
{
public class Class1
{
private static int capdo = 100;
public int Muc
{
get { return capdo; }
set { capdo = value; }
}
}
}
Try keeping the capdo variable static...
namespace textMuc
{
public class Class1
{
private static int capdo = 100;
public int Muc
{
get { return capdo; }
set { capdo = value; }
}
}
}
Related
As you can see I instantiated the classes I need into the form_load, in order to use methods and classes features. The problem is that I need to Call the item NuovoCliente from CreateClientemethod, but I don't know how to do, since intellisense, even when I try to type, does not show any link to NuovoCliente.
The class you can see with the method is ClienteModel.
Which is basically structured like this:
public class ClienteModel
{
public int IDCliente { get; set; }
public string Cognome { get; set; }
public string Nome { get; set; }
public string Indirizzo { get; set; }
}
This is my method, which is placed in DBMemoryManager class:
public class DBMemoryManager : DBManager
{
//Array
ClienteModel[] MemoryClienti = new ClienteModel[0];
public int CreateCliente(ClienteModel model)
{
ClienteModel NuovoCliente = new ClienteModel();
int MaxCID = MemoryClienti.Select(ClienteModel => ClienteModel.IDCliente).Max();
MemoryClienti[0] = NuovoCliente;
NuovoCliente.IDCliente = MaxCID++;
return NuovoCliente.IDCliente;
}
This is how my Form start:
public partial class Form1 : Form
{
DBMemoryManager dbMemoryManager = null;
ClienteModel clienteModel = null;
OrdineModel ordineModel = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dbMemoryManager = new DBMemoryManager();
clienteModel = new ClienteModel();
ordineModel = new OrdineModel();
}
Return a ClienteModel from this method.
public ClienteModel CreateCliente(ClienteModel model)
{
ClienteModel NuovoCliente = new ClienteModel();
int MaxCID = MemoryClienti.Select(ClienteModel => ClienteModel.IDCliente).Max();
MemoryClienti[0] = NuovoCliente;
NuovoCliente.IDCliente = MaxCID++;
return NuovoCliente;
}
Now access data from Form_load
public partial class Form1 : Form
{
ClienteModel clienteModel = null;
OrdineModel ordineModel = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
clienteModel = new ClienteModel();
ordineModel = new OrdineModel();
DBMemoryManager dbMemoryManager = new DBMemoryManager(); //initialize here
ClienteModel nuovoCliente = dbMemoryManager.CreateCliente(clienteModel)
//here you can get all data from nuovoCliente
}
here is the data.
I want to access labelNummer.Text from my User Control in my form.
Form
if (idPartijen.Contains(labelNummer.Text))
{
con.SqlQuery("SELECT * FROM `kandidaat` where `partijnummer` =#nummer ");
con.Cmd.Parameters.Add("#nummer", MySql.Data.MySqlClient.MySqlDbType.VarString).Value = labelNummer.Text;
User Control
public string KandidaatNummer
{
set
{
Nummer = value;
labelNummer.Text = Nummer;
}
}
Your UserControl
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
}
private void MyUserControl_Load(object sender, EventArgs e)
{
}
public string KandidaatNummer
{
get
{
return labelNummer.Text;
}
set
{
labelNummer.Text = value;
}
}
}
Let's say that your user control has a name called: MyUserControl1
if (idPartijen.Contains(myUserControl1.KandidaatNummer)) { ... }
I can't figure out why MessageBox show "false" if nuovo.matrice refers to the same object but not maintain the array reassignment done by the method. Why nuovo.matrice == mat is false if they refers to the same object?
namespace WindowsFormsApplication15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class Class1
{
public ClassType[] matrice;
public class ClassType
{
public string a { get; set; }
public int b { get; set; }
}
}
Class1.ClassType[] mat;
private void Form1_Load(object sender, EventArgs e)
{
Class1 test = new Class1();
Class1.ClassType prova = new Class1.ClassType();
test.matrice = new Class1.ClassType[1];
test.matrice[0] = prova;
mat = test.matrice;
mat[0].a = "rtuier";
mat[0].b = 94;
Modify nuovo = new Modify(mat);
nuovo.inizia();
MessageBox.Show((nuovo.matrice == mat).ToString());
}
class Modify
{
public Class1.ClassType[] matrice;
public Modify(Class1.ClassType[] mat)
{
matrice = mat;
}
public void inizia()
{
matrice[0].a = "asuidh";
matrice[0].b = 123;
Class1.ClassType[] newMatrice = new Class1.ClassType[2];
Class1.ClassType ins = new Class1.ClassType { a = "pollo", b = 456 };
newMatrice[0] = matrice[0];
newMatrice[1] = ins;
matrice = newMatrice;
}
}
}
}
The problem is, they don't ref the same object.. because you cannot alter the mat variable in the class. You get a copy of a reference and you're altering the copy. If you want to be able to modify a reference, you should wrap it in a class. Then you'll get a copy of the wrapper reference, but the Class1 field is unique.
Class wrap example:
public class ClassType
{
public string a { get; set; }
public int b { get; set; }
}
public class Class1
{
public ClassType[] classType;
}
public class Wrapper
{
public Class1 WrappedClass1;
}
public class Class2
{
public Wrapper Wrapped;
public Class2(Wrapper wrapper)
{
Wrapped = wrapper;
}
public void ChangeClass1()
{
WrappedClass1.WrappedClass1 = new Class1();
}
}
Class1 class1 = new Class1();
Wrapper wrapper = new Wrapper();
wrapper.WrappedClass1 = class1;
Class2 class2 = new Class2(wrapper);
class2.ChangeClass1();
MessageBox.Show(wrapper.WrappedClass1 == class2.Wrapped.WrappedClass1); // <--- true
When I have a button click change the value of a label to the name of a class object, I get the error
"The name 'maxwell' does not exist in the current context"
Can anyone tell me how I can refer to the data members of an object that I have created? I want the dog that I created to be accessible across my whole application and by all of the buttons in my application.
Here is my code:
public partial class MainWindow : Window
{
public class Dog
{
string name;
int length;
public Dog(string nm)
{
name = nm;
}
}
public MainWindow()
{
InitializeComponent();
Dog maxwell = new Dog("Maxwell");
Dog fred = new Dog("Fred");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
LabelName.Content = maxwell.name;
}
}
}
You want to declare maxwell as a field in your class MainWindow:
public partial class MainWindow : Window
{
public class Dog
{
string name;
int length;
public Dog(string nm)
{
name = nm;
}
}
private Dog maxwell;
private Dog fred;
public MainWindow()
{
InitializeComponent();
maxwell = new Dog("Maxwell");
fred = new Dog("Fred");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
LabelName.Content = maxwell.name;
}
}
Here is more progressive approach.
Note that methods of the class have only access to the properties declared in the class scope/body
public partial class MainWindow : Window
{
// Inner Classes
public class Dog
{
private string name;
public string Name {
get{ return this.name };
set{ this.name = value}'
}
private int length;
public int Length {
get{ return this.length };
set{ this.length = value}'
}
public Dog( string _name )
{
this.name = _name;
}
}
// Properties
// For storing dogs
private Dicionary<string, Dog> Dogs;
// Methods
public MainWindow()
{
InitializeComponent();
// New dictionary of dogs
Dogs = new Dictionary<string, Dog>();
// adding Dogs objects
Dogs.add("maxwell", new Dog("Maxwell));
Dogs.add("fred", new Dog("Fred"));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
LabelName.Content = Dogs["maxwell"].Name;
LabelLength.Content = Dogs["maxwell"].Length;
}
}
I have created a class that has an automatic property for an int variable called BagsOfFeed. I added an automatic property to prevent the value from being changed from outside the class.
I use another property that calculates the BagsOfFeed only when the NumberOfCows property is set.
I have come across problem when I make BagsOfFeed readonly it prevents the NumberOfCows property from assigning a value to BagsOfFeed.
here is the class Code
namespace cow_calculator1
{
class Farmer
{
public Farmer(int numberOfCows, int feedMultiplier)
{
this.feedMultiplier = feedMultiplier;
NumberOfCows = numberOfCows;
}
public int BagsOfFeed { get { return BagsOfFeed; } }
private int feedMultiplier;
public int FeedMultiplier
{
get
{
return feedMultiplier;
}
}
private int numberOfCows;
public int NumberOfCows
{
get
{
return numberOfCows;
}
set
{
numberOfCows = value;
BagsOfFeed = numberOfCows * FeedMultiplier;
}
}
}
}
this is the form Code
namespace cow_calculator1
{
public partial class Form1 : Form
{
Farmer farmer;
public Form1()
{
InitializeComponent();
farmer = new Farmer(15, 30);
}
private void calculate_Click(object sender, EventArgs e)
{
Console.WriteLine("I need {0} bags of feed for {1} cows", farmer.BagsOfFeed, farmer.NumberOfCows);
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
farmer.NumberOfCows = (int) numericUpDown1.Value;
}
}
}
this is the error
Error 1 Property or indexer 'cow_calculator1.Farmer.BagsOfFeed' cannot be assigned to -- it is read only ( "Line 38" "Column 17" cow calculator1)
You can scope a setter to be private, and remove the return value.
public int BagsOfFeed { get; private set; }
Which is roughly equivalent to
private int bagsOfFeed;
public int BagsOfFeed {
get { return bagsOfFeed; }
private set { bagsOfFeed = value; }
}
Or in the second format you could just set the private backing store directly.