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).
Printable View
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.Quote:
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).
"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 .Quote:
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.
maybe because its has the flag "NotInheritable" ????:rolleyes:Quote:
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.Quote:
Originally posted by Pirate
Path is "Public NotInheritable Class Path" even though I can use it without instantiating an 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?
I want to make my class static just like path class ???
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
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:
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?Code:Dim strMyExtension as String
strMyExtension = System.IO.Path.GetExtension("C:\Whatever.txt")
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.
if you declared the constructor as private you won't be able to instantiate an object .Quote:
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:
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?Code:Dim strMyExtension as String
strMyExtension = System.IO.Path.GetExtension("C:\Whatever.txt")
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.
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 !!!!:rolleyes:
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.;)Quote:
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 !!!!:rolleyes:
(I would suggest to read more about OOP in VB.NET:D )Quote:
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.
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?
in vb.net u can have shared classes and shared objects
Well thanx Hu Flung Dung for trying to help .I think you didn't get it yet !!!Quote:
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?
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
How PT Exorcist ???:DQuote:
Originally posted by PT Exorcist
in vb.net u can have shared classes and shared objects
Umm..... Hmm.......Quote:
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
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
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 :D