Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 22nd, 2006, 09:30 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Public classes

Is it correct that if you declare a class as public that you don't have to instantiate it? Like if you have a public class "Tools", which has a method called "isLoggedIn", you can refer to Tools.IsLoggedIn() without having declared a variable and set it to a new instance of Tools?

If that is not the case, I'm having trouble understanding that portion of the case study in "beginning asp.net databases using vb.net" (.net v. 1.0) (Wrox)

Thanks if you can confirm this for me.

  #2 (permalink)  
Old October 22nd, 2006, 10:23 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Always instantiate your objects; If nothing else it saves typing.

While i could do

int i = <namespace>.<class>.method/function;

it is easier if i do
<namespace>.<class> myCls = New <namespace>.<class>();

then my original line becomes int i = myCls.method/function;

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
  #3 (permalink)  
Old October 22nd, 2006, 11:02 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Doug: this isn't true. There is no need to instantiate a class every time you need some functionality. It all depends on your class design. .NET (and other programming languages) come with something called static (or Shared) members. These members operate on the class, rather than on an instance of that class. E.g.:
Code:
C#
public static class Helpers
{
  public static string SayHello()
  {
    return "Hello World";
  }
}

VB.NET
Public Class Helpers
  Public Shared Function SayHello() As String
    Return "Hello World"
  End Function
End Class
With this class, you can use the SayHello method like this:
Code:
Label1.Text = Helpers.SayHello();
You'll find that you can't call instance members on the class, but only on an instance of that class. The reverse is true for static members: you can only call them on the class, but not on an instance of that class.

Static / shared methods are ideal for helper functions that don't require an instance.

myousman: The Public access modifier is to determine the visibility of a class or member. E.g. public members / class can be used anywhere. Private classes / members can only be used within the class that defines them. Besides these two, you also may run into Friend and Protected in Visual Basic. Take a look here for some more background:

http://visualbasic.about.com/od/usin...heritancea.htm
http://msdn2.microsoft.com/en-us/library/76453kax.aspx

What you described in your original post is the shared / static functionality.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #4 (permalink)  
Old October 22nd, 2006, 03:53 PM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar: that helps very much. I had a vague recollection of this distinction; i.e., a class that did not have to be instantiated to be used; but could not put my finger on the specifics. Thank you for a very clear explanation; it is the shared/static functionality of the class member that allows the class to be used in this way; i.e., without an explicit instantiation in the code. Wow! what an excellent response. I am going to use this forum more often. Thanks again.

  #5 (permalink)  
Old October 23rd, 2006, 01:18 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're welcome.

One more thing: I marked the class as static in C# as well. This is actually a .NET 2 feature that allows you to mark an entire class as static which in turns makes the compiler force you to make every method in the class static.

In .NET 1.x you can leave out the static keyword on the class and things will still work.

See you next time then.... ;)

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
public article - public articledetails _keysersoze_ BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 September 8th, 2007 08:38 AM
public sub or function ? kscase Visual Basic 2005 Basics 3 May 20th, 2007 03:14 PM
public functions stojkovmarjan ASP.NET 2.0 Basics 1 September 19th, 2006 03:57 PM
Can an object reference be public? maxpotters Excel VBA 2 March 6th, 2005 01:22 PM
How to make a class public johnjonsson VS.NET 2002/2003 0 October 6th, 2003 02:17 PM



All times are GMT -4. The time now is 06:15 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.