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;
}
}
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 was trying to create a personal list using WinForms. I try to create a new entry via button click. I have a list of objects with string properties Name and Number.
How can I show the list of objects in my ListBox?
namespace sometestname
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Personallistshow(object sender, EventArgs e)
{
}
public void NewItemButton_Click(object sender, EventArgs e)
{
Personallist.Add(new Entrys() { Name = NameBox.Text, Number = Numbox.Text });
}
public List<Entrys> Personallist= new List<Entrys>();
}
public partial class Entrys
{
public string Name { get; set; }
public string Number { get; set; }
}
}
The user has 2 Textboxfields. If they click the NewItemButton, than create a new Entrys object. This new Entrys object should be added to Personallist object and ListBox should show the updated list.
List<Entrys> someList = ...;
Personallist.DataSource = someList;
You should use a bindinglist and set the datasourcebinding after initializing your form.
Something like:
public partial class Form1 : Form
{
public BindingList<Entrys> Personallist = new BindingList<Entrys>();
public Form1()
{
InitializeComponent();
comboBox1.DataSource = Personallist;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Number";
}
private void button1_Click(object sender, EventArgs e)
{
Personallist.Add(new Entrys() { Name = "TESTNAME", Number = "TESTNR" });
}
}
public partial class Entrys
{
public string Name { get; set; }
public string Number { get; set; }
}
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; }
}
}
}
I have some label that should display actual amount of items that contain BindingList that bound to the DataGridView.
I tried to bind in this way:
CountOfLoadedItemsLabel.DataBindings.Add("Text", _items.Count, String.Empty);
But when BindingList updates, the label that bound to its Count property not changes.
Never used BindingList<T> but this worked for me:
public partial class Form1 : Form
{
private BindingList<Test> list;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.list = new BindingList<Test>
{
new Test(1,"Entry"),
new Test(2,"Another Entry")
};
dataGridView1.DataSource = new BindingSource(list,null);
list.ListChanged += list_ListChanged;
list.Add(new Test(3, "After Binding"));
}
void list_ListChanged(object sender, ListChangedEventArgs e)
{
CountOfLoadedItemsLabel.Text = string.Format("Items: {0}", list.Count);
}
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public Test(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
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.