denotes 'field' where 'class' expected error

  • Thread starter Thread starter ChrisB
  • Start date Start date
C

ChrisB

Hello All:

I have created the following field within a class definition:

Class User
{
// fields
private RoleInfo roleInfo =
RoleInfo.GetRoleInfo(Thread.CurrentPrincipal.Identity.Name);

// remaining class definition
. . .
}

In a method defined in the User class, I attempt to invoke a method on the
roleInfo object:

// method in user class
public static User NewUser()
{
boolean Result = roleInfo.RolePrivilegeInfoCollection.Includes("User",
"Add")

// remaining method
. . .
}

When I attempt to compile the above method, I receive an error message
stating that "roleInfo denotes a field where a class was expected". I
notice that if RoleInfo is instantiated in the NewUser method instead of as
a field within the User class, the code compiles fine - but I would prefer
to only create one instance of RoleInfo that can be used throughout the User
class instead of creating an instance each time it is needed.

Any ideas on what may be causing this error?

Thanks!

Chris
 
Hi.

You are trying to access an instance field (roleInfo) from a static method.

I think you should declare the roleInfo field as static.

Have a nice day
GV
 
Your NewUser method is static and the field roleInfo is non-static. static
methods can only see other static methods and fields. I assume you meant
roleInfo to be static as well?

Regards
Lee
 
Gianluca Varenni said:
Hi.

You are trying to access an instance field (roleInfo) from a static method.

I think you should declare the roleInfo field as static.

Have a nice day
GV
 
Yes, that was the issue.

Thanks,
Chris

Gianluca Varenni said:
Hi.

You are trying to access an instance field (roleInfo) from a static method.

I think you should declare the roleInfo field as static.

Have a nice day
GV
 
Back
Top