Results 1 to 4 of 4

Thread: Is there any difference??

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    3

    Question Is there any difference??

    Hi all,

    When i want to create an object from x class!!
    i'm using two ways as bellow:

    1-
    dim a as random

    a=new random //using new


    MsgBox(x.Next())

    2-

    Dim b As System.IO.File
    b.Create("c:\h.zzz")


    How can i know that a class needs "new"(1) to create object or jus needs declaration(2) ??


    thanx in advance

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You can tell by what your doing. The Create method you shown is a 'static' method that is shared among all instances of the File object, and can be used just by calling System.File.IO.Create(). This is because a static method doesn't require an instance of the object to be created to use it. Its functionality will be the same no matter what instance your calling the method from.

    The opposite of that is what most objects do. They usually require an instance of the object to call methods because the methods you are calling affect ONLY that instance of the object, not all of them.

    Kind of confusing for me to explain, I hope that helps. I will try to find some links to sites that explain it better than I can...

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    If you go here:
    http://msdn.microsoft.com/library/de...rfSystemIO.asp

    You will see that the File class contains only static methods. The FileInfo class provides instance methods. The instance methods allow you to alter just that object.

    If you look at the documentation of the File.Create method, you can see by the declarations that it is a static class, and therefor you don't need to create an instance in order to use it.
    http://msdn.microsoft.com/library/de...reatetopic.asp


    Oh ya, I have been refering to it as 'static', but in VB.net, it is known as 'shared'. Hope I am not confusing you.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    3
    thanx hellswraith

    I appreciate ur efforts!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width