Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 22nd, 2006, 09:30 AM
Registered User
 
Join Date: Oct 2006
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.

 
Old October 22nd, 2006, 10:23 AM
Wrox Author
 
Join Date: Oct 2005
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
 
Old October 22nd, 2006, 11:02 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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.
 
Old October 22nd, 2006, 03:53 PM
Registered User
 
Join Date: Oct 2006
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.

 
Old October 23rd, 2006, 01:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.