I'm getting an error when I try to access a member class.
Here's the class:
class person
{
public class fullname
{
public string firstname;
public string lastname;
}
public double age;
public string interests;
public static int totalpeople;
public void greet()
{
Console.WriteLine("Hello, " + person.fullname.firstname + person.fullname.lastname);
Console.WriteLine("I am also " + age + "years old and like " + interests);
}
public static void displayTotalPeople()
{
Console.WriteLine("You have introduced a total of " + totalpeople + " people");
}
}
I'm trying to access the "fullname" class:
People[TotalPeople].fullname.firstname = FirstName;
People[TotalPeople].fullname.lastname = LastName;
fullname is underlined, and I read what Visual Studio reccommended to fix it, and that didn't work either.

Problem Accessing Member Class?
elmoman
That's not quite what Ivolved meant when he said to add m_fullname ... it needs to be outside of the fullname class and instantiated. Here's what he meant:
class person{
public class fullname
{
public string firstname;
public string lastname;
}
public fullname m_fullname = new fullname();
// etc.etc. with all the rest of the person class
dannyR
Thanks, but I still get errors. It says that there isn't a definition for "m_fullname", it says:
Error 5 An object reference is required for the nonstatic field, method, or property 'C_Sharp_For_Dummies.person.fullname.firstname' C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\C Sharp For Dummies\C Sharp For Dummies\Program.cs 68 43 C Sharp For Dummies
And there are more errors relating to it. Here is the edited class:
class
person{
public class fullname{
public string firstname; public string lastname; public fullname m_fullname;}
public double age; public string interests; public static int totalpeople; public void greet(){
Console.WriteLine("Hello, " + person.fullname.firstname + person.fullname.lastname); Console.WriteLine("I am also " + age + "years old and like " + interests);}
public static void displayTotalPeople(){
Console.WriteLine("You have introduced a total of " + totalpeople + " people");}
}
Paranth
Class declarations do not serve as members.
class person can have class fullname defined within it through its class declaration, but you still need a variable of type fullname to be the member of class person. This fullname member variable may need to be instantiated in a constructor for class person.
class person {
class fullname {
public string firstname = String.Empty;
public string lastname = String.Empty;
}
public fullname personfullname = new fullname();
// now you have a fullname instance, personfullname.firstname is accessible.
}
/*
I also recommend practicing the technique of using properties for class data:
private string firstname = String.Empty;
public string Firstname {
get { return this.firstname; }
set { this.firstname = value; }
}
This is accessible as personfullname.Firstname;
*/
ramdee
First you might want to consider declaring person and fullname as Structs instead of Classes. I won't get into the details of the differences between the two here. If you'd like to know more, feel free to send me an email.
Second, the reason you can't access the fullname class is that your person class hasn't declared a member variable of type fullname. So you want to add:
public fullname m_fullname;
To your person class. Then you can say:
People[TotalPeople-1].m_fullname.firstname=FirstName;
People[TotalPeople-1].m_fullname.lastname=LastName;
As a quick aside, have you ever done object oriented programming before Or are you just learning If you would like some suggestions for books to get you started...well actually if you have downloaded Visual C# Express, make sure to register it by going to help->register, and you will get a downloadable e-book that gives a good introduction to programming with C#.