I want to change my struct Patient to a class but when I do my program don't work(free of errors) I want to substitute the struct patient for class patientn, as you can see my button click uses the struct patient and I want to change it for a class and still work.
visual studio 10 My program:
public partial class Form1 : Form
{
int itemCountInteger;
public struct Patient
{
public string patientidstring;
public string firstNameString;
public string lastNameString;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public class Patientn
{
private int patientId;
public string firstName;
private string lastName;
public Patientn()
{
patientId = 0;
firstName = "";
lastName = "";
}
public Patientn(int idValue, string firstNameVal, string lastNameVal)
{
patientId = idValue;
firstName = firstNameVal;
lastName = lastNameVal;
}
}
//Array
Patient[] patientInfo = new Patient[10];
//this method is used to add items to array and display listbox
private void button1_Click(object sender, EventArgs e)
{
try
{
foreach (Patient patientinfoIndex in patientInfo)
patientInfo[itemCountInteger].patientidstring = textBox1.Text;
patientInfo[itemCountInteger].firstNameString = textBox2.Text;
patientInfo[itemCountInteger].lastNameString = textBox3.Text;
string names = patientInfo[itemCountInteger].firstNameString + " " + patientInfo[itemCountInteger].lastNameString;
listBox1.Items.Add(names);
itemCountInteger++;
listBox1.SelectedItem = names;
}
catch
{
MessageBox.Show("Contacts are limited to 20. Please delete some contacts prior to adding more.");
}
}
You should explicitly create class instances. In your case
// It's quite enough since Patient is a struct
Patient[] patientInfo = new Patient[10];
In case of Patientn that is class it should be
// As it was...
Patientn[] patientInfo = new Patientn[10];
// You should add this since Patientn is a class
for (int i = 0; i < patientInfo.Length; ++i)
patientInfo[i] = new Patientn();
Related
Im writing a code where you can search a name and the subjects teaches will pop-up etc..
however I'm not really sure why but i'm getting Object reference not set to an instance of an object error im missing something i know, can someone help me? i tried different methods didn't really work... heres my code :
public partial class MainWindow : Window
{
Course my = new Course();
public class Course
{
public string[] Name { get; set; }
public string[] Subject { get; set; }
public string[] Hour { get; set; }
public Course(string[] name, string[] subject, string[] hour)
{
this.Name = name;
this.Subject = subject;
this.Hour = hour;
}
}
public MainWindow()
{
InitializeComponent();
my.Name[0] = "Ali";
my.Name[1] = "Sefer";
my.Subject[0] = "INFORMATIKA";
my.Subject[1] = "ENGLISH";
my.Hour[0] = "12";
my.Hour[1] = "22";
}
private void searchButton_Click(object sender, RoutedEventArgs e)
{
Find();
}
private void Find()
{
int index = 0;
string wanted = wantedName.Text;
while (my.Name[index] != wanted && (my.Name[index] != "END"))
{
index++;
}
if (my.Name[index] == wanted)
{
outputLabel.Content = " " + my.Name[index] + " " + my.Subject[index];
}
else
{
outputLabel.Content = "Name not found";
}
}
}
}
You are using arrays without initializing them. While you have defined a constructor for your Course class that takes values for the arrays, you are using the default constructor. Try calling your own constructor with arguments like
Course my = new Course(new string[2], new string[2], new string[2]);
Before you can assign a value to an element like my.Name[0], you have to ensure that my.Name is referencing an allocated array, which means there is memory available for your elements.
I'm pretty new at this. Using Windows Forms in Visual Studio. I am to hammer out a store that has clothes, with stock that can be transferred in or out of the store.
I've gotten as far as to having a class, a list that contains the clothes and their quantities, and I've managed to get them into comboboxes. What I want to do now is to be able to 'buy' new quantities, changing the value in the list.
I'm stumped as to how to change the actual quantities, I'm sure I am missing stuff here.
This is my class:
public class Store
{
public string Clothing { get; set; }
public int Quantity { get; set; }
public Store(string c, int q)
{
Clothing = c;
Quantity = q;
}
And this is my current code:
}
public partial class Form1 : Form
{
List<Store> stock = new List<Store>
{
new Store ("Jeans size S", 1),
new Store ("Jeans size M", 3),
new Store ("Jeans size L", 5)
};
public Form1()
{
InitializeComponent();
}
private void bShow_Click(object sender, EventArgs e)
{
cbStockType.ValueMember = "Clothing";
cbStockType.DisplayMember = "Clothing";
cbStockType.DataSource = stock;
cbStockQnt.ValueMember = "Quantity";
cbStockQnt.DisplayMember = "Quantity";
cbStockQnt.DataSource = stock;
}
private void lblHighlightAdd_Click(object sender, EventArgs e)
{
}
private void bSlctClothing_Click(object sender, EventArgs e)
{
if (cbStockType.SelectedIndex < 0)
{ lblHighlightAdd.Text = "None"; }
else
lblHighlightAdd.Text = cbStockType.SelectedValue.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
string quantityToAdd = tbQntAdd.Text;
int add = Convert.ToInt32(quantityToAdd);
string addToStock = cbStockQnt.SelectedValue.ToString();
int newAmount = Convert.ToInt32(addToStock);
int result = newAmount + add;
foreach (var item in stock)
{
if (item.Clothing == cbStockType.SelectedValue.ToString())
{
item.Quantity = item.Quantity + result;
MessageBox.Show(cbStockQnt.SelectedValue.ToString());
}
}
}
}
}
If you can read this spaghetti junk, I'm stuck at getting the quantity of the selected piece of clothing to change. How do I get it to change the value both in the list and in the combobox?
Im having a problem in my listview because whenever I get value in other form it is not adding on the list but when I put breakpoint it have a value but still not adding on my listview.
here is my function in form1 getting values from datagridview
public void dataGridView1_DoubleClick(object sender, EventArgs e)
{
qtyOfOrders orders = new qtyOfOrders();
if (dataGridView1.SelectedRows.Count > 0)
{
String mealname = dataGridView1.SelectedRows[0].Cells[1].Value + String.Empty;
String price1 = dataGridView1.SelectedRows[0].Cells[2].Value + String.Empty;
pts.meal = mealname;
pts.qtyprice = Int32.Parse(price1);
orders.Show();
}
}
here is my function from form2 and will save data in listview in form1
private void OK_Click(object sender, EventArgs e)
{
cashier c = new cashier();
pricetempstorage pts = new pricetempstorage(); //class
int qty = Int32.Parse(QTYNumber.Text);
int totalPrice = qty * pts.qtyprice;
pts.qtynumber = qty;
pts.TotalPrice = totalPrice;
c.listView1.Items.Add(pts.meal);
c.qtyOrder.ListView.Items[0].SubItems.Add(pts.qtynumber.ToString());
c.price111.ListView.Items[0].SubItems.Add(pts.TotalPrice.ToString());
this.Hide();
}
this is my class
namespace jollibee4
{
class pricetempstorage
{
static int qtyNumber;
static int qtyPrice;
static int ListViewCount;
static String Meal;
static int totalprice;
public int TotalPrice
{
get
{
return totalprice;
}
set
{
totalprice = qtyNumber * qtyPrice;
}
}
public int qtynumber
{
get
{
return qtyNumber;
}
set
{
qtyNumber = value;
}
}
public int qtyprice
{
get
{
return qtyPrice;
}
set
{
qtyPrice = value;
}
}
public int listviewCount
{
get
{
return ListViewCount;
}
set
{
ListViewCount = value;
}
}
public String meal
{
get
{
return Meal;
}
set
{
Meal = value;
}
}
}
}
Try adding this code this.listView1.View = View.Details; After the c.listView1.Items.Add(pts.meal);
form1
public List<pricetempstorage> Items { get; private set; }
private void OK_Click(object sender, EventArgs e)
{
cashier c = new cashier();
pricetempstorage pts = new pricetempstorage(); //class
int qty = Int32.Parse(QTYNumber.Text);
int totalPrice = qty * pts.qtyprice;
pts.qtynumber = qty;
pts.TotalPrice = totalPrice;
Items.Add(pts);
this.Hide();
}
Create a shopping cart class where items can be append in list
I assume pricetempstorage is your class of item, its name can be product
public static ShoppingCart GetInstance()
{
if (cart == null)
{
cart = new ShoppingCart();
}
return cart;
}
protected ShoppingCart()
{
Items = new List<pricetempstorage>();
}
You have many architectural and stylistic issues with your program (use of static, capitalization, etc.)--to correct them all would require a very lengthy response.
Your code isn't working because you're creating a new instance of the cashier class, and then you're updating its listView1 object. What I think you're trying to do is update the listView object of Form2. So what you should be doing is grabbing a reference to Form2 and populating its ListView object in your OK_Click event handler...
Just a tip here: Public properties should have an initial capital letter. Your pricetempstorage class needs some work.
In the combo box I can get it to load all the conversions to the combo box. But, when I click a conversion from the combo box it changes the data to null for some reason. I am not sure if anyone can help me without looking at all my code. But I thought it was worth a shot. I am trying to select a conversion I.e feet to miles and have the "feet" be in the first label and the "miles" be in the second. Here is the code for the click event of the combo box:
public void CmboBxConversion_SelectedIndexChanged(object sender, EventArgs e)
{
//This is not working. If I were to say change the conversion.From to "hello world" the label text does change to hello world.
//But, as is it is it changes it to a null.
Conversion conversion = new Conversion();
Lbl1stConv.Text = conversion.From; //these are labels
LblSecondConv.Text = conversion.To;
}
//this is my Conversion class
public class Conversion
{
public string From { get; set; }
public string To { get; set; }
public decimal Multiplier { get; set; }
public string GetDisplayText(string sep)
{
return From + ("|") + To + ("|") + Multiplier;
}
public string GetCmboBxText(string sep)
{
return From + (" to ") + To;
}
}
//this is how I am loading the combo box
private void Conversion_Load(object sender, EventArgs e)
{
conversions = ConversionDB.GetConversions();
FillConversionComboBox();
}
//here is the method used to fill the combobox
private void FillConversionComboBox()
{
CmboBxConversion.Items.Clear();
foreach (Conversion c in conversions)
{
CmboBxConversion.Items.Add(c.GetCmboBxText("\n"));
}
}
//this is a data base class that the textfile is pulling data
public static class ConversionDB
{
public const string Path = #"..\..\Conversions.txt";
//public const string Path = Dir + "Conversions.txt";
public static List<ConversionList> GetConversions()
{
StreamReader textIn = new StreamReader(
new FileStream(Path, FileMode.Open, FileAccess.Read));
List<ConversionList> conversions = new List<ConversionList>();
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
string[] columns = row.Split('|');
ConversionList conversion = new ConversionList();
conversion.From = columns[0];
conversion.To = columns[1];
conversion.Multiplier = Convert.ToDecimal(columns[2]);
conversions.Add(conversion);
}
textIn.Close();
return conversions;
}
I'm not sure if this is what you want but it is a little simpler approach that gives you the same functionality. I think it will be much simpler if you bind the combobox to your list of conversions. The selecteditem and selectedvalue will be easier to work with.
public partial class Form1 : Form
{
private List<Conversion> conversions=new List<Conversion>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
loadconversions();
//foreach (Conversion c in conversions)
//{this.comboBox1.Items.Add(c); }
comboBox1.DataSource = conversions;
this.comboBox1.DisplayMember = "GetCmboBxText";
}
private void loadconversions()
{
fillconversions("feet","yards",3);
fillconversions("yard", "feet", 0.33m);
fillconversions("km", "mile", 0.54m);
fillconversions("mile", "km", 1.6m);
fillconversions("quarts", "gallons", 4);
}
private void fillconversions(string from, string to, decimal multiplier)
{
conversions.Add(new Conversion(from, to, multiplier));
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Conversion c = (Conversion)this.comboBox1.SelectedItem;
this.textBox1.Text = c.GetDisplayText;
}
}
public class Conversion
{
public string From { get; set; }
public string To { get; set; }
public decimal Multiplier { get; set; }
public Conversion(string from, string to, decimal multiplier)
{
From = from;
To = to;
Multiplier = multiplier;
}
public string GetDisplayText
{
get { return From + ("|") + To + ("|") + Multiplier; }
}
public string GetCmboBxText
{
get
{
return From + (" to ") + To;
}
}
}
I'm trying to learn List<> in C#. I made a test program that takes the result of three textboxes and inputs it in a multiline textbox, after I put them in a list (to later save the list to file).
Here is my class declaration:
public class Film
{
public Film (string Num, string Title, string Categ)
{
this.Numero = Num;
this.Titre = Title;
this.Categorie = Categ;
}
public string Numero;
public string Titre;
public string Categorie;
}
Now I instantiate the list:
List<Film> ListeFilms = new List<Film>();
And here is my event:
private void btSaveMovie_Click(object sender, EventArgs e)
{
var MyMovie = new Film(txtNum.Text, txtTitre.Text, cbCateg.Text);
ListeFilms.Add(MyMovie);
foreach (Film x in ListeFilms)
{
txtAffichage.AppendText(x.ToString());
}
}
Now when I run, all that is written in the text box is:
test_1.Form1+Film
What did I do wrong?
You have to override the ToString() method in the Film class declaration.Otherwise it returns the type name.
Example:
public class Film
{
public Film(string Num, string Title, string Categ)
{
this.Numero = Num;
this.Titre = Title;
this.Categorie = Categ;
}
public string Numero;
public string Titre;
public string Categorie;
public override string ToString()
{
return Numero.ToString() + " " + Titre.ToString() + " " + Categorie.ToString();
}
}
You just have to concatenate the three fields into the AppendText function:
private void btSaveMovie_Click(object sender, EventArgs e)
{
var MyMovie = new Film(txtNum.Text, txtTitre.Text, cbCateg.Text);
ListeFilms.Add(MyMovie);
foreach (Film x in ListeFilms)
{
txtAffichage.AppendText(x.Numero + " - " + x.Titre + "- " + x.Categorie));
}
}