Closed Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12

Thread: Class library

  1. #1
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    303
    Rep Power
    3

    Class library

    Hi guys ( my virgin post here )

    ok so after alot of research, the best way for me to re-use code is to compile a separate class assembly and include it in my solution. Then, add references to this accembly from my web project.

    HOW THE HECK DO I DO THIS
    what is a separate class assembly?
    how do i include in my solution and reference it from my web project.



    Thanks
    Last edited by richyrich; May 6th, 2008 at 11:21 AM.

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    You don't have to do it exactly that way. Take a look at the following: Building Layered Web Applications with Microsoft ASP.NET 2.0 - Part 1 - Imar.Spaanjaars.Com

    Sure, you build separate classes and all, but it can be apart of the same solution. UNLESS, you want to reuse this code in other projects...then just compile it using the vb compiler and then in VS, go to Website on the menu and select Add Reference. Then you can reuse the code in other apps
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    303
    Rep Power
    3

    cool will check out that link.

    as per my previous question:
    ok so i won't reuse the code in other projects, lets say this app / website
    will only use these classes.

    where do i store these classes, and how do i 'include / reference' them in my diff pages?

    Shem

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Quote Originally Posted by Shem View Post
    cool will check out that link.

    as per my previous question:
    ok so i won't reuse the code in other projects, lets say this app / website
    will only use these classes.

    where do i store these classes, and how do i 'include / reference' them in my diff pages?

    Shem
    That link will show you how to do such within the same app. Just to break it down, the code would typically be placed in the App_Code folder. The classes and the subs/functions would be public so they could be accessed throughout the application. I would place each class (based on which layer in the app it resides into a namespace - see the example in the link). Then import the namespace into the appropriate areas that they are needed.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    Just a an example of what I think you're after, this is what I use at the moment (admittedly after reading J's link in the past, layered apps sound the way to go), but this is what I do.

    Create a seperate .vb file for your "re-useable" code. For instance, I have one for user_details.
    user_details.vb. This file goes in your App_Code folder.
    Code:
    Imports Microsoft.VisualBasic
    Imports MySql.Data.MySqlClient 'only needed because I use MySQL
     
    Public Class user_details
     
      Private Shared _userref as Integer
     
      Public Shared Property userref as integer
        get
             return _userref
        end get
     
        set(ByVal value as Integer)
             _userref = value
        end set
      End Property
     
      Public shared function forename() as string
        Dim conn as new mysqlconnection("your_connection_string")
        Dim mycommand as new mysqlcommand
        mycommand.connection = conn
        Dim rs as new mysqldatareader
             Try
                   mycommand.commandtext = "SELECT forename FROM users WHERE userref=?userref"
                   mycommand.parameters.addwithvalue("userref",userref)
                   conn.open()
                   rs = mycommand.executereader()
                   if rs.hasrows then
                        rs.read()
                        return rs("forename")
                   else
                        return ""
                   end if
                   rs.close
              catch ex as exception
                   return ex.message.tostring
              finally
                   mycommand.dispose()
                   conn.close
                   conn.dispose
               end try
      End Function
     
    End Class
    
    Then in your code behind:-
    Code:
    Sub Page_Load
          if not ispostback then
                    user_details.userref = 1 'or whatever value you want
     
                    lbl_userforename.text = user_details.forename
          end if
    end sub
    
    Hope that helps Shem

  6. #6
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    303
    Rep Power
    3

    cool thanks alot guys.
    both your explanations will help me achieve my goals

    @RR: thats exactly what i was after, that is what or how i want to code in .net
    but will still have to go JMH's way in the end

    Shem

  7. #7
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    303
    Rep Power
    3

    @ RR:
    is this how you are coding in .net at the moment?

  8. #8
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Quote Originally Posted by Shem View Post
    cool thanks alot guys.
    both your explanations will help me achieve my goals

    @RR: thats exactly what i was after, that is what or how i want to code in .net
    but will still have to go JMH's way in the end

    Shem
    Indeed...JMH's way is much cleaner Separating all your business objects, business logic and data access layers is a great way to organize and reuse code. Keep in mind, that example I provided is very basic and once you get used to it you will be able to expand on it. Don't limit yourself to stick strictly to that example
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  9. #9
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    303
    Rep Power
    3

    Quote Originally Posted by jmurrayhead View Post
    Indeed...JMH's way is much cleaner Separating all your business objects, business logic and data access layers is a great way to organize and reuse code. Keep in mind, that example I provided is very basic and once you get used to it you will be able to expand on it. Don't limit yourself to stick strictly to that example
    I'll keep that in mind, thanks

  10. #10
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    Quote Originally Posted by Shem View Post
    @ RR:
    is this how you are coding in .net at the moment?
    Yep...I'm still a noob and need to push this project on, so figure I'll get things working pretty well then look at making improvements at a later date...

    I believe using layers you basically call the business logic layer in your top level code and this in turn makes a call to the data access layer to gather the data.

    Correct me if I'm wrong J!!

Closed Thread
Page 1 of 2 1 2 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO