How to create a function

  • Thread starter Thread starter Miguel Dias Moura
  • Start date Start date
M

Miguel Dias Moura

Hello,

I am working on an Asp.Net 2.0.
I created a master page named "MyMasterPage.master".
I created the function "SUM" in my master page VB code.

I then created the page "MyPage.aspx" from my master page.
When I try to use the function "SUM" in "MyPage" VB code it says:
- Name "SUM" is not declared.

I created the SUM function in different ways:
- "SUB SUM()..."
- "Private SUB SUM()..."
- "Public SUB SUM()..." > I thought this would solve the problem.

Basically I need to create functions which will be accessible from many
Aspx pages.

Thank You Very Much,
Miguel
 
Lo,

In VB.NET

1. Create new module called i.e. MyUtilityFunctions and add code
public function Sum(byval a as integer, byval b as integer) as Integer
return a + b
end function
You can use it in your code as follows
dim result as integer = MyUtilityFunctions.Sum(1, 2)

or.
2. Create new class called i.e. MyUtilityFunctions and add code
public shared function Sum(byval a as integer, byval b as integer) as
Integer
return a + b
end function

and you can use it in your code:
dim result as integer = MyUtilityFunctions.Sum(1, 2)

I advise to read a VB.NET tutorial/book to gain basic knowledge about vb.net.
 
In your page, specifying the @masterType directive..

<%@ Page .... %>
<%@ MasterType VirtualPath="~/master/Main.master" %>


then from your codebehind you should be able to do:
Master.Sum(...)

declare sum as

public sub Sum
end sub
 
I agree that a separate utility class is the way to go assuming it doesn't
actually interact with the master page.

Say you wanted Sum to do:

Sub Sum (byval x as string)
myHeaderText.Text = x
end sub

where myHeaderText is a label on the master page, then you'd want to keep
Sum on the master page.

Karl
 
Agree, i thought He wanted just a utility independent function that does't
interact with master page.
--
Milosz Skalecki
MCP, MCAD


Karl Seguin said:
I agree that a separate utility class is the way to go assuming it doesn't
actually interact with the master page.

Say you wanted Sum to do:

Sub Sum (byval x as string)
myHeaderText.Text = x
end sub

where myHeaderText is a label on the master page, then you'd want to keep
Sum on the master page.

Karl
 

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

Back
Top