Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I have a class defined as follows
C#
public class Project
{
    public class ProjectListClass
    {
        public int nPom;
        public string sName;
        public int nTotPom;
        public int nExtraTime;
        public bool bSelected;
    }
}

in another class when a person enters a name into a text box I want to put that name into an instance of the above class. So what I have so far is
Project Projects = new Project();

Now, how do I add the name that's been entered by the user into sName in the Projects class?
Posted
Updated 23-Mar-11 21:02pm
v2

You will need to instantiate the ProjectListClass class as well.
You will then set the sName value to the text value.

Just as an addition, you dont need two classes (inner and outer) here.
Just have one class with properties or you can have two classes but one of the would be a property inside the other class.
 
Share this answer
 
Comments
CathyT 24-Mar-11 3:10am    
Right, I didn't even see that I had two classes there. Not sure what I was thinking. Thanks!
Abhinav S 24-Mar-11 3:19am    
No problem.
Espen Harlinn 24-Mar-11 3:37am    
My 5
Abhinav S 24-Mar-11 4:02am    
Thanks Espen.
TweakBird 24-Mar-11 4:05am    
My 5
MIDL
public class Project
{
    public class ProjectListClass
    {
        public int nPom;
        public string sName;
        public int nTotPom;
        public int nExtraTime;
        public bool bSelected;
        public ProjectListClass(string name)
        {
            sName = name;
        }
    }
    public ProjectListClass lstClass;
    public Project(string name)
    {
        lstClass = new ProjectListClass(name);
    }
}
void button_Click(object sender, EventArgs e)
{
    Project pro = new Project("Hello");
    MessageBox.Show(pro.lstClass.sName);
}

or simpler:
C#
public class Project
{
   public int nPom;
   public string sName;
   public int nTotPom;
   public int nExtraTime;
   public bool bSelected;
   public Project(string name)
   {
    sName = name;
   }
}
void button_Click(object sender, EventArgs e)
{
    Project pro = new Project("Hello");
    MessageBox.Show(pro.sName);
}

or:
C#
public class Project
{
   public int nPom;
   public string sName;
   public int nTotPom;
   public int nExtraTime;
   public bool bSelected;
}
void button_Click(object sender, EventArgs e)
{
    Project pro = new Project();
    pro.sName = "Hello";
    MessageBox.Show(pro.sName);
}
 
Share this answer
 
v2
Comments
Abhinav S 24-Mar-11 3:14am    
Good solution. 5.
Espen Harlinn 24-Mar-11 3:36am    
Nice effort, my 5
TweakBird 24-Mar-11 4:06am    
My 5
First, fields of the class should be private. For access to the instance from the our-of-class code better use properties.

Secondly, public is only needed if you want to have access from another assembly. In other cases, use internal (or internal protected).

Think about what properties should be read-only. You can initialize them from constructor. It includes your sName.

Do not violate Microsoft naming conventions. Don't abbreviate. Worst thins is your prefixes. Type are all in meta-data, no need to prefix with type. The idea came from C, but C# is rather opposite to C in main ideas.

C#
public class Project
{
    public class ProjectListClass
    {
        public int Pom { get; set; } //give sensible name
        public string Name { get; set; }
        public int TotPom { get; set; } //give sensible name
        public int ExtraTime { get; set; }
        public bool IsSelected { get; set; }
    }
}

//Project Projects = new Project();
//Same name for the variable and class is perfectly fine and often recommended:
Project Project = new Project(); //why it was plural? It wasn't array...

//...

//Here, Project is instance, not class:
Project.Name = MyForm.textBoxName.Text;


Any questions?

—SA
 
Share this answer
 
Comments
Abhinav S 24-Mar-11 3:14am    
Good answer. 5.
Sergey Alexandrovich Kryukov 24-Mar-11 3:16am    
Thank you, Abhinav.
--SA
Espen Harlinn 24-Mar-11 3:33am    
Good reply, my 5
Sergey Alexandrovich Kryukov 24-Mar-11 3:38am    
Thank you, Espen.
--SA
TweakBird 24-Mar-11 4:06am    
My 5

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