Monday, March 26, 2012

simple class and web form

How would one create a class file that holds a method that would take information from a Web Form and insert the data into a database? I understand the SQL part, but it's the class file talking with the Web Form that's tough. Does the class have to extend the Web Form class or something? Like a webForm.aspx and a myClass.cs.No. All you have to do is to create an instance of the class, and use that instance to call the method where you are inserting the data into db

Lets say you have Class1 and you have a method named method1(), which is doing the database insertion.

Then in your webform.
Class1 cls = new Class1();
cls.method1();

This is a simple skeleton code, you should customize it according to your needs, like passing parameters and stuff
in the class definition i don't have to add anything like:


class class1 : web form{
public void method1(){
...
}
}

No. Your class just consist of a definition for some variables and some methods. It has nothing to do with WebForm. Your web form makes use of your class, but not vice versa. Think of it that way. Your class do not care about your web form, it is its independent implementation, but your web form, needs the help of your class to finish certain operations. Thats why you create an instance of that class in the webform.
Kumar is right, dont use : WebForm unless you want to Inherit all of the Web Forms functionality into your class, which is most likely NOT what you want to do. And your web form most likely won't :YourClassName either. A google search on "Inheritance" should clear things up for you. To use your class in your web form, instantiate an object for it and proceed:

Pseudocode:

dim obj as new Class1()

obj.FunctionA(someParams)
obj.FunctionB(someParams)

dim returnVal as Type = obj.FunctionC(someParams)

Google "N-tier design" as well.

0 comments:

Post a Comment