Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
I have the following snippets of code

C#
public class Project
{
    public int nPom;
    public string sName;
    public int nTotPom;
    public int nExtraTime;
    public bool bSelected;
}


when the user enters a name into a text box I do the following

C#
public partial class AddProjectForm : Form
{
  public AddProjectForm()
  {
      InitializeComponent();
  }

  private void btnOkay_Click(object sender, EventArgs e)
  {
      Project Projects = new Project();
      Projects.sName = txtProjectName.Text;
      this.Close();
  }
}

I want to make an ArrayList of Projects. What I have so far is this

C#
public static class ProjectGlobals
{
    ArrayList ProjectArray = new ArrayList();
}


Since Projects is not accessible to ProjectGlobals how would I go about doing this?

[edit]Code blocks moved to only cover code fragments - OriginalGriff[/edit]
Posted
Updated 23-Mar-11 21:37pm
v3
Comments
Toli Cuturicu 24-Mar-11 9:13am    
For all of you! Stop using ArrayList! Think of them as deprecated for over 5 years! My God!
CathyT 24-Mar-11 22:18pm    
If it's "deprecated" then why is it supported by .Net 4.0?!

Since you have declared Project as public it is available to all classes that can reference the assembly / namespace.
C#
public class Project
{
...
}
There is nothing stopping you from using it in ProjectGlobals
C#
public static class ProjectGlobals
{
    List<Project> ProjectArray = new List<Project>();
}
(Note that I have replaces ArrayList with the generic List<>: they are very, very similar, but a List<Project>will only hold projects, and you do not need to cast the contecnts when you use them)

Your problem is a bit more fundamental:
C#
private void btnOkay_Click(object sender, EventArgs e)
{
    Project Projects = new Project();
    Projects.sName = txtProjectName.Text;
    this.Close();
}
The method Creates an instance of a Project, and assigns a name to it. It then closes the form it is in. This makes doubly sure that the instance is discarded as it is out of scope!.

I assume that you are trying to set up a dialog to get a name for a new project?
If so, you could either:
1) Return the Project name via a property and let the calling form create the project and add it to it's list.
2) Return a completed Project via a property.
3) Mess about with a static as you are.

I would go with the first option: That way the same dialog can be re-used to change the name of a project.
The route you are taking is very messy, and there are much simpler ways to do it:

Main Form:
private List<Project> Projects = new List<Project>();
...
AddProjectForm apf = new AddProjectForm();
if (apf.ShowDialog() == DialogResult.OK)
   {
   Projects.Add(new Project(apf.ProjectName));
   }
...
   }
AddProjectForm:
public string ProjectName 
   { 
   get { return txtProjectName.Text; }
   set { txtProjectName.Text = value }
   }
You do not need the ProjectGlobals class at all.
 
Share this answer
 
Comments
CathyT 24-Mar-11 22:14pm    
I guess I was using ProjectGlobals because I want to put the info in the Project Class into a ListView.
OriginalGriff 25-Mar-11 4:40am    
You can do that no matter where you store it: keep it relative to class that will use it (i.e. Main Form) that way you can have multiple lists if you need to at a later date. If you use a static list, you can only have one per application!
R. Erasmus 25-Mar-11 4:28am    
good answer!
I fixed many problems of your code in my Answer to your previous Question, why you repeat your mistakes?

OK. Don't use <code>ArrayList, it is obsolete, since generics were introduce. Use System.Collections.Generic.List<T> instead. Do you want all that problems with type casting?

Now, what's your problem with access modifiers. Make your list 1) a property (accessible field is a bad style), 2) make it internal (preferred) or public (only if you plan to access it from the other assembly).

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900