Saturday, March 31, 2012

Sharing functions across files?! (new to .NET)

I'm not at all new to ASP, but ASP.NET both amazes me and confuses me totally.

In normal ASP every page I made included a theme.asp file that would generate the site design. I have now converted that to User Controls. No problems there - so far... :)

My problem is that when I at the time included the theme.asp file it contained common functions, const's etc. that I used in various pages. Now in ASP.NET i cannot include files just like that. I will have to have the exact same function in every aspx file. Can't they share some how? (well i know they can, but how?)

Sorry for asking silly questions but I am totally green ragarding ASP.NETstick the function in class and call the class like you would any other class/object.
I apologize for my lack of brain power... ;)

Can you tell me how exactly I would do that?
you would want to start a component project and write your funtions in the class using the proper oop techniques
I assume you presume I am using Visual Studio.NET right? Guess what - I'm not.
Besides, with all due respect, your reply was good nothing for me... :(
Do you even know what you are talking about? heh
well then I would suppose you would just want to write a code behind page and there is proablly some sort of special command that is different to then the regular complile but i wouldnt know but your code should look like

public class jerk
function beajerk() as string
return "I am a jerk"
end function
end class

Next time you could be a little nicer when responding to someone who is trying to help ya out oh yeha you can also add properties to you class but what ever
Ok, I'm sorry. I was rude. Actually I appreciate that you try to help me. It's just that I have been working on this for ages and have achieved almost nothing. Therefore I'm not in my best mood.

My apologies...!
Code behind can be compiled dlls or compiled on the fly - vs.net automatically creates projects with code behind for every single page that will be compiled. It didn't take me long to start looking for alternatives.
For your code behind to compile on the fly then you use the src attribute instead of codebehind so that your page reads:-

<%@. page arc="webform1.aspx.vb" inherits="webform1" %>

where the file is webform1.aspx.vb and the class name is webform1.
This is fine but does not allow you to declare other files to compile on the fly so that just one vb file could contain all the functions you want to use on every page. A typical example using vs.net to compile a project would be a class with a series of shared functions.
To do this with aspx files that compile on the fly you have to compile your shared functions class. I could not find any other way of doing it. The only other alternative is to include the shared functions class in all .vb files which to me is innefficient.
You say you are not using vs.net but you must be using the .net sdk which contains the compilers. vs.net is just an ide. So you write your shared functions file as normal and compile it using vbc.exe to a dll. Then copy it to the bin folder of your webproject. Then use the imports statement in your vb files for each page to import a reference to it.
An example class might be like one I am using. Here it is with one function to get my connection string from my web.config file:-

Import System.Web.UI 'example - other imports may be required
Public Class SharedFuncs
Public Shared Function ConnStr(ByVal wpage As Page) As String
If wpage.Server.MachineName = "localcomputername" Then
Return CStr(ConfigurationSettings.AppSettings("LocalConnString"))
Else
Return CStr(ConfigurationSettings.AppSettings("WebConnString"))
End If
End Function

Public Shared Function ConnStr(ByVal wcontrol As UserControl) As String
If wcontrol.Server.MachineName = "localcomputername" Then
Return CStr(ConfigurationSettings.AppSettings("LocalConnString"))
Else
Return CStr(ConfigurationSettings.AppSettings("WebConnString"))
End If
End Function
End Class

My class contains one overloaded function (the same function but 2 versions taking in the first case a webform reference and in the second case a usercontrol reference) which will return my connection string depending on whether I'm working on my home machine or it is live on my host.
Now with this code saved as sharedfuncs.vb I can compile it:-

vbc target:library /out:sharedfuncs.dll sharedfuncs.vb

assuming your dos box has a path to the vbc.exe file which is in the framework folder normally under the windows directory you will get the sharedfuncs.dll file. Now copy this file to the bin directory of you web project (create the bin folder if necessary) and it is ready to use.
A Webform's code file for example webform1.aspx.vb now needs an import statement at the very start of the code:-

Imports sharedfuncs.dll

Public Class WebForm1
Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dim conn as new system.data.sqlclient.sqlconnect(sharedfuncs.ConnStr(me))
End Sub

End Class

And there you have an example of called the shared function to create a sql server connection object.
Sorry for being so cryptic. :D Class usage is a very important thing to know. You are dead in the water without it in .NET.

hope Musician helped you.
Thanks alot!

I appreciate the hard work you did for me there :-)
You should start a personal tutorial ordering company hehe

I will try it and get back to you if I can't manage
Funny you should mention it - my new dotnet website will be launching soon.
Sounds very cool! Wish I had the time (and knowledge! heh) to do that...

Let me know when it's up. I will use it for sure...

0 comments:

Post a Comment