'UserRole'未声明。('UserRole' is not declared. It may be inaccessible due to its protection level)
我使用VB.NET和microsoft访问作为我的数据库,我是VB.NET的新手,我试图根据当前角色制作不同的访问级别。例如,登录后,Admin将进入主页面。用户登录后,会将其定向到其他页面。 我已经在我的数据库中设置了不同的角色,我在网上发现了一些编码来帮助我
但是我继续收到错误'UserRole'没有声明。 由于其保护级别,它可能无法访问。 我需要先声明UserRole吗? 如果是的话,我该怎么做? 任何帮助将不胜感激,谢谢:)
公共类Form1 Dim loginerror As String Dim UserRole As String公共函数login()Dim DBconn As New ADODB.Connection Dim user as New ADODB.Recordset
Dim Username As String Dim userDB As String Dim passDB As String Dim UserFound As Boolean DBconn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _ "Data Source = '" & Application.StartupPath & "\LoginDB.mdb'") user.Open("UserTable", DBconn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic) UserFound = False login = False Username = "Username = '" & txtuser.Text & "'" ' Do user.Find(Username) If user.BOF = False And user.EOF = False Then userDB = user.Fields("Username").Value.ToString passDB = user.Fields("Password").Value.ToString If userDB <> txtuser.Text Then user.MoveNext() Else UserFound = True If passDB = txtpass.Text Then UserRole = user.Fields("roles").Value.ToString user.Close() DBconn.Close() Return True Else loginerror = "Invalid Password" user.Close() DBconn.Close() Return False End If End If Else loginerror = "Invalid Username" user.Close() DBconn.Close() Return False End If Loop Until UserFound = True user.Close() DBconn.Close() Return False End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If login() = True And UserRole = "admin" Then adminwelcome.Show() Me.Close() ElseIf login() = True Then Welcome.Show() Me.Close() Else MessageBox.Show(loginerror, "Login Message") End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AcceptButton = Button1 Me.Show() Application.DoEvents() txtuser.Focus() End Sub Private Sub txtpass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.TextChanged End Sub Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click End Sub Private Sub txtuser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuser.TextChanged End Sub Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click End Sub Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click End Sub Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click End Sub
结束类
I am using VB.NET and microsoft access as my database, i am new to VB.NET and i tried to make different access levels depending on the current role.For example, After logging in, Admin will proceed to the main page And if users logs in, they will be directed to a different page. I have already set the different roles in my database, and i found some coding online to help me
However i keep getting the error 'UserRole' is not declared. It may be inaccessible due to its protection level. Do i need to declare UserRole first? if so, how do i do it? Any help would be much appreciated , thank you :)
Public Class Form1 Dim loginerror As String Dim UserRole As String Public Function login() Dim DBconn As New ADODB.Connection Dim user As New ADODB.Recordset
Dim Username As String Dim userDB As String Dim passDB As String Dim UserFound As Boolean DBconn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _ "Data Source = '" & Application.StartupPath & "\LoginDB.mdb'") user.Open("UserTable", DBconn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic) UserFound = False login = False Username = "Username = '" & txtuser.Text & "'" ' Do user.Find(Username) If user.BOF = False And user.EOF = False Then userDB = user.Fields("Username").Value.ToString passDB = user.Fields("Password").Value.ToString If userDB <> txtuser.Text Then user.MoveNext() Else UserFound = True If passDB = txtpass.Text Then UserRole = user.Fields("roles").Value.ToString user.Close() DBconn.Close() Return True Else loginerror = "Invalid Password" user.Close() DBconn.Close() Return False End If End If Else loginerror = "Invalid Username" user.Close() DBconn.Close() Return False End If Loop Until UserFound = True user.Close() DBconn.Close() Return False End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If login() = True And UserRole = "admin" Then adminwelcome.Show() Me.Close() ElseIf login() = True Then Welcome.Show() Me.Close() Else MessageBox.Show(loginerror, "Login Message") End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AcceptButton = Button1 Me.Show() Application.DoEvents() txtuser.Focus() End Sub Private Sub txtpass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.TextChanged End Sub Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click End Sub Private Sub txtuser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuser.TextChanged End Sub Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click End Sub Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click End Sub Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click End Sub
End Class
原文:https://stackoverflow.com/questions/17666765
最满意答案
此代码导致未定义的行为:
uint32_t *p = (uint32_t *)vector; uint32_t tmp = p[0];
vector
指向的内存是uint64_t
类型的对象,但是您可以通过uint32_t
类型的左值读取它。 这违反了严格的别名规则。由于你的程序总是调用这个函数,你的整个程序的行为是不确定的。 因此,当优化器删除导致UB的路径时,您可以期望看到奇怪的优化工件。
另一个问题是您使用
%lx
打印出uint64_t
。 除非您的系统使用64位长,否则这将不起作用。 要获得正确的说明符,请使用printf("%" PRIx64 "\n", vector);
。 你可能需要#include <inttypes.h>
。
修复printf格式说明符后,我的系统在
-O2
和更低的位置提供以下内容,或者使用-fno-strict-aliasing
开关:10000000000000a touch... 20000000000000a
并在
-O3
垃圾。This code causes undefined behaviour:
uint32_t *p = (uint32_t *)vector; uint32_t tmp = p[0];
The memory pointed to by
vector
is an object of typeuint64_t
, however you read it via an lvalue of typeuint32_t
. This violates the strict aliasing rule.Since your program always calls this function, the behaviour of your whole program is undefined. So you can expect to see strange optimization artifacts when the optimizer cuts out paths which lead to UB.
Another problem is that you are using
%lx
to print out auint64_t
. This won't work unless your system uses 64-bit longs. To get the right specifier, useprintf("%" PRIx64 "\n", vector);
. You may need#include <inttypes.h>
for this.
After fixing the printf format specifier, my system gives the following at
-O2
and lower, or if using the-fno-strict-aliasing
switch:10000000000000a touch... 20000000000000a
and garbage at
-O3
.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
GCC中的优化(Optimization in GCC)[2021-07-04]
唯一可以肯定的方法是使用-O2和-O3编译您的应用程序。 此外还有一些-O3包含的单独优化选项,您可以单独打开和关闭。 关于更大的二进制文件的警告,请注意,仅仅比较使用-O2和-O3编译的可执行文件大小在这里并不会有太大的好处,因为它是这里最重要的小型关键内部循环的大小。 你真的需要基准。 这将导致更大的可执行文件,但不应该有任何可衡量的放缓。 The only way to know for sure is to benchmark your application compiled with -O2 a ... -
此代码导致未定义的行为: uint32_t *p = (uint32_t *)vector; uint32_t tmp = p[0]; vector指向的内存是uint64_t类型的对象,但是您可以通过uint32_t类型的左值读取它。 这违反了严格的别名规则。 由于你的程序总是调用这个函数,你的整个程序的行为是不确定的。 因此,当优化器删除导致UB的路径时,您可以期望看到奇怪的优化工件。 另一个问题是您使用%lx打印出uint64_t 。 除非您的系统使用64位长,否则这将不起作用。 要获得正确的说明符 ...
-
让我们使用另一个问题的示例代码 。 编译,但告诉gcc不要组装: gcc -std=c99 -S -O2 test.c 现在我们来看看结果test.s文件中的_atoi函数(Mac OS 10.5上的gcc 4.0.1): .text .align 4,0x90 _atoi: pushl %ebp testl %eax, %eax movl %esp, %ebp movl %eax, %ec ...
-
要迂腐,有8种不同的有效-O选项,你可以给gcc,虽然有一些意思是一样的事情。 这个答案的原始版本有7个选项。 GCC从此增加了-Og ,总共达到8个 从手册页: -O (与-O1相同) -O0 (不要优化,如果没有指定优化级别,则为默认值) -O1 (最佳化) -O2 (优化更多) -O3 (优化甚至更多) -Ofast (非常积极地优化到突破标准合规性的程度) -Og (优化调试经验)-Og可以实现不干扰调试的优化,它应该是标准编辑 - 调试循环的优化级别,提供合理的优化级别,同时保持快速的编译和良好的 ...
-
(可能)你有一个func堆栈粉碎的可能性,它会覆盖调用者堆栈上的ptr 。 确保func任何数组或结构访问都保留在边界内。 如果不是因为我对gcc 3.x的错误记忆,我会说这几乎肯定是实际发生的事情。 即便如此,这也是最可能的解释。 在这一点上给你带来疑问的好处, 如果你的代码真的在你的问题中发布,那么是的,这可能是一个编译器错误: printf("BEFORE: %p\n", ptr); // ok if (func(ptr, x, y)) { continue; ...
-
导致分段错误的ICE(内部编译器错误)始终是编译器中的一个错误,因此它会要求您报告该错误。 您应该无法使用任何有效或无效的源代码崩溃编译器。 (有各种各样的事情可能发生,通常涉及拒绝编译无效代码,但崩溃不是其中之一。) 由于GCC 4.6.1是最新版本,为什么不考虑升级到更新版本的GCC(不是4.4.3版本就是旧版本)。 在提交错误报告之前,您应该尽量减少您的复制。 494行之后的所有内容可能都不重要; 幸运的是,您可以将第1行和第493行之间的材料从近500个减少到20个左右。 您当然应该尽可能减少它,同 ...
-
优化#define vs static const(在avr-gcc中)(Optimization of #define vs static const (in avr-gcc))[2023-02-08]
这取决于编译器的实现。 如果您从未将其地址和符号导出(对于C来说它是默认的,因此您必须使用static ),那么优化器应该启动并优化它。 对于简单的类型( int , float ),您可以合理地期望它可以在整个板上工作,但对于struct更好地检查一下编译器的功能。 对于像我的GCC那样的简单结构,优化它,消除结构并直接传递它的值,将常量加载到CPU寄存器。 对于较大的结构,它可能决定它是不值得的。 您可以生成要检查的代码的汇编列表: avr-gcc -O-S somefile.c 要么 gc ... -
GCC优化纯函数(GCC optimization of pure functions)[2022-03-06]
是否存在为接受指针参数的纯函数显式定义GCC行为的地方? 行为与不带指针参数的纯函数没有什么不同; 他们可以读取程序中的任何内存,但不能写入内存或执行IO。 通过将pure和const函数编写为inline,你已经非常困惑了。 如果函数体在调用站点可供编译器使用,它可以自己解决被调用函数执行的操作。 pure和const属性在函数体不可见的情况下最有用,因为它将在单独的编译单元中定义。 例如,考虑以下非纯,纯和const函数的集合: __attribute__((__pure__)) int a(); __ ... -
您确实在调用fgets(line, 80, fp) ,但目标数组line被定义为40 char的数组。 Amazon EC2上的编译器比您的配置更好或更智能。 如果文件包含超过39个字节的行,则这是一个真正的错误。 顺便提一下,在Mac上作为gcc调用的编译器通常是clang一个实例。 试试gcc --version 。 请注意,您的程序不会检查是否读取了太多数据,也不检查sscanf()转换是否成功。 两者都导致未定义的行为。 这是一个更安全的版本: int loadPriceData(const cha ...