asp.net - Why am I getting Expression Expected? -


this simple codefile vb code username , password login form redirect different 'members area' pages:

public class mypage inherits page private structure cred     public username string     public password string     public redirecturl string     public sub new(un string, pw string, optional ru string = "/admin/default.aspx")         username = un         password = pw         redirecturl = ru     end sub end structure  private readonly _credentials system.collections.generic.ienumerable(of cred) =  new cred(){new cred("userone", "passwordone"), new cred("usertwo", "passwordtwo"), new cred("userthree", "passwordthree", "/admin/custom.aspx")}  public sub page_load(sender object, e eventargs)     dim user = _credentials.singleordefault(function(x) x.username = username.text andalso x.password = password.text)     if user isnot nothing         session("admin") = true         response.redirect(user.redirecturl)     else         session("admin") = false         ltllogin.text = "<p>sorry, have provided incorrect login details.</p>"     end if end sub end class 

it's on line:

dim user = _credentials.singleordefault(function(x) x.username = username.text andalso x.password = password.text) 

thanks much.

david.

the problem using structure against class cred. aware structures value types , classes reference types.

so:

dim user = _credentials.singleordefault(function(x) x.username = username.text andalso x.password = password.text) 

always return structure (when nothing found members of structure gets default values).

you cannot compare structure nothing ti not reference type.

change structure class , fine.

or change check with:

if not user.equals(new cred) 

check this

update examples

class cred

imports system.linq  module startupmodule      private readonly _credentials system.collections.generic.ienumerable(of cred) = new cred() {         new cred("userone", "passwordone"),         new cred("usertwo", "passwordtwo"),         new cred("userthree", "passwordthree", "/admin/custom.aspx")}      sub main()         dim username string = ""         dim password string = ""          dim crd = _credentials.where(function(x) x.username = username andalso x.password = password).singleordefault          if crd nothing             console.writeline("user nothing")         else             console.writeline("user something")         end if           console.readline()     end sub      private class cred         public username string         public password string         public redirecturl string         public sub new(un string, pw string, optional ru string = "/admin/default.aspx")             username = un             password = pw             redirecturl = ru         end sub     end class   end module 

structure cred

imports system.linq  module startupmodule      private readonly _credentials system.collections.generic.ienumerable(of cred) = new cred() {         new cred("userone", "passwordone"),         new cred("usertwo", "passwordtwo"),         new cred("userthree", "passwordthree", "/admin/custom.aspx")}      sub main()         dim username string = ""         dim password string = ""          dim crd = _credentials.where(function(x) x.username = username andalso x.password = password).singleordefault          if crd.equals(new cred)             console.writeline("user nothing")         else             console.writeline("user something")         end if           console.readline()     end sub      private structure cred         public username string         public password string         public redirecturl string         public sub new(un string, pw string, optional ru string = "/admin/default.aspx")             username = un             password = pw             redirecturl = ru         end sub     end structure   end module 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -