Solved

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

Posted on 2009-05-14
3
187 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 74

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 74

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now