L.S.,
For my program I've to update a file. This can be done at two ways:
1. You load the last file and update it.
2. You start with a MT file and start from scratch.
I want to do the last, but my MT file consists of 50 "category" records, which have to be loaded in the array.
Because of the destructor I'm not able to do this within a Method. The data records will be destroyed and the end of the Method.
How can I achieve this?
ItemRec.cs
namespace Stores
{ class ItemRec
{ public int index;
public string descr;
public double price;
//Constructor
public ItemRec(int c_index, string c_descr, double c_price)
{ index = c_index:
descr = c_descr;
price = c_price; }
public int my_index { get {return index;} set {index = value;}}
public string my_descr { get {return descrt;} set {descr = value;}}
public double my_price {get {return price;} set {price = value;}}
|
Program.cs
List ItemRecN = new List();
Application.Run (new Form1());
|
Form1.cs
public partial class Form1: Form
{ list ItemRecN = new ();
]
public Form1()
{}
private void FileNew_Click(......................)
{ ItemRecN.Add(new Stores.ItemRec(0,"Stationary", 0.00));
ItemRecN.Add(new Stores.Itemrec(1,"Books ", 0.00));
etc.
etc.
}
|
Thanks and Regards,
Baily
theLizardPosted Dec 21, 2009, 7:38 PM
Place the declaration before any forms constructor, this will make it available in the scope of the form, any method will be able to access it.
Putting the declaration in a method makes it visible only to the method.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace yourNameSpace
{
public class RecordItem
{
public int Index
{
get{return(index);}
set{index = value;}
}
}
//--------------------------------------------------------------------------------------
public class RecordItems : List
{
public RecordItem AddItem
{
set{this.Add(value);
}
}
//--------------------------------------------------------------------------------------
public RecordItems Index(int i)
{
return(this[i].index);
}
}
}
Create a class using the above example then all you have to do is this
using yourNameSpace
//before form constructors
public RecordItem rec = new recordItem();
public RecordItems recItems = new RecordItems();
//declaring them public exposes them to ANY form that has access to the form so if you have, say form1 and form2, you do not need to declare them in form 2 you can call them from form 2 with form1.rec = new RecordItem();
//form1.rec.index = 12345; etc...
in your methods
rec.desc = "whatever"
rec.index = nextIndex;
rec.price = 345.00;
or in the class have a
public void AddToList(RecordItems items, int i, string s, double d)
{
items.Add(new recordItem());
items[items.Count-1].index = i;
items[items.Count-1].desc = s;
items[items.Count-1].price = d;
}
then to add to list
recItems.AddItem(rec);
or
recItems.AddToList(recItems, 1, "Hello New Item" 345.88);
and thats it.
to read through the list
for(int i =0; i < RecordItems.Count; i++)
{
targetvar = recItems[i].index;
targetvar = recItems[i].desc;
targetvar = recItems[i].price;
}
I still do not understand what you mean by MT records,
Do you want to create new ones each time or modify existing records for the tank?
BailyPosted Dec 21, 2009, 6:21 PM
To answer your questions:
Why in both programs? Without I get the following error: the name ItemRecN does not exist in the current file.
What am I doing wrong? The List ..... has been declared in program.cs.
Within program.cs form1.cs will be called and within form1.cs another form will be called for displaying the DataGridView.
Can I make the following Method within the class?
public void NewRecord()
{ ItemRecN.Add(new ItemRec(0, "tank1",9.98)):
ItemRecN.Add(new ItemRec(1,"tank2",8.54)):
etc. etc. etc.
}
What I meant regarding the destructor is that, according to my information, the variables declared within a Method will be automatically deleted at the end of the Method by the destructor. I've tried to fill the array within a Method and it did not work.
Please advise?
The MT records is because a number of commodities are stored in tanks, every tank has a identification code, a maximum sounding value etc. These values are stored in the 50 "MT" records.
Thanks and Regards,
Baily
theLizardPosted Dec 20, 2009, 3:03 AM