Error CS0103 when I try to export class

William O'Gorm 61 Reputation points
2021-04-12T21:10:45.79+00:00

Hello. I'm trying to export a class into another class, and I am getting error CS0103 in the receiving class. Much thanks in advance!

Windows Forms App (.NET Framework) C#

namespace DM
{
    public class RandomNameGenerator
    {
        public class CharacterExportTemplate
        {
            public string title;
            public string firstname;
            public string lastname;

            public string Title
            {
                get { return title; }
                set { title = value; }
            }

            public string Firstname
            {
                get { return firstname; }
                set { firstname = value; }
            }
            public string Lastname
            {
                get { return lastname; }
                set { lastname = value; }
            }
        }
        CharacterExportTemplate characterexport = new CharacterExportTemplate();
return characterexport;
}

This is the receiving class.

{
    public class RandomCharacterGenerator
    {
        string characterFilePath = ConfigurationManager.AppSettings["CharacterFilePath"].ToString();
        RandomNameGenerator randomnamegenerator = new RandomNameGenerator();
        int nm;
        public struct CharacterTemplate
        {
            public string firstname;
            public string lastname;
            public string title;
            public string background;
            public int st;
            public int con;
            public int dex;
            public int intel;
            public int wis;
            public int ch;
        }
        public CharacterTemplate CreateCharacter()
        {
            CharacterTemplate character = new CharacterTemplate();
            RandomNameGenerator namegenerator = new RandomNameGenerator();
            character.title = characterexport.title;                                                                         << The error is on these three lines,
            character.lastname = characterexport.firstname;                                                        <<  on characterexport.
            character.lastname = characterexport.lastname;

                                                     <<
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,841 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2021-04-13T02:50:07.093+00:00

    Hi WilliamOGorm-1523,
    You need to use full name of nested class(RandomNameGenerator.CharacterExportTemplate) to create a new instance of the nested class.
    As follows:

    public CharacterTemplate CreateCharacter()  
    {  
                CharacterTemplate character = new CharacterTemplate();  
                RandomNameGenerator.CharacterExportTemplate characterexport = new RandomNameGenerator.CharacterExportTemplate();  
                character.title = characterexport.title;  
                character.lastname = characterexport.firstname;  
                character.lastname = characterexport.lastname;  
                return character;  
    }  
    

    More details about nested types,please refer to this document.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful