Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-05-14
3
Medium Priority
?
199 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
  • 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

916 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