I am creating an online app, which required some C# code. So I created a new file name: User.cs, in that file I am having this code:
public class UserProperties {
public string hasPermission (string permission) {
// get the permissions of the user
var db = Database.Open("VMS");
var roles = db.Query("SELECT * FROM webpages_UsersInRoles
WHERE UserId =@0", WebSecurity.CurrentUserId);
// Get the Role Id
var getRole = "";
foreach (var row in roles) {
getRole = row.RoleId.ToString();
}
// Get the permission ID
var permissions = db.Query("SELECT * FROM Permissions WHERE
LOWER(Permission_Name) = LOWER(@0)", permission);
var permissionId = "";
foreach (var row in permissions) {
permissionId = row.Permission_Id.ToString();
}
// Now get the rolePermissions
var role_permissions = db.Query("SELECT * FROM Role_Permissions
WHERE Role_Id =@0 AND Permission_Id =@1", getRole, permissionId);
if(role_permissions.Count() == 0) {
return "Not Allowed.";
} else {
return "Yes, full permission.";
}
}
}
This code would look for the user's permissions and then return a value. But its not happening. When I try to execute it:
@{
new UserProperties.hasPermission("Create_Page");
}
It gives me an exception of:
CS0118: 'User.hasPermission(string)' is a 'method' but is used like a 'type'
I am not sure how am I using it as a type?
You need to use:
new UserProperties().hasPermission("Create_Page")
alternatively, you could make hasPermission
static, then you won't need to create an instace
public static string hasPermission (string permission) { ... }
@{
UserProperties.hasPermission("Create_Page");
}
The first line your code:
public class UserProperties
Defines a (object) Type.
The next line you declare an instance method.
public string hasPermission (string permission)
Unless you create an instance of UserProperties..
var userProperties = new UserProperties();
You can't call hasPermission()
.
You could alternatively (not recommended) create a static method.
public static string hasPermission (string permission)
All you need to do is add parentheses:
@{
new UserProperties().hasPermission("Create_Page");
}
new UserProperties()
creates an instance of the UserProperties
class, which you then call the hasPermission
method on.
If hasPermission
were defined as static
then your syntax would be valid (you don't need an instance to call the method on).
u will have to create an instance of UserProperties.
var up = new UserProperties();
up.hasPermission("Create_Page");
or just
new UserProperties().hasPermission("Create_Page");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.