Need Help Building My First Class

Posted on 2009-02-21
Last Modified: 2012-05-06
I'm building my first class for my website project.  The Class will need to define my User entity as well as contain some subroutines for SelectUser, UpdateUser, DeleteUser etc etc.  I have no problem with the subroutines.  For some reason I can't for the life of me figure out where and how i define my "User".  Can someone show me an example of how to architect this class.   Let's just assume that  a  User will have the following properties


And then how i would instantiate an instance of a User object on one of my pages.  Or basically, how i'm going to "tie it together" into one of my pages.  For instance, my login page.
Question by:davidcahan
    LVL 11

    Accepted Solution

    Here's some basics for you (see code below stored as .vb file in your App_Code folder).
        dim oU as new MyProject.User

    To tie it together, you would hold UserID, Email, LoggedIn status in Session variables.
    Namespace MyProject
       Public Class User
              Public FirstName as string
              Public LastName as string
              Public Email as string
              Public IsActive as boolean
              Public Function GetUserIDByEmail(ByValue E as string) as long
                   dim UID as long     
                  .... your script to query db and return userid
                  return UID
              End Function
              Public Function CheckPassword(ByVal UID as long,PWD as string) as boolean
                  dim PwdCorrect as boolean = false
                 .... your script to test password
                  return PwdCorrect
              End Function
              Public Sub InitialiseUser()
                 FirstName = nothing
                 LastName = nothing
                 Email = Nothing
                 Address = nothing
                 Active = nothign
              End Sub
              Public Function GetUser(ByVal UID as long) as long
                 ... your script to read user properties from db
                 Return UID 
             end function
              Public Function SetUser(ByVal UID as long) as long
                 ... your script to update database user properties 
                 Return UID 
             end function
        End Class
    End Namesapce

    Open in new window


    Author Comment

    very nice....could you show me an example of what it would look like if i were returning a whole user record.  for instance.  if i had a sub called

    Public function getUser(byVal UserID as Integer) as User

         DO I DIM A USER IN HERE?  dim u as New User()

        return u
    End Function
    LVL 11

    Assisted Solution

    This is where you would read the values from your database into your object properties.
    My example below assumes that i have built a connectivity class to setup and close adodb.recordset objects.

    Dim oC as new MyProject.Connectivity
    dim rs as adodb.recordset = oC.odbs("SELECT * FROM tblUser WHERE iUserID=" & UserID)
    With rs
      If .eof = false then
               FirstName = .fields("sUserFirstName").value
               LastName = .fields("sUserLastName").value
               Email = .fields("sUserEmail").value
               Address = .fields("sUserAddress").value
               IsActive = .fields("bUserActive").value
           Call Initialise()
      end if
    End with
    oC = nothing

    Open in new window


    Author Comment

    thanks for the help.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now