views:

59

answers:

2

The below function has to be put within my common functions file in app_code folder. how do I do it?

It gives error like this: Reference to a non-shared member requires an object reference

Public Sub setHeadTags(ByVal title As String, ByVal description As String, ByVal keywords As String)

 Dim metaDescription As HtmlMeta = DirectCast(Page.Master.FindControl("metaDescription"), HtmlMeta)
 Dim metaKeywords As HtmlMeta = DirectCast(Page.Master.FindControl("metaKeywords"), HtmlMeta)
 metaDescription.Attributes.Add("content", "My big content description")
 metaKeywords.Attributes.Add("content", "all, are, my, keywords")
 Dim pageTitle As HtmlTitle = DirectCast(Page.Master.FindControl("pageTitle"), HtmlTitle)
 pageTitle.Text = "Hey hey heY"

End Sub

A: 

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.

Found on:

http://msdn.microsoft.com/en-us/library/zwwhc0d0%28VS.80%29.aspx

Younes
Thanks, but did not help a bit.An example of how to fix this issue might help.If you are writing a function in a file in app_code folder, and the function has to set/change the title of the page, how would you do that?
John
Sorry I don't know how to help you then. I think you should make the function able to contain the Page. Like: ByVal thePage As Page. I dno if you can return the page with it's new info in your return... I don't even know if you will have acces to the page object from the app_code dir.
Younes
A: 

You should be passing the Page object through from where your calling the function. Change your code to have the page as a parameter.

Public Sub setHeadTags(ByVal p as Page, ByVal title As String, ByVal description As String, ByVal keywords As String)

 Dim metaDescription As HtmlMeta = DirectCast(p.Master.FindControl("metaDescription"), HtmlMeta)
 Dim metaKeywords As HtmlMeta = DirectCast(p.Master.FindControl("metaKeywords"), HtmlMeta)
 metaDescription.Attributes.Add("content", "My big content description")
 metaKeywords.Attributes.Add("content", "all, are, my, keywords")
 Dim pageTitle As HtmlTitle = DirectCast(p.Master.FindControl("pageTitle"), HtmlTitle)
 pageTitle.Text = "Hey hey heY"
End Sub

When calling the method from the page send the page object through:

setHeadTags(me.Page,<rest of parameters here>)
Tjaart