.Net "include" files

M

Matt Jensen

Howdy

I want to simulate with .Net what I used to do with classic ASP where you
would have a series of include files with utility functions that you would
include when you needed them.

I read some article about creating a utility class, but it gave no details,
and I'm not sure what to do. Do I create a .cs file and then just include
it? What should the class inherit from? And how do you inherit more than one
class on your page in order to use the utility class?

Clearly there's a understanding gap I've got here, any pointers/links etc.
would be greatly appreciated.
Cheers
Matt
 
K

Karl Seguin

If you just have some miscallaneous functionality, you create a static
methods inside a class

public class Utility
{
public static in Adder(int x, int y)
{
return x + y; //what about overflows, oh oh!
}
}



which you can then use via

Utility.Adder(1,2)


if your Utility class and your consuming class aren't in the same namespace,
you either have to import (via using) the Utility's namespace, or fully
reference it via Namespace.Utility.Adder()

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
K

Kevin Spencer

Because of ASP's procedural nature, the use of include files to include
other files of scripting code was a necessity, a way of achieving code
reusability in a procedural, scripting technology. As ASP.Net is both
object-oriented, and fully compiled, the use of include files is not
generally a good practice. If one is using include files that contain
pre-processor code, all well and good. But to use include files for
executable code is a big mistake.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Expect the unaccepted.
 
M

Matt Jensen

Thanks guys
I'm essentially using notepad at the moment, if that makes a
difference.... - I don't understanding how making a simple 'using' call to
the namespace manages to locate the class...?
Thanks for any enlightenment :)
Matt
 
K

Kevin Spencer

As ASP.Net is object-oriented and fully compiled, it makes no difference
what tools you use to write your code. Remember, this is not procedural
scripting.

Are you using CodeBehind, or a Siingle-File WebForm? I would recommend using
CodeBehind classes, as it separates the UI business logic from the Template
code, making it easier to manage. You could put your CodeBehind files into
the bin directory, as well as the classes (class files) you create. Then
(you're using VB.Net, right?) you would simply use an Imports NameSpace
statement in the CodeBehind to use the external classes.

Another alternative (although this is not as pretty) is to define multiple
classes in the same file. Of course, this makes reusing your business
classes much more difficult.

You may find the following MSDN Library article helpful:

http://msdn.microsoft.com/library/d...y/en-us/vbcon/html/vbconwebformscodemodel.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Expect the unaccepted.
 
J

Juan T. Llibre

re:
I read some article about creating a utility class, but it gave no details, and I'm not
sure what to do. Do I create a .cs file and then just include it?

What I do is write my classes/methods/properties in a .cs ( or .vb )
file and compile it from the command-line. That creates an assembly
which I call it utilities.dll, original, huh ? :) which I place in the bin directory.

Then, it's a simple thing to call the
classes/methods/properties in my .aspx files.

The classes/methods/properties can be called from any .aspx page.

As others have suggested, writing user controls works, too,
although I find it simpler to use a hand-compiled assembly for utillities.

To compile a single file, use :

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

To compile all .cs files in a directory to utils.dll , use :
csc /t:library /out:utils.dll *.cs

If you need to reference a library/libraries,
add it/them, separated by slashes :

csc /t:library /r:system.dll /r:system.data.dll /out:utils.dll *.cs



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

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