Results 1 to 18 of 18

Thread: shared class ?

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    shared class ?

    How can I make a class or namespace static .I think it's possible as I can see many shared classes in .net . How did they make static classes such as (msgbox , path ...etc).

  2. #2
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261

    Re: shared class ?

    Originally posted by Pirate
    How can I make a class or namespace static .I think it's possible as I can see many shared classes in .net . How did they make static classes such as (msgbox , path ...etc).
    In VB.NET, a module acts like a shared class, but I think its just compiled as a regular class with all of the members shared.

    "msgbox, path, etc.." are functions of an object. Not objects themselves. I don't think you can make shared objects and namespaces, only shared class members.

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: Re: shared class ?

    Originally posted by Hu Flung Dung
    In VB.NET, a module acts like a shared class, but I think its just compiled as a regular class with all of the members shared.

    "msgbox, path, etc.." are functions of an object. Not objects themselves. I don't think you can make shared objects and namespaces, only shared class members.
    Path is "Public NotInheritable Class Path" even though I can use it without instantiating an object .

  4. #4

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: Re: Re: shared class ?

    Originally posted by Pirate
    Path is "Public NotInheritable Class Path" even though I can use it without instantiating an object .
    maybe because its has the flag "NotInheritable" ????

  5. #5
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261

    Re: Re: Re: shared class ?

    Originally posted by Pirate
    Path is "Public NotInheritable Class Path" even though I can use it without instantiating an object .
    Oh, I see what you mean! I forgot there was a 'Path' object.

    Anyway, any shared object member can be used without instantiating the object. For example, you can put the following line in your code:

    Int16.Parse("1234")

    This would (of course) return an Int16 value of 1234. You didn't have to create a new Int16 object to use that function because Parse is shared.

    Is this what you mean?

  6. #6

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I want to make my class static just like path class ???

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This is what I want but compiler doesn't accept it ??
    I dunno if there is another way around can do the trick !
    VB Code:
    1. Namespace DataBaseClass
    2.  
    3. Public Shared Class SaveDB
    4.  
    5.  
    6.  
    7.  
    8. End Class
    9.  
    10. End Namespace

  8. #8
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    I don't quite understand what you mean anymore. Any shared object member can be used without instantiating that object, no matter how the class is declared.

    In the case of the 'Path' class, are you talking about the ability to just do this:

    Code:
    Dim strMyExtension as String
    strMyExtension = System.IO.Path.GetExtension("C:\Whatever.txt")
    Oh, wait a minute. I just realized that you cannot even create a new Path object (I havn't used the Path object before, so I'm experimenting in .NET while writing this post). Are you talking about the fact that there are no 'New' methods available?

    Hmm, I wonder what would happen if you declared your constructor (New method) as Private?

    I'll look into it some more, when I have the time.

    EDIT:

    Just replacing some words with more accurate terminology.
    Last edited by Hu Flung Dung; Feb 22nd, 2003 at 08:00 PM.

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Hu Flung Dung
    I don't quite understand what you mean anymore. Any shared object member can be used without creating that object, no matter how that object is declared.

    In the case of the 'Path' class, are you talking about the ability to just do this:

    Code:
    Dim strMyExtension as String
    strMyExtension = System.IO.Path.GetExtension("C:\Whatever.txt")
    Oh, wait a minute. I just realized that you cannot even create a new Path object (I havn't used the Path object before, so I'm experimenting in .NET while writing this post). Are you talking about the fact that there are no 'New' methods available?

    Hmm, I wonder what would happen if you declared your constructor (New method) as Private?

    I'll look into it some more, when I have the time.
    if you declared the constructor as private you won't be able to instantiate an object .

  10. #10

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I'm not talking about shortening namespaces . Maybe it is kind of confusion but is it possible to set all my classes within my namespace as Shared (static) .I know (func , methods , subs within a class) can be shared . I didn't mean this
    Clear !!!!

  11. #11
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    Originally posted by Pirate
    I'm not talking about shortening namespaces . Maybe it is kind of confusion but is it possible to set all my classes within my namespace as Shared (static) .I know (func , methods , subs within a class) can be shared . I didn't mean this
    Clear !!!!
    How is a shared class supposed to be different from a regular class with all shared members? Sorry if I seem a bit thick-headed now, I just can't think straight. Maybe I should go to bed early tonight.
    Last edited by Hu Flung Dung; Feb 22nd, 2003 at 08:17 PM.

  12. #12

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Hu Flung Dung
    How is a shared class supposed to be different from a regular class with all shared members? Sorry if I seem a bit thick-headed now, I just can't think straight.
    (I would suggest to read more about OOP in VB.NET )

  13. #13
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    Hey, I did some searches on google.

    Apparently, you can declare your constructor as shared so it gets fired the first time your object is accessed, at least according to http://www.microsoft.com/mspress/boo...chap/6199b.asp

    I didn't know you could do this. It may turn out to be quite useful. My VB.NET book never covered this.

    Is this what your looking for?

  14. #14
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    in vb.net u can have shared classes and shared objects
    \m/\m/

  15. #15

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Hu Flung Dung
    Hey, I did some searches on google.

    Apparently, you can declare your constructor as shared so it gets fired the first time your object is accessed, at least according to http://www.microsoft.com/mspress/boo...chap/6199b.asp

    I didn't know you could do this. It may turn out to be quite useful. My VB.NET book never covered this.

    Is this what your looking for?
    Well thanx Hu Flung Dung for trying to help .I think you didn't get it yet !!!
    I don't want to do this
    dim cls as new myclass

    I want to use the class directly in my code like so
    cls.somemethod

  16. #16

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by PT Exorcist
    in vb.net u can have shared classes and shared objects
    How PT Exorcist ???

  17. #17
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    Originally posted by Pirate
    Well thanx Hu Flung Dung for trying to help .I think you didn't get it yet !!!
    I don't want to do this
    dim cls as new myclass

    I want to use the class directly in my code like so
    cls.somemethod
    Umm..... Hmm.......
    Isn't this what I've been telling you all along?

    VB Code:
    1. Public Class MyClass
    2.  
    3.      Public Shared Sub Whatever()
    4.           'Do Something
    5.      End Sub
    6.  
    7. End Class
    8.  
    9. Public Class MyForm
    10.  
    11.      Public Sub Form_KeyDown (whatever the event args are) handles Me.Keydown
    12.  
    13.           MyClass.Whatever()
    14.  
    15.           'Or you could do this instead:
    16.           Dim X as MyClass 'Notice no "New" keyword
    17.           X.Whatever()
    18.  
    19.      End Sub
    20.  
    21. End Class
    Last edited by Hu Flung Dung; Feb 22nd, 2003 at 09:06 PM.

  18. #18

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It was my fault guys while I was tracking through my code I found
    this "Public shared class DBCon" inside the long namespace so
    the compiler didn't accept it and kept saying this
    (Class or Module cannot be declared Shared)
    sorry

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