Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to refence a class's member in a shared(static in C#) function

Posted on 2009-05-14
3
Medium Priority
?
197 Views
Last Modified: 2012-05-07
Hi, To better understand my problem please consider the code below. I want to use the member upoadcount in the shared sub DoSomething(..)
Partial Public Class Main
    Inherits System.Web.UI.Page
    Public uploadcount As Integer = 0
 
 
    <WebMethod()> _
    Public Shared Sub DoSomething(ByVal s As String)
        If HttpContext.Current.Session("View") Is Nothing Then
            HttpContext.Current.Session("View") = 1 ' 1: Details List
 
        End If
 
     End sub
 
End Class

Open in new window

0
Comment
Question by:Abdu_Allah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24389378
A static member cannot access an instance member without creating an instance of that class. Have you considered making "uploadcount" static?
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 24393544
I know that and this is why I'm asking this question, I want to know how to do that in the code that I provided.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 24394507
Well it would be something like below. But I don't think you are going to get the results you are expecting, which is why I suggested marking your private variable as "Shared".
<WebMethod()> _
Public Shared Sub DoSomething(ByVal s As String)
    Dim m as New Main()
 
    m.uploadcount += 1
 
    If HttpContext.Current.Session("View") Is Nothing Then
         HttpContext.Current.Session("View") = 1 ' 1: Details List
 
    End If
End Sub
 
////////////////////////////////////////////////////////////////
//  My suggestion
////////////////////////////////////////////////////////////////
Partial Public Class Main
    Inherits System.Web.UI.Page
    Public Shared uploadcount As Integer = 0
 
 
    <WebMethod()> _
    Public Shared Sub DoSomething(ByVal s As String)
    	Main.uploadcount += 1
    
        If HttpContext.Current.Session("View") Is Nothing Then
            HttpContext.Current.Session("View") = 1 ' 1: Details List
 
        End If
 
     End sub
 
End Class

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 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