Link to home
Start Free TrialLog in
Avatar of jeremyduj
jeremyduj

asked on

Identifier expected error

I can't compile a program - it keeps saying identifier expected. This is my code:

namespace CircuitTRK {
   public struct UserStruct
   {

       public static string firstname;
       public static string lastname;
       public static string username;
       public static bool logout;

       public UserStruct(firstname, lastname, username, logout)
       {
         this.firstname = firstname;
         this.lastname = lastname;
         this.username = username;
         this.logout = logout;
       }

   }

}
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Hi jeremyduj,
----------

not sure if i understand what you are trying to do but you could read up on structs in C# here
source: http://www.c-sharpcorner.com/Language/StructuresInCSRVS.asp

if i'm correctly interpreting you need to write it like

namespace CircuitTRK {

   public struct UserStruct
   {
       public string firstname;
       public string lastname;
       public string username;
       public bool logout;
   }

// use it in a class like
   class MyClient
   {
            public static void Main()
            {
                        MyStruct ms = new UserStruct();
                        ms.firstname = "first";
                        ms.lastname = "last";
                        ms.username = "lastfirst";
                        ms.logout = false;
                        // rest of code
              }
   }
}

but i could be wrong just comment if so

----------
bruintje
share what you know, learn what you don't
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PoeticAudio
PoeticAudio

public UserStruct(firstname, lastname, username, logout)
       {
         this.firstname = firstname;
         this.lastname = lastname;
         this.username = username;
         this.logout = logout;
       }

This is your problem. You are saying "this" when "this" represents an instance of the class whereas static variables are classifier variables (ie apply to all instances, stored at the class level not the instance level)

Are you sure that you want to use static variables? Whenever you create instances of this, all instances will have the same firstname, lastname, username...etc because those variables are static. So if you create an instance with the name "john" then all instances of that struct will have the name "john" because firstname is static.

If that's what you're after then you will have to do something like:

public struct UserStruct
   {

       public static string user_firstname;
       public static string user_lastname;
       public static string user_username;
       public static bool user_logout;

       public UserStruct(firstname, lastname, username, logout)
       {
         user_firstname= firstname;
         user_lastname= lastname;
         user_username= username;
         user_logout= logout;
       }

   }



Sorry, this would be the correct implementation. (you didn't specify the types in your constructor either, which is another problem)

public struct UserStruct
   {

       public static string user_firstname;
       public static string user_lastname;
       public static string user_username;
       public static bool user_logout;

       public UserStruct(string firstname, string lastname, string username, bool logout)
       {
         user_firstname= firstname;
         user_lastname= lastname;
         user_username= username;
         user_logout= logout;
       }

   }
You should Use

public struct UserStruct
{
public static string str_firstname;
public static string str_lastname;
public static string str_username;
public static bool str_logout;

public UserStruct(string firstname,string lastname,string username,bool logout)
{
str_firstname = firstname;
str_lastname = lastname;
str_username = username;
str_logout = logout;
}
}

this is related to an instance of class and static variables are shared among all the instances of class..