Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Noob question sharing methods?

Status
Not open for further replies.

Smeglor

IS-IT--Management
Jan 16, 2004
70
US
I want to be able to share commonly used methods. In classic asp I would just make an inlcude file with whatever functions I wanted to be accessible anf the common function was directly accessible. When I try this in C#, it always retuns null, in other words, I can include the method, variable, array etc from one common methods page, but no value ever retuns on the page I want to view. Here is a simple example:

//this is the page I want to view

Code:
namespace test
{
	public class test3 : System.Web.UI.Page
	{
		protected common.WebForm1 userctrl;

		private void Page_Load(object sender, System.EventArgs e)
		{
			string moon = userctrl.str1("thisisalongstring").ToString();
			Response.Write(moon);
		}
	}
}

//this is the other page I set up for common methods

Code:
namespace test.common
{
	public class WebForm1 : System.Web.UI.Page
	{
		public string str1(string wordtoevalualte)
		{
			string result = "";
			if(wordtoevalualte.Length > 0)
			{
				result = "yes";
			}
			else
			{
				result = "no";
			}
			return result;
		}
	}
}

I have tried this by making the common methods page a web user control as well with the same result. Anyone know how to do this?


I don't know sir...I guess its broke.
 
user controls are not the place to put shared methods.

you'll want to create a class file in the App_Code folder, make sure it's a public class.
 
How would the class file look like for this example?

I don't know sir...I guess its broke.
 
pretty much as you're written it on that user control. but no inheritance from UI.Page.

Code:
namespace test.common
{
    public class YourClass
    {
        public string str1(string wordtoevalualte)
        {
            string result = "";
            if(wordtoevalualte.Length > 0)
            {
                result = "yes";
            }
            else
            {
                result = "no";
            }
            return result;
        }
    }
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top