Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 March 11th, 2008, 04:09 AM
Registered User
 
Join Date: Mar 2008
Location: Karachi, Sindh, Pakistan.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mjsoomro Send a message via Yahoo to mjsoomro
Default Difference b/w declaring and using object keyword

Hi,

I want to understand the difference b/w declaring object in class that is,

    myclass sample1 = new myclass()

    and using object o

please explain the difference!!

thanks

Mahmood Jalal Soomro
 
Old March 11th, 2008, 04:19 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Do you mean the difference between using:

myclass s = new myclass();

and

object o = new myclass();

?

Well for starters if myclass had a method called dothis(), then you could write the following:

s.dothis();

but you couldn't write this:

o.dothis();

I don't really see much point in your question because you only ever use object when you don't know what the class is.

/- Sam Judson : Wrox Technical Editor -/
 
Old March 11th, 2008, 08:52 AM
Registered User
 
Join Date: Mar 2008
Location: Karachi, Sindh, Pakistan.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mjsoomro Send a message via Yahoo to mjsoomro
Default

Thanks for the reply
 
Old March 11th, 2008, 09:31 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Mahmood,

Do you mean the different between:

    myclass s = new myclass();
    //Do stuff with s

versus

    using(myclass s = new myclass()){
        //do stuff with s
    }


The latter is basically a single block wrapper of this:

    myclass s = new myclass();
    try{
        //do stuff with s
    }
    finally{
        //dispose s properly
    }

Using this is a shortcut for cases where you want to ensure that resources are released/closed/cleaned up before processing continues whether there is an exception thrown by the contained code or not.

-Peter
peterlanoie.blog




Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Instance and Object in # Manoj Bisht C# 9 August 26th, 2008 09:22 AM
Difference b/w Active X & Data Object bhasker_braj121 ADO.NET 0 June 19th, 2007 04:09 AM
Declaring a dropdownlist Rod Merritt BOOK: ASP.NET Website Programming Problem-Design-Solution 0 August 1st, 2006 02:11 PM
declaring variables that are not used crmpicco VB How-To 2 May 19th, 2005 04:22 PM
Declaring and instantiating... jacob ASP.NET 1.0 and 1.1 Basics 4 August 21st, 2003 10:00 AM





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