Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 244957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:02:55+00:00 2026-05-11T21:02:55+00:00

I have had several occasions recently to access a specific class several times over

  • 0

I have had several occasions recently to access a specific class several times over a relatively small time frame.

So I’ve been storing the value of the class in Session and trying to access it on page load, if it’s not available creating a new instance and storing that in session.

So instead of constantly replicating the same code for different classes on different pages I’m trying to create an extension method to do this for me.

I want to use it like this

Dim objName as MyClass

objName.SessionSingleton()

So far this is what I have for my extension method:

    <Extension()> _
    Public Sub SessionSingleton(ByRef ClassObject As Object)
        Dim objType As Type = ClassObject.GetType
        Dim sessionName As String = objType.FullName
        If TypeOf HttpContext.Current.Session(sessionName) Is objType And HttpContext.Current.Session(sessionName) <> "" Then
            ClassObject = HttpContext.Current.Session(sessionName)
        Else
            Dim singleton As Object = New objType???????

            HttpContext.Current.Session(sessionName) = singleton
            ClassObject = singleton

        End If
    End Sub

I’m stuck on what to do when I make my new instance of my class (it would have to have a New() sub)

I’m not sure where to go from here… or even if this is the best way to do it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:02:55+00:00Added an answer on May 11, 2026 at 9:02 pm

    I figured it out and am posting my code for reference. While digging thru pages about Class/Object Factories (thanks RBarry) I found several references to Activator.CreateInstance() in the System.Reflection Class I came up with this.

    Imports Microsoft.VisualBasic
    Imports System.Runtime.CompilerServices
    Imports System.Reflection
    
    Public Module enviornmentUtilities
        <Extension()> _
        Public Function SessionSinglton(ByVal objType As Type) As Object
            Dim sessionName As String = objType.FullName.ToString
            If Not HttpContext.Current.Session(sessionName) Is Nothing Then
                HttpContext.Current.Trace.Write(HttpContext.Current.Session(sessionName).ToString)
                Return HttpContext.Current.Session(sessionName)
            Else
                Dim ss = Activator.CreateInstance(objType)
                HttpContext.Current.Session(sessionName) = ss
                Return ss
            End If
        End Function
    End Module
    

    This will let you create a session based singleton from any class that does not require parameters in the new method (which isn’t required for this to work)

    To test I made a simple Class:

      Public Class HasNew
            Public FreshInstance As Boolean = True
            Public Sub New()
                HttpContext.Current.Trace.Warn("This Class has a new method")
            End Sub
            Public Sub CheckFreshness()
                If FreshInstance Then
                    HttpContext.Current.Trace.Warn("Fresh HasNew Instance")
                    FreshInstance = False
                Else
                    HttpContext.Current.Trace.Warn("NotFresh HasNew Instance")
                End If
            End Sub
            Public Shared Function type() As Type
                Return GetType(HasNew)
            End Function
            Public Shared Function SessionSinglton() As HasNew
                Return GetType(HasNew).SessionSinglton
            End Function
        End Class
    

    You’ll notice the two Public Shared Methods type() and SessionSinglton which calls the above extension method.

    With those two functions added we have three ways to initiate the Session Singlton demonstrated here:

    Dim HN As HasNew
    
    HN = HasNew.SessionSinglton
    HN.CheckFreshness()
    
    HN = HasNew.type.SessionSinglton
    HN.CheckFreshness()
    
    HN = GetType(HasNew).SessionSinglton
    HN.CheckFreshness()
    

    The Trace Output for this file is as follows:

    This Class has a new method
    Fresh HasNew Instance
    NotFresh HasNew Instance
    NotFresh HasNew Instance
    

    The classes new() method is accessed on the first call to the SessionSinglton method and subsequent calls reflect that the instance is in fact being pulled from memory.

    I hope this helps someone else in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have had to do this several times, usually when trying to find in
I am very new to creating webservers - and I have had several goes
Have had to write my first proper multithreaded coded recently, and realised just how
I have had a bug recently that only manifested itself when the library was
I have had several contexts where table names or schemas were not hard-coded, but
I have a data entry form that had several comboboxes on it. Each of
I've been using MIME::Lite for a short while now and have had no issues
We have had SharePoint where I work for a little while now, but we've
We have had issues with Mootools not being very backward compatible specifically in the
I have had a few problems with log files growing too big on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.