Sharing code possible-ASP.NET and Windows Forms

  • Thread starter Thread starter Adam Smith
  • Start date Start date
A

Adam Smith

Hello. I'm wondering. is it possible to share code between a windows
forms application and an asp.net. Specifically, I'd like to do
something like:

class Demo
{
private static ArrayList al = null;
private string sType;

public Demo(string sType)
{
this.sType = sType;
if(sType == "Web")
al = Application["al"] = al;

if(al == null)
{
al = new ArrayList();
if(sType == "Web")
Application["al"] = al;
}
}

public static void initDemo(string sType)
{
if(sType == "Web")
al = Application["al"];
al.Add(3);
al.Add(5);
}

public static bool testforvalue(int n)
{
if(sType == "Web")
al = Application["al"];
return al.Contains(n);
}

}
 
Adam,

It is possible to share code like this. However, the way you are doing
it is not the way I would do it. What I would do is create a base class
that holds the common functionality that you want to share. Then, I would
derive a class from that base, one for windows, one for web, which has the
specific logic you need for each presentation layer, overriding the methods
that are appropriate on the base class.

Hope this helps.
 
Yes, but how do I access that Application cache from a derived common
code module? A class needs to be derived from
System.Web.HttpApplication or System.Web.UI.Page in order to do that.

I just want to call my class method and have it do all the work for me.

Thanks.
 
Adam,

In this case, you would have the base class have virtual
methods/properties which the derived class would return values for. Your
base class would perform processing based on the return values of these
methods. Meanwhile, your derived classes would return values for the
operations. In the case of the web application, it would return values from
the application object.
 
Correction! Didn't realize that you could have a static variable in a
class in asp.net and it would keep it active for the life of the
application and not initialize it at every page access.

I probably can use my existing class as is, just not the database
pooling features which would use the same database connection throughout
a single threaded app. For asp.net, I will derive my database pooling
class to open a new connection every time for different users.
 
Back
Top