ASP.NET
  Home arrow ASP.NET arrow Page 3 - Using Objects in ASP.NET: Part 1/2
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Using Objects in ASP.NET: Part 1/2
By: Wrox Team
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 39
    2003-02-25

    Table of Contents:
  • Using Objects in ASP.NET: Part 1/2
  • Some of the Grander OOP Features
  • Class Members
  • Class Accessibility
  • The Constructor
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Objects in ASP.NET: Part 1/2 - Class Members


    (Page 3 of 6 )

    To get more granular, classes themselves are made up of members. Members generally consist of the class’ data (or fields) and its functionality (or methods). Both C# and VB (and presumably nearly all .NET languages) build classes using constructs below.
    • Constructors – Constructors are procedures that run when a class is instantiated as an object using the new statement mentioned above. They are useful for initializing values in fields or properties (see below) or running any other code that needs to run when an object is instantiated. A constructor is defined as follows:

      Public Sub New()
      ... 'Code to make the Constructor do something
      End Sub


      Since constructors cannot return values, the VB.NET compiler will return an error if you try to define a Constructor as a Function rather than a Sub.
    • · Fields – While variables can be declared anywhere, fields are variables declared at the beginning of the class definition. A variable declared as a field is accessible from anywhere in that class. Variables declared within a function or procedure, are only accessible within that function or procedure. Furthermore a variable can be a simple value such as a character or integer, or a more complex value such as another object. To define a public field in VB.NET you would use the following:

      Public FieldName as ValueType
      ...
      End Sub


      Fields are accessed from outside of your class using the following syntax:

      Value = ObjectName.Fieldname

      ObjectName.Fieldname = NewValue
    • · Methods – Functions and Procedures in VB are methods. They are defined within a class and therefore a member of that class.

      Public Sub MethodName()
      ...
      End Sub


      or to write a function that will return a value:

      Public Function MethodName() As ValueType
      ...
      return newvalue
      End Function


      If you are used to functions in VB6, you should be aware of the syntax change here. VB.NET now uses the return statement like Java, Jscript and C# to return a value.
    • · Properties – A good analogy for Properties might be to consider them a "hybrid": a cross between a field and a method. If you have used properties in VB 6, they haven’t changed that much. When defined within your code, properties can perform any code that a method can and usually (although not necessarily) encapsulate a matching field within the class. However, when used from another class, properties are accessed as if they are standard fields. This enables the developer writing the properties to ensure that the fields they manipulate never get set to values that would cause errors or data loss within your software. A "Get" block in the property definition is created with code that executes when the property is read. Then a "Set" block is created to define what values in the class change when the property is written to. In their simplest form, properties are defined in the manner below. But it is important to note that within the Get and Set blocks, and code can be written to make the properties do something.

      Public Property PropertyName() As ValueType
      Get
      Return Value
      End Get

      Set(ByVal Value As ValueType)
      FieldName = Value
      End Set
      End Property


      When the properties are read from, they are accessed as if you are reading a field from an object:

      SomeValue = MyObject.Property

      Similarly, when written to, the property is accessed like so:

      MyObject.Property = NewValue

      Be aware that in VB.NET, unlike VBScript and VB6, there are not default properties. If you refer to an object in your code, there is no default property that will return a value – you’ll be changing or reading the reference to that object.
    Member Accessibility
    You’ll notice in the fields, methods and properties above, I used the keyword Public. This means that the member being defined is accessible, that is can be read from and/or written to, from another class or even another application. However, there are many different types of access in .NET that you can define for a class member. To make the class easier to use or prevent errors, you can limit your members in various ways:
    • Public – At this level of access, the data member can be accessed from outside the class by any other class.
    • Protected – A protected member can only be accessed by members within the same class, or by a derivative of that class using Inheritance. We’ll get to inheritance and protected methods later on.
    • Friend – A member declared as Friend can be accessed by any other class in the same project.
    • Protected Friend – the best of the previous two access levels, protected friend means the member can be accessed by the same class, a derivative class or any classes in the same project.
    • Private – This is the most restrictive access you can define within a class. A member defined as Private can only be accessed within that class. If you define a member using only the Dim statement, the member is considered Private. However, it is a better practice to use Private instead of Dim to declare these types of members and it is easier to read and understand.

    More ASP.NET Articles
    More By Wrox Team


     

    ASP.NET ARTICLES

    - How Caching Means More Ca-ching, Part 2
    - How Caching Means More Ca-ching, Part 1
    - Reading a Delimited File Using ASP.Net and V...
    - What is .Net and Where is ASP.NET?
    - An Object Driven Interface with .Net
    - Create Your Own Guestbook In ASP.NET
    - HTTP File Download Without User Interaction ...
    - Dynamically Using Methods in ASP.NET
    - Changing the Page Size Interactively in a Da...
    - XML Serialization in ASP.NET
    - Using Objects in ASP.NET: Part 1/2
    - IE Web Controls in VB.NET
    - Class Frameworks in VB .NET
    - Cryptographic Objects in C#: Part 1
    - Sample Chapter: Pure ASP.Net







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek