|
-
Feb 22nd, 2003, 06:09 PM
#1
Thread Starter
Sleep mode
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).
-
Feb 22nd, 2003, 07:26 PM
#2
Hyperactive Member
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.
-
Feb 22nd, 2003, 07:32 PM
#3
Thread Starter
Sleep mode
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 .
-
Feb 22nd, 2003, 07:33 PM
#4
Thread Starter
Sleep mode
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" ????
-
Feb 22nd, 2003, 07:38 PM
#5
Hyperactive Member
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?
-
Feb 22nd, 2003, 07:40 PM
#6
Thread Starter
Sleep mode
I want to make my class static just like path class ???
-
Feb 22nd, 2003, 07:43 PM
#7
Thread Starter
Sleep mode
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:
Namespace DataBaseClass
Public Shared Class SaveDB
End Class
End Namespace
-
Feb 22nd, 2003, 07:57 PM
#8
Hyperactive Member
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.
-
Feb 22nd, 2003, 08:02 PM
#9
Thread Starter
Sleep mode
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 .
-
Feb 22nd, 2003, 08:07 PM
#10
Thread Starter
Sleep mode
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 !!!!
-
Feb 22nd, 2003, 08:14 PM
#11
Hyperactive Member
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.
-
Feb 22nd, 2003, 08:18 PM
#12
Thread Starter
Sleep mode
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 )
-
Feb 22nd, 2003, 08:39 PM
#13
Hyperactive Member
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?
-
Feb 22nd, 2003, 08:46 PM
#14
yay gay
in vb.net u can have shared classes and shared objects
\m/  \m/
-
Feb 22nd, 2003, 08:50 PM
#15
Thread Starter
Sleep mode
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
-
Feb 22nd, 2003, 08:51 PM
#16
Thread Starter
Sleep mode
Originally posted by PT Exorcist
in vb.net u can have shared classes and shared objects
How PT Exorcist ???
-
Feb 22nd, 2003, 09:03 PM
#17
Hyperactive Member
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:
Public Class MyClass
Public Shared Sub Whatever()
'Do Something
End Sub
End Class
Public Class MyForm
Public Sub Form_KeyDown (whatever the event args are) handles Me.Keydown
MyClass.Whatever()
'Or you could do this instead:
Dim X as MyClass 'Notice no "New" keyword
X.Whatever()
End Sub
End Class
Last edited by Hu Flung Dung; Feb 22nd, 2003 at 09:06 PM.
-
Feb 22nd, 2003, 10:05 PM
#18
Thread Starter
Sleep mode
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|