Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

In my class object I am trying to access the cache object and am receiving an "Object Reference Not Set To Instance Of An Object" wherever I try to use it. See code below. This is a simple example of a more complex task that I am trying to accomplish, but I still can't get this to work. The code seems to work fine if I just put it in my code behind of my aspx page. This is driving me crazy...any suggestions? What I am doing wrong?

 

Dim x As New MyCache
Me.Label1.Text = (x.GetName)

Code in my class file
Imports System
Imports System.Web.Caching

Namespace AEAClasses
   Public Class MyCache
       Public Sub New()
       End Sub

       Public Function GetName() As String
           Dim mycache As New Cache
           '----------------------------------
           'ERROR ON NEXT LINE - Object Reference Not Set to Instance of an object
           '-----------------------------------
           If IsNothing(mycache("test")) Then
               'Insert test into cache
               mycache("test") = "hello from cache"
               GetName = " it was nothing"
           Else
               'Return Value
               GetName = mycache("test")
           End If

       End Function
   End Class

End Namespace

Edited by Robby
  • Moderators
Posted

One problem is that you're creating a new instance of mycache each time you enter the method.

I would also rename the variable so as not to have the same name as the class.

 

Also, at the top of the page place Option Strict On

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...