Showing posts with label classes. Show all posts
Showing posts with label classes. Show all posts

Monday, March 26, 2012

Newbie ? about Classes

I'm not entirely clear on when to create a class and when to just code subs and funcs. I'm getting pretty comfortable with the latter and am beging to wonder if I should be learning to write classes. Any thoughts would be apreaciated.
Thanks
Raif
This depends entirely on a combination of your goals and your preferences.

Classes are an excellent way to get some reusability out of your code that might apply to more than one web page, or even multiple applications. They can increase development time initially, but you may recover this time on future projects from reuse.

Using classes can also make your code more readable, and therefor more maintainable.

The biggest advantage to writing classes is that it gets you going with Object Oriented Programming. OOP is extremely important in .NET because as you create classes and such, you will begin to build a giant library that you and your team can re-use for future projects. If you do not practice OOP in .NET, you will be spending way to much time in writing the same code over and over again for different projects. For instance, creating Table controls, TableRow controls, TableCell controls can be tedious at times. This would call for a WebControlHelper class that you can reuse for all of your projects. Not only that, as you come up with better techniques for clean up and speed, you can make changes to that class and as long as the method signature and its return type doesn't change, all applications you used this helper for would benefit from it.

i.e.


public class WebControlHelper
{
public WebControlHelper
{

}

public Table CreateTable ()
{
return new Table();
}

public Table CreateTable (int numRows)
{
Table thisTable = null ;

try
{
thisTable = CreateTable();
for (int i=0;i<numRows;i++)
thisTable.Rows.Add(new TableRow());
}
catch (Exception e)
{ throw new Exception (e.ToString(), e); }
finally
{
thisTable.Dispose();
thisTable = null ;
}
}
}

This can go on and on to create quite a large helper class and it will save you time later. Let me know if I can be of further help. I'll be glad too!
AND - if you have subs or functions that you use over and over, you'd want to centralize them in a .vb or .cs file (depending on the language you're using)
That way, they're accessible to any aspx document in your application.
how do i include the cs/vb file containing my reusable functions and procedures?

i read that you can compile it to a dll and have it as part of your Imports. is there a way to do it without compiling?

thanks.

Newbie and wanting to get up to speed fast

Okay what is the best way to get up to speed fast on ASP.NET? I come from a background of using classes and such in classic ASP but I really need to race ahead now into ASP.NET! Anybody want to be my mentor:)

Seriously, I have a few books and have read most of them but I cant quite seem to catch on (maybe I am dense), If someone could point me at the good books or sites (other than this one) and maybe give me a heads up on what is the most important parts it would help.

TIAHad to you got any cange of looking quick START Tutorials!!
If you go through them and practice then you could get confidence on ASP.nET. It's @.http://asp.net/Tutorials/quickstart.aspx
yeah i have started on them and I am pulling DNN apart to see how it works but I seem to be missing something - I don;t feel i understand it all very well like I do classic ASP. Maybe I just need to keep plugging away at it.

newbie class problem

Hi
Sorry to hassle you all again...

Still stuggling with classes. Heres the problem:

I have an aspx code with the following:


<%@dotnet.itags.org. Page Language="VB" src="http://pics.10026.com/?src=photogranny_connections.vb" inherits="Peter" %>
<script runat="server">
dim SQL as string = "SELECT * FROM [images] Order by ID asc"
dim Referrer as string ="OpenDS"
connectionStr(SQL,Referrer)
</script>
<html>...etc

and then I have the class as below


imports system
imports System.Data
imports System.Data.ODBC
imports System.Web.HttpContext
imports System.Web.UI.Page
imports system.web.UI

Public Class Peter
Inherits Page

Sub connectionStr(SQL as string,referrer as string)
-->logic here
End Sub

End Class

however, When I try and call the sub I get 'declaration expected'. Its referring to connectionStr. Its as if it can't see the sub. Iver tried making it a public sub but no joy. The .vb file is in the same directory as the other page.

Thanks for your help
PeterWhen you create a class, you must initialize an object of that class before using it.

MyClass obj = New MyClass
obj.MethodName1("...")

regards
thanks. is this the same for a codebehind file. If I just change the page directive to codebehind="filename" how then do I call the individual subs?

Thanks
Well its the same thing.

regards