question

WilliamOGorm-1523 avatar image
0 Votes"
WilliamOGorm-1523 asked WilliamOGorm-1523 commented

Error CS0103 when I try to export class

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
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That worked, Daniel. Thanks!

0 Votes 0 ·

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.