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?
Code
ItemRec.cs
Code
namespace Stores
Code
{ class ItemRec
Code
{ public int index;
Code
public string descr;
Code
public double price;
Code
//Constructor
Code
public ItemRec(int c_index, string c_descr, double c_price)
Code
{ index = c_index:
Code
descr = c_descr;
Code
price = c_price; }
Code
public int my_index { get {return index;} set {index = value;}}
Code
public string my_descr { get {return descrt;} set {descr = value;}}
Code
public double my_price {get {return price;} set {price = value;}}
theLizardRank #1,079 · 1.8K reputationPosted Dec 19, 2009, 10:03 PM EST
0
Why have you declared this
VB.NET
List ItemRecN = new List();
in both program.cs and form1.cs
If you start from an MT file how can you have 50 records to begin with?
If you have a file with50 records, then you load an array with the 50 records anddo what you need todothen delete the file and save a new one.
What do you mean by this
Because of the destructor I'm not able to do this within a Method. The data records will be destroyed and the endof the Method.
And why have you started extra threads when these relate to your original ListBox question?
You also need to decide onwhere your data is coming fromand going to, if its an sql type database, then you don't really
need to have a List ItemRecN = new List();
to load a grid, it could be databound if that's the way you want to go otherwise a List allows you to manage your
data from a binary file a lot easier.
Finally, there are a lot of learners on this forum, some answer questions when they donot know what they are talking about
just so their answer can be marked as'accepted' I have no need for my answers to be marked as accepted answers, if you learn something from my answer, that is good enough.
Just remember, in programming there are NO experts, we ALL have something to learn from someone.
Baily22 reputationPosted Dec 21, 2009, 1:21 PM EST
0
Hi theLizard,
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,
theLizardRank #1,079 · 1.8K reputationPosted Dec 21, 2009, 2:38 PM EST
0
Take the declaration out of program.cs, you do not need it there.
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 {
Code
public int index;
Code
public string descr;
Code
public double price;
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();
theLizardRank #1,079 · 1.8K reputationPosted Dec 19, 2009, 10:03 PM EST
List ItemRecN = new List(); in both program.cs and form1.cs If you start from an MT file how can you have 50 records to begin with? If you have a file with 50 records, then you load an array with the 50 records and do what you need to do then delete the file and save a new one. What do you mean by this 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. And why have you started extra threads when these relate to your original ListBox question? You also need to decide on where your data is coming from and going to, if its an sql type database, then you don't really need to have a List ItemRecN = new List(); to load a grid, it could be databound if that's the way you want to go otherwise a List allows you to manage your data from a binary file a lot easier. Finally, there are a lot of learners on this forum, some answer questions when they do not know what they are talking about just so their answer can be marked as 'accepted' I have no need for my answers to be marked as accepted answers, if you learn something from my answer, that is good enough. Just remember, in programming there are NO experts, we ALL have something to learn from someone.Baily22 reputationPosted Dec 21, 2009, 1:21 PM EST
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
theLizardRank #1,079 · 1.8K reputationPosted Dec 21, 2009, 2:38 PM EST
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
{
{
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?