Showing posts with label class. Show all posts
Showing posts with label class. 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 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

Newbie class inheritance question

I'm running into a simple issue when instatiating a derived class that
calls a custom constructor. Here's the type of code found in the
parent class:

Public Class parent
Private _a As Long
Private _b As Long
Private _c As String
....

Public Sub New(ByVal a As Long, ByVal b As Long, ByRef c As
String)
_a = a
_b = b
_c = c
EndSub
EndClass

And now the derived class:

Public Class child
Inherits parent

' apparently, i have to redeclare the private vars found in the
parent?
Private _a As Long
Private _b As Long
Private _c As String
....
' cannot inherit the constructor, so define identical one here
Public Sub New(ByVal a As Long, ByVal b As Long, ByRef c As
String)
' call mybase.new to assign vars
MyBase.New(a, b, c)
EndSub
EndClass

By calling 'mybase.new' in the child class, I had expected the var
assignments in the parent to be executed, populating the vars declared
in the child.
What I found with the above class definitions is that the child 'var
_c' is NOT assigned a value.

I'm sure there's a simple answer, but I'm a beginner. Also, I found
some MSDN articles on class inheritance in the 'Upgrading to .NET'
section, but am looking for something that goes deeper. Any idea
where I can find articles that go into greater depth on the subject of
class inheritance?

Thanks... JonJon, "Private" means "Private". Not even a child class can see private
members. You would need to declare them as protected in the parent in order
for the child to use them.

And as a general rule, you should avoid having derived classes access fields
in the parent class. Let them stay private, but declare protected properties
to access them:

Protected Property A as Long
Get
Return _a
End Get
Set
_a = Value
End Set
End Property

This way, the parent class is in control of when these fields are set, and
the values to which it may be set. For instance, you can put code in the Set
accessor to validate the value, or even put code in the Get accessor to get
the value from somewhere else. If you allow your child classes to access the
fields directly, then the fields will _always_ have to be there, just the
way they are, or you'll break the derived classes.
--
John Saunders
Internet Engineer
john.saunders@.surfcontrol.com

"Jon" <to_jon@.hotmail.com> wrote in message
news:e4b2505c.0308061218.513521ee@.posting.google.c om...
> I'm running into a simple issue when instatiating a derived class that
> calls a custom constructor. Here's the type of code found in the
> parent class:
> Public Class parent
> Private _a As Long
> Private _b As Long
> Private _c As String
> ...
> Public Sub New(ByVal a As Long, ByVal b As Long, ByRef c As
> String)
> _a = a
> _b = b
> _c = c
> EndSub
> EndClass
> And now the derived class:
> Public Class child
> Inherits parent
> ' apparently, i have to redeclare the private vars found in the
> parent?
> Private _a As Long
> Private _b As Long
> Private _c As String
> ...
> ' cannot inherit the constructor, so define identical one here
> Public Sub New(ByVal a As Long, ByVal b As Long, ByRef c As
> String)
> ' call mybase.new to assign vars
> MyBase.New(a, b, c)
> EndSub
> EndClass
> By calling 'mybase.new' in the child class, I had expected the var
> assignments in the parent to be executed, populating the vars declared
> in the child.
> What I found with the above class definitions is that the child 'var
> _c' is NOT assigned a value.
> I'm sure there's a simple answer, but I'm a beginner. Also, I found
> some MSDN articles on class inheritance in the 'Upgrading to .NET'
> section, but am looking for something that goes deeper. Any idea
> where I can find articles that go into greater depth on the subject of
> class inheritance?
> Thanks... Jon

Saturday, March 24, 2012

NEWBIE HELP

Hi All

I have a web form that uses an object class to populate a standard table (not datagrid etc)

I have a button on this form that when clicked opens another form for editing. In this form all the values of the object are used to populate text boxes (in the load event of the form there is a call to a DBHelper class which uses a SQL Parameter Array to execute a strored procedure (SQL) and return the data) and text areas which are free to edit in this form.

Also on this page I have a button called save changes which when clicked I am attempting to create a new paramater array to execute an update procedure again using the DBHelper Class.

Unfortunately when I try to do this the values in the new SQLParamater Array are exactly the same as before and are not reflecting the changes that I make by typing new entries into the text boxes on the form. As a newbie to .net I am lost here and realise that this is probably a scope or state problem but I would like someone to point me in the right direction please. When I hard wire the new SQLParamater Array the update does work so that eliminates the chances of the DBHelper Class from any fault.

Please help as I am going bonkers here!!!!!

Phill

You probably have your "populate form" bit in your Page Load function - which will execute when the save button causes a post back.

Surround your "populate the textboxes" code with:

if (!IsPostBack)
{

...

}

This will mean it only runs when the page is first loaded.


Have you looked if your sql update statement is passing the values as @.new_value

I would recommend doing this on a datagrid, it would be easier to update cuz the datagrid already has an update command field!


I cant use a datagrid as the recordset only ever returns one row with 17 fields. Datagrids need multirows to work dont they?


Datagrids work with "a number" of rows - that can be one row.


welshpunk:

I cant use a datagrid as the recordset only ever returns one row with 17 fields. Datagrids need multirows to work dont they?

Hi welshpunk,

Try to follow those codes as follows:

Working with a Single Table
http://msdn2.microsoft.com/en-us/library/aa719548(VS.71).aspx

Thanks.


Yes but you cannot turn a datagrid on its side. I have 17 fields in the recordset(just one row) Using a datagrid in this fashion is not user friendly as some of the fields are up to 2000 chars long. Datagrids will display like this

Column Heading Column Heading Column Heading Column Heading Column Heading

Data Data Data Data Data

I want to display the data in a more user friendly way like this

Column Header Data

Column Header Data

Column Header Data

Column Header Data

Column Header Data

Column Header Data

Column Header Data

With the 17 column headers on the left and their respective data adjacent to them. This is why I had to create the form in this way.


Hi,

You can write each cell every time by reading each cell once.

Hope this helps.


I have already marked as answer


Don't worry - you'll soon see that comment about marking posts as the answer absolutely everywhere!


You could try using a detailsview. The data would be displayed the way you want.