Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie needs help with code

Status
Not open for further replies.

zerkat

Programmer
Jul 12, 2007
103
US
I am using VS 2005 and .net 2.0. I have this code:

Public Class MenuSystem
Inherits System.Web.UI.Page
Public Shared strPath As String
Public Shared strServerURL As String

Public Shared Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
strServerURL = Request.Url.ToString
If strServerURL = "someURL" Then
strPath = "../somefilepath.aspx"
Else
strPath = "someOtherfilepath.aspx"
End If
End Sub
End Class

I am receiving this error when I mouse over Request in VS - Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

I have read on Microsoft's website that - you have referenced a non-shared member within your code and failed to supply an object reference. You cannot use the class name itself to qualify a member that is not shared. The instance must first be declared as an object variable and then referenced by the variable name.

Didn't I already do that by delcaring strServerURL? Any help would be greatly appreciated.



 
I forgot to mention that I am trying to set some variables up that are accessible throughout the whole app. I did just read on KBAlertz that can not use a shared sub with instance Me. So how would I write this so it's accessible to whole app?
 
if you need to share them across the application then either
1. put them in session
2. create another object which manages these values.

Don't use the Page object to manage this as it's outside the scope of this object to manage.

also "../somefilepath.aspx" will cause problems depending on what sub directory you are in. use "~/somefilepath.aspx" instead.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
see below for the line assigning a value to strServerURL

Public Class MenuSystem
Inherits System.Web.UI.Page
Public Shared strPath As String
Public Shared strServerURL As String

Public Shared Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
strServerURL = HttpContext.Current.Request.Url.ToString
If strServerURL = "someURL" Then
strPath = "../somefilepath.aspx"
Else
strPath = "someOtherfilepath.aspx"
End If
End Sub
End Class


Gilbert M. Vanegas
Senior Programmer Analyst
County of San Bernardino - ISD
 
Thanks for the advice - adding httpContext.Current fixed the error. Now that I have the strPath set - how would I use that in my other pages? Do I need to use MenuSystem.strPath?
 
At the risk of sounding thick-headed - I am just learning as I go along. How would I output the var strPath to a .aspx page? Is that even how it's done in .net? I have tried the obvious response.write(MenuSystem.strPath) but that doesn't work.
 
Remember http is stateless, so it wouldn be fine to create local variables for strpath and retrieve it from the http request object.

However, if you insist on having global variables accessible you could create a module with a global or public variable in scope

Since you are writing asp.net code, you need to become aware of the application/cache and session objects..
You could conceivably put a variable in application/cache state (scoped to the entire web app) or session state (scoped to the users session)

Or create and instantiate a global object with properties that are accessible from the global object.

This is really a "scoping" and object oriented design issue..
If I were you, I would get a handle on variables and "scope" as well as object oriented analysis and design that way you will understand it better.

As to your other question, response.write simply writes directly to the http output stream. In older technologies like classic asp, response.write was used often to render html back to the client browse.

In your specific situation, response.write(strPath) should have shown something, however if you have lots of server controls and stuff on your browser, its just not showing on screen, try right clicking on "view source" and look at the rendered html from the asp.net webpage, that will show you everything rendered to the http response object.


Gilbert M. Vanegas
Senior Programmer Analyst
County of San Bernardino - ISD
 
I am just learning as I go along. How would I output the var strPath to a .aspx page
fair enough we all need to start somewhere, but you will need to actually learn the language instead of throwing objects around until something just works.

find a book on the language you are using (I love C# via CLR) they may have one for VB as well. this will explain how the language works. And you need to know this before you start writing code. I would also recommend Domain Driven Design (language agnostic) as this will help you structure/model your code.

the more you drag/drop the less you know, or have control, of what your code is doing.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi Gilbert. Thanks for the advice. I see what you are saying. I do have this code in a vb file in my app_code folder. So the code itself is accessible to the whole app. I am just not sure where to go from here. Coming from a coldfusion background to .net is frustrating. What I am trying to do is not that difficult. All I want to do is set some file paths based on what is in the URL. Any way, you are right - about time I stop muddling through the code and take some basic tutorials.

thanks again for your assistance
 
No problem, look at regular modules versus class modules.

Just want you to know I wasnt trying to be critical, just trying to give you sound advice. Programming is not "memorization or anything like that", its more like getting an understanding of how things like object oriented programming works and things like scoping of variables, public/private/static friend, polymorphism, inheritance, encapsulation etc...

Once you get those concepts down, it will make things much easier and then you will intuitively "know" how to make things work because you understand how the code itself works.

Gilbert M. Vanegas
Senior Programmer Analyst
County of San Bernardino - ISD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top