Sharing code possible-ASP.NET and Windows Forms

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);
}

}
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
A

Adam Smith

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
A

Adam Smith

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.
 

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