Currently I have several forms, and classes. I have one form MainForm handles the main window of my program. Inside this class, I have a static ArrayList of type "Room".
public partial class MainForm : Form
{
public static ArrayList rooms = new ArrayList();
...
}
On load, MainForm will read in a list of items from a text file, and create "Room" objects and store them in an arraylist.
public void loadSettings()
{
string path = System.IO.Path.Combine(Environment.CurrentDirectory, "config/Rooms.cfg");
string currentLine;
StreamReader read = new StreamReader(path);
while ((currentLine = read.ReadLine()) != null)
{
String[] parts = currentLine.Split(';');
string name = parts[0];
string video = parts[1];
string isLogged = parts[2];
string notify = parts[3];
rooms.Add(new Room(name, video, isLogged, notify));
}
The problem I am running into, is later on, I want to be able to manipulate this rooms array from another form, duely named "AddRoom", so I have done something like this:
//In AddRoom.cs
MainForm.rooms.Add(new Room(textBoxRoomName.Text));
MainForm.SaveAllRooms();
//In MainForm.cs
public static void SaveAllRooms()
{
TextWriter tw = new StreamWriter(Environment.CurrentDirectory + "/config/Rooms.cfg");
foreach(Room room in rooms)
{
tw.WriteLine(room.name + ";" + room.videoFeed + ";" + room.isLogged + ";" + room.notify);
}
tw.Close();
populateListOfRooms(); //THIS IS WHERE MY LOGICAL ISSUE IS
}
The problem is, in populateListOfRooms() I want to save the name of each Room in the arraylist rooms to an item in a Combobox.
Because of the fact that SaveAllRooms() and Arraylist rooms are both static, I can not access the specific instance of MainForm
in order to add items to the Combobox.
So I guess essentially my question is, since I can not use a static Arraylist object to store the room data, how can I go about storing these objects in a manner that is accessible from other forms, yet still allows me to access objects in the MainForm (like comboboxes).
I know this post was probably confusing, and definitely longer than it probably need have been, but it's late and so I apologize for any confusion.
VulpesPosted Mar 9, 2011, 6:09 PM
Suthish NairPosted Mar 12, 2011, 12:55 PM
Sam HobbsPosted Mar 9, 2011, 11:08 PM
I think a big problem that beginners have is that the C# (and VB) IDE emphasize forms too much. You are using the main form as the place to hold your data. It is better to use a form only as a UI. I am not sure how it would help you to separate your data from the form, but it is likely that you should do that. Your data that is to accessed from everywhere in your application should be in an object that is not in a form. Does that make sense?
There is a methodolgy in which applications are split into three "tiers"; a Data Access Layer (DAL), a Business Logic Layer (BLL) and I forget what the third is but it is the UI layer. So the important thing is that experienced developers separate the UI from the data. In other words, a form should be a way to show the data and provide an interface between people and the computer but the data should live as a separate object. A form is like an office but people should have a life elsewhere.
VulpesPosted Mar 9, 2011, 6:45 PM
1. In MainForm.cs:
// add this static field
internal static MainForm Me;
// and change the constructor to set this field:
public MainForm()
{
InitializeComponents();
Me = this;
}
// in AddRoom.cs, you can now do:
MainForm mf = MainFrom.Me;
mf.rooms.Add(new Room(textBoxRoomName.Text));
mf.SaveAllRooms();
2. //In AddRoom.cs change the constructor to accept an argument of type MainForm and store it in a private field
private MainForm mf;
public AddRoom(MainForm mf)
{
InitializeComponents();
this.mf = mf;
}
// you can now simply do:
mf.rooms.Add(new Room(textBoxRoomName.Text));
mf.SaveAllRooms();
When you construct AddRoom from MainForm, you'll need to do:
AddRoom af = new AddRoom(this);
David RichardsonPosted Mar 9, 2011, 6:37 PM
VulpesPosted Mar 9, 2011, 6:21 PM
David RichardsonPosted Mar 9, 2011, 6:15 PM
So I guess my question is does: MainForm mf = (MainForm)Application.OpenForms["MainForm"]; create a new instance of MainForm, or access a pre-existing instance? Since the arraylist is in MainForm if It creates a new instance it doesn't help me unless the arraylist is static.
Thank you for your quick reply.
- David