Solved

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

Posted on 2009-05-14
3
191 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 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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