public class myClass { public static string myString; }使用时: 命名空间.myClass.myString = ".....";
using system; 在这里定义的变量就可以在整个程序中使用; int a; public class main { 在这里定义的变量就可以在整个类中使用; int b; public void static Main() { 在这里定义的变量就可以在整个方法中使用; int c; } }也可以用static实现!
public class user { private static string userName;
public user(string name) { userName=name;
}
public static string UserName { get { return userName; } } } }
登陆按钮: —————————————————————————————— Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick Dim conna As SqlConnection = New SqlConnection("server='localhost';uid=sa;pwd=;database=pqtax") Dim mbfa As String = "select * from yhb where username='" & Trim(username.Value) & "' and password='" & Trim(Password.Value) & "'" Dim mycoa As SqlCommand = New SqlCommand(mbfa, conna) Dim testreada As SqlDataReader conna.Open() testreada = mycoa.ExecuteReader() If testreada.Read Then Session("user") = testreada("username") Session("pass") = testreada("password") Session("zsjg") = testreada("zsjgbm") Session("qx") = testreada("qx") testreada.Close() conna.Close() mycoa.Dispose() Else testreada.Close() conna.Close() mycoa.Dispose() Response.Write("<script language='javascript'>alert('用户名或口令错误! ')</script>") Return End If Response.Redirect(ym) End Sub _________________________________________________________________________ 其它页面,当然了,我这是个小程序,没有继续到数据库验证———————————————————————————————————— Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here If Session("user") = Nothing Or Session("pass") = Nothing Or Session("zsjg") = Nothing Or Session("qx") = Nothing Then Response.Redirect("login.aspx?ym=fpgr.aspx") End If If Session("qx") <> "2" Then Response.Write("<script language='javascript'>alert('抱歉,您不是发票管理员! ');window.open('login.aspx?ym=fpgr.aspx','_self');</script>") End If webuser.Text = Session("user") End Sub ————————————————————————————————————
public static string sPassword = "";在别的类中可以直接访问:名称空间.类名.sUserID
名称空间.类名.sPassword
二,网页中用session变量。
{
public static string myString;
}使用时:
命名空间.myClass.myString = ".....";
在这里定义的变量就可以在整个程序中使用;
int a;
public class main
{
在这里定义的变量就可以在整个类中使用;
int b;
public void static Main()
{
在这里定义的变量就可以在整个方法中使用;
int c;
}
}也可以用static实现!
看你写什么应用?
windows应用程序还是web的了,
web下的全局变量可以定 application("youname")=XXX
下面的例子定义了一个用户类,初始化用户名后,在filetobase命名空间的其它类都可以获得用户名
namespace FileToBase{
public class user
{
private static string userName;
public user(string name)
{
userName=name;
}
public static string UserName
{
get
{
return userName;
}
}
}
}
——————————————————————————————
Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
Dim conna As SqlConnection = New SqlConnection("server='localhost';uid=sa;pwd=;database=pqtax")
Dim mbfa As String = "select * from yhb where username='" & Trim(username.Value) & "' and password='" & Trim(Password.Value) & "'"
Dim mycoa As SqlCommand = New SqlCommand(mbfa, conna)
Dim testreada As SqlDataReader
conna.Open()
testreada = mycoa.ExecuteReader()
If testreada.Read Then
Session("user") = testreada("username")
Session("pass") = testreada("password")
Session("zsjg") = testreada("zsjgbm")
Session("qx") = testreada("qx") testreada.Close()
conna.Close()
mycoa.Dispose()
Else
testreada.Close()
conna.Close()
mycoa.Dispose() Response.Write("<script language='javascript'>alert('用户名或口令错误! ')</script>")
Return
End If
Response.Redirect(ym) End Sub
_________________________________________________________________________
其它页面,当然了,我这是个小程序,没有继续到数据库验证————————————————————————————————————
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Session("user") = Nothing Or Session("pass") = Nothing Or Session("zsjg") = Nothing Or Session("qx") = Nothing Then
Response.Redirect("login.aspx?ym=fpgr.aspx")
End If
If Session("qx") <> "2" Then
Response.Write("<script language='javascript'>alert('抱歉,您不是发票管理员! ');window.open('login.aspx?ym=fpgr.aspx','_self');</script>")
End If
webuser.Text = Session("user")
End Sub
————————————————————————————————————
上面是关于简单用户验证的,VB的
如果是winform,可以参照前面关于类的静态成员和在主窗口中定义变量的做法