Thread: Public classes
View Single Post
  #3 (permalink)  
Old October 22nd, 2006, 11:02 AM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts

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.:
public static class Helpers
  public static string SayHello()
    return "Hello World";

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:
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:

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

Hope this helps,

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.