【问题标题】:Error when passing variable or property to shared method将变量或属性传递给共享方法时出错
【发布时间】:2021-10-26 23:07:36
【问题描述】:

我有一个关于公共共享方法的查询,我有一个错误指示 BC30369:无法从共享方法中引用类的实例成员。

我有错误:

strUsuarioapp = ucase (user.identity.Name.split ("\") (0))

正好在userucase(user.identity.Name.split ("\") (0))这一行

返回我需要的数据(域用户),我尝试使用另一个 ucase(system.environment.usernane),但根据环境的不同,它给了我不同的值,而 user.identity.Name 并没有让我失望。

我了解错误引用但我不知道如何传递值,必须共享方法。

我无法从方法中删除共享,是否有任何代码可以将user.identity.Name 的值传递给它而不给我一个错误?代码:

public class frmview1
    inherits system.web.ui.page
    public pstruserApp as string

public property strUsuarioapp () as string
    get
      return pstruserapp
    end get
      set (value as string)
      pstrUsuarioapp = value
    end set
end property

public shared function usercall (byval name as string, byval namev as string) as string
   dim strUsuarioapp = ucase (user.identity.Name.split ("\") (0))
   ' Error here
   ' ....
   ' ....
    return strresult
end function

【问题讨论】:

  • 尝试使用System.Web.HttpContext.Current.User 而不是Page.User
  • page.user 有很多代码,如果可能的话,我想保持这样。谢谢。
  • 它们都返回相同的对象。 Page.User 只是HttpContext.Current.User 的快捷方式。由于您想从 Shared 方法访问 User 对象,因此您应该使用全局方法。您仍然可以使用 Page.User 作为实例方法。
  • 我认为应该是:DOMAIN 用户名的 Environment.Usename,但我不确定是访问该页面的用户还是发布网络的机器的域用户。

标签: asp.net .net vb.net


【解决方案1】:

将用户参数添加到您的函数中

pubic shared function usercall (byref System.Security.Principal.Principal user,  byval name as string, byval namev as string) as string
   dim strUsuarioapp = ucase (user.identity.Name.split ("\") (0))
   'here Err
....
......

return strresult
end function

【讨论】:

  • 如果其中一个参数具有显式类型,则所有参数都必须具有显式类型。谢谢。
猜你喜欢
  • 2012-02-11
  • 2017-09-10
  • 2016-04-20
  • 2013-05-18
  • 2014-04-06
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
相关资源
最近更新 更多