Still not clear about global functions

A

Alan Silver

Hello,

I have spent ages trawling through Google, looking for information about
global functions in ASP.NET and I'm still not clear about the best way
to go about this (or not).

I am writing a site that will be for members only. They will have to log
in to gain access to any of the pages. I am holding the user information
in an XML file (there will probably never be a large number of user, so
this is efficient enough).

What I would like to do is have a global function that I can use to get
user settings out of the XML file. For example, if I called ...

UserSettings("accessLevel")

then I would get the access level setting for the currently logged in
user (where "accessLevel" is something I put in the XML file).

After researching this, two main points seem to come up over and over
again...

1) To use global functions, create a class and add the functions in
there. If they are static, then you don't need to instantiate the class,
you just call MyStuff.UserSettings("accessLevel").

2) Don't have global functions, it's not the OOP way.

The problems I have are (in relation to these two points)...

1) OK, so I can create a file called MyStuff.cs, which contains a class
and the static members. What do I do with it then? I guess if you are
using VS, then that's all you need to do, but I'm using a text editor.
What do I do with the .cs file when I've created it? No-one seems to
mention that point.

2) What's a better (ie more OOP) way to do this? I'm very keen to learn
the right way to do things, but I can't see one here.

Any advice appreciated.
 
M

Mythran

1) OK, so I can create a file called MyStuff.cs, which contains a class
and the static members. What do I do with it then? I guess if you are
using VS, then that's all you need to do, but I'm using a text editor.
What do I do with the .cs file when I've created it? No-one seems to
mention that point.

2) What's a better (ie more OOP) way to do this? I'm very keen to learn
the right way to do things, but I can't see one here.

Any advice appreciated.

How do you currently compile more than 1 class file into a single assembly?
If you don't, and you would prefer to use a single file, you can move the
class definition to the single file you use. 1 file can contain more than 1
class definition :)

Other than that, I do not know how to compile from the command line without
researching it. This is because, I don't do it and never needed to.

HTH,

Mythran
 
J

Juan T. Llibre

re:
1) OK, so I can create a file called MyStuff.cs, which contains a class and the static
members. What do I do with it then?

Just open a command window ( cmd.exe );
make sure that the .Net Framework directory is in your path
( the .Net dir is where the compilers are located (vbc.exe, csc.exe));
navigate to the directory where you have your .vb files,
and run the compile command :

csc /t:library /out:MyStuff.dll MyStuff.cs

If you need to import .Net classes, include them in your command line:
csc /t:library /r:system.dll /r:system.web.dll /out:MyStuff.dll MyStuff.cs

MyStuff.dll will be created/compiled in the current directory.

Move it to the /bin directory of your application and fire away.
If you want to have VS.NET use it, reference it in your project.

You can now call the classes in MyStuff.dll using
YourClassName.yourmethod, as long as you import
the assembly into your aspx file :

<%@ Import Namespace="YourClassname" %>



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
J

Juan T. Llibre

errata :
navigate to the directory where you have your .vb files,

should be : navigate to the directory where you have your .cs files,

and
<%@ Import Namespace="YourClassname" %>

Should be :
<%@ Import Namespace="YourNameSpace" %>

Sorry...




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

Juan T. Llibre said:
re:
1) OK, so I can create a file called MyStuff.cs, which contains a class and the static
members. What do I do with it then?

Just open a command window ( cmd.exe );
make sure that the .Net Framework directory is in your path
( the .Net dir is where the compilers are located (vbc.exe, csc.exe));
navigate to the directory where you have your .vb files,
and run the compile command :

csc /t:library /out:MyStuff.dll MyStuff.cs

If you need to import .Net classes, include them in your command line:
csc /t:library /r:system.dll /r:system.web.dll /out:MyStuff.dll MyStuff.cs

MyStuff.dll will be created/compiled in the current directory.

Move it to the /bin directory of your application and fire away.
If you want to have VS.NET use it, reference it in your project.

You can now call the classes in MyStuff.dll using
YourClassName.yourmethod, as long as you import
the assembly into your aspx file :

<%@ Import Namespace="YourClassname" %>



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
A

Alan Silver

Juan,

Thanks for your answer. I suppose it's kinda obvious that they should be
compiled into DLLs, but I wasn't sure and no-one seemed to specify.

I suppose they can't be done from source files? DLLs aren't a huge
problem (not like COM DLLs in Classic ASP), but one huge advantage of
source files (like .aspx files) is that you can do quick changes on the
fly.

As to the other question, is this a sensible approach altogether? I
can't see how you can get around having a global function here, but
everyone seems to say that they are really against the idea of OOP.

Thanks for the reply.
a
re:
1) OK, so I can create a file called MyStuff.cs, which contains a
class and the static
members. What do I do with it then?

Just open a command window ( cmd.exe );
make sure that the .Net Framework directory is in your path
( the .Net dir is where the compilers are located (vbc.exe, csc.exe));
navigate to the directory where you have your .vb files,
and run the compile command :

csc /t:library /out:MyStuff.dll MyStuff.cs

If you need to import .Net classes, include them in your command line:
csc /t:library /r:system.dll /r:system.web.dll /out:MyStuff.dll MyStuff.cs

MyStuff.dll will be created/compiled in the current directory.

Move it to the /bin directory of your application and fire away.
If you want to have VS.NET use it, reference it in your project.

You can now call the classes in MyStuff.dll using
YourClassName.yourmethod, as long as you import
the assembly into your aspx file :

<%@ Import Namespace="YourClassname" %>



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
A

Alan Silver

1) OK, so I can create a file called MyStuff.cs, which contains a class
How do you currently compile more than 1 class file into a single
assembly? If you don't, and you would prefer to use a single file, you
can move the class definition to the single file you use. 1 file can
contain more than 1 class definition :)

OK, you're ahead of me already!! I should have said that I'm relatively
new at this and am learning as I go along. I have got as far as creating
..aspx and .ascx files, but haven't yet ventured into DLLs. I didn't
realise that class files had to be compiled into DLLs.
Other than that, I do not know how to compile from the command line
without researching it. This is because, I don't do it and never
needed to.

I knew how to compile a DLL from the command line (read about it in a
book), but I didn't realise that was what as needed here.

Thanks for the reply.
 
J

Juan T. Llibre

Hi, Alan.

re:
I suppose they can't be done from source files?

You could, but it's not good to publish source code.

One of the great advantages of assemblies ( DLLs ) is that they
give you a measure of protection against those who would simply
copy your source code.

re:
As to the other question, is this a sensible approach altogether?

Sure it is...

Using classes is more efficient and helps you organize your code functions.

re:
I can't see how you can get around having a global function here, but everyone seems to
say that they are really against the idea of OOP.

Tell "everyone" that OOP *is* the standard,
and the most efficient, way to manage functionality.



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

Alan Silver said:
Juan,

Thanks for your answer. I suppose it's kinda obvious that they should be compiled into
DLLs, but I wasn't sure and no-one seemed to specify.

I suppose they can't be done from source files? DLLs aren't a huge problem (not like COM
DLLs in Classic ASP), but one huge advantage of source files (like .aspx files) is that
you can do quick changes on the fly.

As to the other question, is this a sensible approach altogether? I can't see how you
can get around having a global function here, but everyone seems to say that they are
really against the idea of OOP.

Thanks for the reply.
a
 
A

Alan Silver

I suppose they can't be done from source files?
You could, but it's not good to publish source code.

One of the great advantages of assemblies ( DLLs ) is that they
give you a measure of protection against those who would simply
copy your source code.

Hmm, aren't .aspx files source files? I mean code-beside. I know, you'll
tell me that this is a good argument against code-beside!!

Anyway, given the simplicity of replacing a DLL in ASP.NET (unlike COM
DLLs in Classic ASP), it's not really a problem, I just wanted to make
sure I understood the full picture.
re:

Sure it is...

Using classes is more efficient and helps you organize your code functions.

Even when the class is artificial? In the example I have, the fact of
having a class is really immaterial. I could just as easily have a
global function without a class, if there were a simple way to do this.

Creating classes merely to organise functions is a bit like putting
functions in organised standard modules in VB6. There's nothing
inherently OOP about it, it's just a way to put each on with others of
its kind.
re:

Tell "everyone" that OOP *is* the standard,
and the most efficient, way to manage functionality.

I guess the same comments apply here. It isn't *really* OOP, it's using
classes as a method of organising.

As usual, your comments are greatly appreciated.
 
J

Juan T. Llibre

re:
you'll tell me that this is a good argument against code-beside!!

You got it!

:)

re:
Creating classes merely to organise functions is a bit like putting functions in
organised standard modules in VB6.

Of course, function organization is not the only benefit received from OOP.

re:
There's nothing inherently OOP about it, it's just a way to put each on with others of
its kind.

True, but there's absolutely no way to work with OOP
except by using classes, AFAIK. Can you think of one ?

re:
I guess the same comments apply here. It isn't *really* OOP, it's using classes as a
method of organising.

See above.

re:
As usual, your comments are greatly appreciated.

Thanks. I try to do my best.

Sometimes I hit the nail on the head.
Other times I find I really need to take a break!

;-)



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
A

Alan Silver

Creating classes merely to organise functions is a bit like putting
Of course, function organization is not the only benefit received from OOP.

No, but in this case, it seems to be the only benefit.
re:

True, but there's absolutely no way to work with OOP
except by using classes, AFAIK. Can you think of one ?

No, but that wasn't what I meant. I meant that this way doesn't seem to
have any advantages over using a common include file. Therefore, this
isn't really an OOP issue.

I have no problem with putting such functions into a class, I was just
checking if I was missing something important when I couldn't see any
benefit from the class as opposed to the include file. It sounds like
you're saying that in this case, there isn't any specific benefit to a
class, it's just a convenient way of doing it.

After posting, I realised that I have one more problem here. Now that
the function has been moved into a class, how does it get access to
things like the Server object? When the function was in the .aspx, this
was readily available. Now it's in a class, I guess I need some way of
telling the class that it's going to be in an ASP.NET page. How do I do
that? Does the class need to inherit from something? If so, what?

Thanks again.
 
A

Alan Silver

After posting, I realised that I have one more problem here. Now that
the function has been moved into a class, how does it get access to
things like the Server object? When the function was in the .aspx, this
was readily available. Now it's in a class, I guess I need some way of
telling the class that it's going to be in an ASP.NET page. How do I do
that? Does the class need to inherit from something? If so, what?

OK, it looks as if I need to inherit from the Page object. I tried this,
but ran into problems when trying to use MapPath. The code is shown
below...

using System.Web.UI;

namespace PixataUtils {
public class Util : Page {
public static string UserVars(string varName) {
return Server.MapPath("/");
}
}
}

When I try to compile this, I get the following error...

Util.cs(6,14): error CS0118: 'System.Web.UI.Page.Server' denotes a
'property' where a 'class' was expected

Any idea what this means? As far as I can see from the SDK, Server *is*
a class, it's a member of the Page object.

Any advice welcome!! Thanks.
 
J

Juan T. Llibre

For the Server object you can use System.Web.HttpServerUtility.

http://www.csharpfriends.com/quicks...&namespace=System.Web&class=HttpServerUtility

For other objects, use System.Web.HttpApplication
See its list of properties for ASP.NET objects at :

http://www.csharpfriends.com/quicks...3a&namespace=System.Web&class=HttpApplication

You should also look at System.Web.HttpContext :
http://www.csharpfriends.com/quicks...d50a3a&namespace=System.Web&class=HttpContext




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
A

Alan Silver

Thanks for those links.

I actually discovered (shortly after posting - as usual) an old post
that discussed this exact subject. It pointed out that static methods
are assumed to be called without the object being created. At the point
the static member is in existence, the Page object hasn't been created,
so the reference to Server doesn't point to an object.

I don't think I explained that very well, but basically what I did was
change the method to be non-static, and it compiled fine. In the calling
page, I just created an instance of the class first. Then the code
worked fine.

Thanks again
For the Server object you can use System.Web.HttpServerUtility.

http://www.csharpfriends.com/quickstart/aspplus/samples/classbrowser/vb/
classbrowser.aspx?assembly=System.Web,%20Version=1.0.5000.0,%20Culture=n
eutral,%20PublicKeyToken=b03f5f7f11d50a3a&namespace=System.Web&class=Htt
pServerUtility

For other objects, use System.Web.HttpApplication
See its list of properties for ASP.NET objects at :

http://www.csharpfriends.com/quickstart/aspplus/samples/classbrowser/vb/
classbrowser.aspx?assembly=System.Web,%20Version=1.0.5000.0,%20Culture=n
eutral,%20PublicKeyToken=b03f5f7f11d50a3a&namespace=System.Web&class=Htt
pApplication

You should also look at System.Web.HttpContext :
http://www.csharpfriends.com/quickstart/aspplus/samples/classbrowser/vb/
classbrowser.aspx?assembly=System.Web,%20Version=1.0.5000.0,%20Culture=n
eutral,%20PublicKeyToken=b03f5f7f11d50a3a&namespace=System.Web&class=Htt
pContext




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
M

Micah LaCombe

Alan, you mentioned that youd like to be able to use your global-class
without having to compile it to a DLL. Have you discovered if this is
possible?

regards,
Micah L.
 
A

Alan Silver

Alan, you mentioned that youd like to be able to use your global-class
without having to compile it to a DLL. Have you discovered if this is
possible?

I have discovered that it isn't possible ;-)

Having said that, compiling the DLL turned out to be so quick and easy
that it's not a great problem.
 

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

Top