p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Professional (http://p2p.wrox.com/forumdisplay.php?f=57)
-   -   Public classes (http://p2p.wrox.com/showthread.php?t=49302)

myousman October 22nd, 2006 09:30 AM

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.


dparsons October 22nd, 2006 10:23 AM

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

Imar October 22nd, 2006 11:02 AM

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.

myousman October 22nd, 2006 03:53 PM

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.


Imar October 23rd, 2006 01:18 PM

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.


All times are GMT -4. The time now is 02:35 PM.

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