"Shared" members as globals?

  • Thread starter Thread starter Ross
  • Start date Start date
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?
 
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.
 
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.

--------------------
 
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.

--------------------
 
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
 
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]>
 
Back
Top