"Shared" members as globals?

R

Ross

MyWebProject.MyWebForm1.someset.somedata is a datatable
within a dataset. Displays quite nicely, too. Now I want
to use the same data in MyWebProject.MyWebForm2. Being a
old, er, experienced Java programmer, I thought I could
write this as a member of MyWebForm1:

Public Shared Function getSomeData() As DataTable
getSomeData = somedata
End Function

so in MyWebForm2 I could have:

Dim quelquedata As DataTable
quelquedata = MyWebForm1.getSomeData()

Could someone point out how to accomplish this the RIGHT
way, as the above clearly doesn't work?
 
C

Chris Jackson

The Shared keyword in VB is equivalent to the static keyword in C-like
languages. It's hard to tell from the snippet that you have here, but as
long as you are referencing the name of the class, and the contents of the
method do not include any instance variables, then it should work just fine.
 
J

Jim Cheshire [MSFT]

Ross,

The Shared keyword means that the method may be used from multiple
instances of an object. In your case, the getSomeData() method is a member
of the WebForm1 class and is not accessible by the WebForm2 class.
However, even so, the WebForm1 class instance is no longer available anyway
because of page lifecycle.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.

--------------------
 
J

Jim Cheshire [MSFT]

Chris,

You are correct in a way. I missed the Public keyword. Under normal
object-oriented situations, the method would be available to the second
class. However, in this instance, the Webform1 instance no longer exists.
Page lifecycle prevents access to the instance.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.

--------------------
 
D

David Browne

Jim Cheshire said:
Ross,

The Shared keyword means that the method may be used from multiple
instances of an object. In your case, the getSomeData() method is a member
of the WebForm1 class and is not accessible by the WebForm2 class.
However, even so, the WebForm1 class instance is no longer available anyway
because of page lifecycle.

Ummm, no.

What the poster is attempting will work fine.


Public Shared Function getSomeData() As DataTable
getSomeData = somedata
End Function


The Shared method belongs to the MyWebForm1 type, not to any particular
instance. It will be available from any method in any type at any time.

Ross, what error are you getting?

David
 
J

Jim Cheshire [MSFT]

You are, of course, correct. As Chris stated earlier, it does work just
like static in C#. My mistake.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.

--------------------
From: "David Browne" <davidbaxterbrowne no potted (e-mail address removed)>
References: <[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top