Include file

  • Thread starter Thread starter JH
  • Start date Start date
J

JH

I'm new in the .NET world, so sorry if this is a faq. In classic ASP I
often use a include file with some standard functions, ex. formating a
string an so. How do a do this i .NET ??

In my test code below, I submit a form with a textbox, use the
function "format_string" to change the value. I would like to use this
function in other scripts also, so need to place the code in another
file, and include it, how ?

I'll appreciate if someone could change my test code, to show me the
right way to do it.

Thx..



test.aspx
------------------
<%@ page language="vb" inherits="test" src="test.vb" %>
<html>
<head>
</head>

<body>
<form id="test" method="post" runat="server">
<asp:textbox id="sometext" text="" runat="server" />
<asp:button id="submit" text="submit" onclick="test_me"
runat="server" />
</form>
</body>
</html>
------------------


test.vb
------------------
Imports System
Imports System.Web.UI

public class test : inherits page
sub test_me(ByVal Sender As Object, ByVal E As EventArgs)
dim sometext as string
sometext = format_string(Request.Form("sometext"))
end sub

function format_string(value as string) as string
return "!" & value & "!"
end function
end class
------------------
 
The easiest way would be to make some utility classes.

public class StringUtilities
{
public static string FormatString(string s)
{
string result;

//Do something
return result;
}
}


And then in the code-behind you just use the class:

private void Page_Load(...)
{
string s="Hello 123";
s=StringUtilities.FormatString(s);
}

I hope this helps
Hugo
 
The easiest way would be to make some utility classes.

Thanks for your reply.... I like the idea with a "utility class".


I put this code in ex. "StringUtilities.vb", and place it in a shared
include-folder
-----
public class StringUtilities
{
public static string FormatString(string s)
{
string result;

//Do something
return result;
}
}
-----

How do my codebehind code (test.vb) know about the class
"StringUtilities" ??
-----
private void Page_Load(...)
{
string s="Hello 123";
s=StringUtilities.FormatString(s);
}
-----
 
Everything is compiled into a single dll if it resides in the same
project. All the classes and pages can use functions from each other.

I've sent another post with what I think is the VB equivalent of what
I posted in my first reply.
 
Sorry here's the post I should have sent before:

Saw that you are using VB, but it should be the same principle.
Separate the format string subroutine from the page and put it in a
class:

public class StringUtilities

public static function format_string(value as string) as string
return "!" & value & "!"
end function

end class

then change sub test_me to:

sub test_me(ByVal Sender As Object, ByVal E As EventArgs)
dim sometext as string
sometext = StringUtilities.format_string(Request.Form("sometext"))
end sub

Something like that anyway.
Cheers, Hugo
 
Do you (can you) place these utility classes in a separate code file where
they will be available to all parts of the application and reusable in other
applications? If so what type (extension) of file do you create?

thanks
 
Yes. That is the beauty with .net. Start a new class library project
(or set the compilation target to library). The output from
compilation will be a dll that you can reference to from your other
projects.
 
Back
Top