-
Oct 5th, 2013, 05:12 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] When are Class declarations treated as a Variable?
I've gotten used to cross-form access such as:
Code:
Form2.ShowDialog()
Form1.SelectedFile = Form2.SelectedDirectory & "\test.txt"
Form1.ShowDialog()
But recently something started bothering me...that is when I started using multiple instances of the same form.
For example, the above:
Code:
Dim f2 As New Form2()
f2.ShowDialog()
Dim f1 As New Form1()
f1.SelectedFile = f2.SelectedDirectory & "\test.txt"
f1.ShowDialog()
' Dispose it, etc.
The documentation on this is hard to find (or it is too obvious?) so I have some general questions related to this:
- How is this sort of 'Class instance access by Class name' called in VB .NET?
- Can I declare Classes that behave this same way, or is it only restricted to Forms?
Mainly on the second point, say I have a Class as follows:
Code:
Public Class MyClass
Public Sub DoStuff()
MsgBox("Stuff!")
End Sub
End Class
How can I access this as MyClass.DoStuff() without having to make an instance of MyClass?
This is mainly interesting in my case for some places where I register 'service' classes where only one instance is required.
Important: I am not talking about making DoStuff() Shared, as I need to keep everything Local to be able to use parameters stored in an inherited class.
Method access needs to remain Local, only MyClass needs to be interpreted as a Variable like is the case with Forms.
-
Oct 5th, 2013, 05:20 PM
#2
Re: When are Class declarations treated as a Variable?
you just do it the same way:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mc As New MyClass1
mc.DoStuff()
End Sub
End Class
Public Class MyClass1
Public Sub DoStuff()
MsgBox("Stuff!")
End Sub
End Class
MyClass is a reserved keyword in vb.net, so I changed it...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2013, 05:22 PM
#3
Re: When are Class declarations treated as a Variable?
here's an alternative:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyClass1.DoStuff()
End Sub
End Class
Public Class MyClass1
Public Shared Sub DoStuff()
MsgBox("Stuff!")
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2013, 05:25 PM
#4
Re: When are Class declarations treated as a Variable?
I've just read the question properly.
To use DoStuff as in post#3, it has to be Shared.
If DoStuff calls other methods, functions, or variables in MyClass1, they have to also be Shared.
Does that help?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2013, 05:26 PM
#5
Thread Starter
Fanatic Member
Re: When are Class declarations treated as a Variable?
Originally Posted by .paul.
here's an alternative:
I know how I can instantiate my class or make the method shared, like I mentioned in the OP. My problem, or rather, question is: how can this entire step be skipped for Forms, and can this also be skipped for other classes?
Let's put it this way: Why is it possible to do:
Without having to instantiate Form1? Form1 is a Class name, how can it be a Variable at the same time?
-
Oct 5th, 2013, 05:31 PM
#6
Re: When are Class declarations treated as a Variable?
Form1.ShowDialog() uses the default instance
if you want project level procedures without declaring instances, why not use a module?
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DoStuff()
End Sub
End Class
Public Module MyModule
Public Sub DoStuff()
MsgBox("Stuff!")
End Sub
End Module
you can call it like this:
but there's no need to reference the module name
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2013, 05:38 PM
#7
Thread Starter
Fanatic Member
Re: When are Class declarations treated as a Variable?
Originally Posted by .paul.
Form1.ShowDialog() uses the default instance
Default instance is the name, so then, can a 'Default Instance' be assigned for non-form classes as well? I specifically need this functionality because I need to store an Instance of this same Class.
Modules won't work since, as you already mentioned, the methods can be called from anywhere.
The fact that the method is only bound to that particular Class instance is required. (Reason being: other classes can inherit it, the class is inherited, methods are overridden)
I need to declare 'MyClass1' in such a way that it can be accessed the same way Forms can: with a default instance.
-
Oct 5th, 2013, 05:44 PM
#8
Thread Starter
Fanatic Member
Re: When are Class declarations treated as a Variable?
Actually, you already helped me quite a bit with the 'Default Instance' name. Looked it up and over here someone mentions that Form1.ShowDialog() is simply compiled as My.Forms.Form1.ShowDialog()
I'll mark it as resolved, as it is clearly just a little compiler feature that only works for forms, not other classes.
-
Oct 5th, 2013, 06:07 PM
#9
Re: When are Class declarations treated as a Variable?
Originally Posted by bergerkiller
Actually, you already helped me quite a bit with the 'Default Instance' name. Looked it up and over here someone mentions that Form1.ShowDialog() is simply compiled as My.Forms.Form1.ShowDialog()
Hans gave good advice on that post - default form instances are a terrible VB 'feature' that only helps to cripple developers' understanding of types vs instances. If you need an instance of a form, create an instance of the form, don't rely on an incoherent, badly-conceived VB feature that's only there to baby immature developers.
Last edited by David Anton; Oct 5th, 2013 at 10:05 PM.
-
Oct 5th, 2013, 09:48 PM
#10
Re: [RESOLVED] When are Class declarations treated as a Variable?
Default instances were added in 2005 to make .NET appear more similar to VB6. Prior to that they didn't exist, and ever since they were added they have been causing confusion. Generally, your startup form will be a default instance, but other than that I would suggest never using them. When you have a default instance, the compiler is quietly doing something like this:
Public Form1 As New Form1
In reality, the compiler is doing something slightly more efficient because it only creates that default instance when you first reference the default instance. Therefore, if you never use the default instance it is never created. This means that not using the default instances is just fine.
I believe you could make default instances for any class, though I have never tried it because default instances cause enough trouble for forms anyways. What you would have to do would probably be to add a module that had something like this in it:
Code:
Private mYourClassName As YourClassType
Public Function YourClassName() As YourClassType
if mYourClassName Is Nothing Then
mYourClassName = New YourClassType
End If
Return mYourClassName
End Function
Whether or not this would even compile I couldn't say without trying it, and I'm not inclined to try it because it's a pretty stupid idea.
My usual boring signature: Nothing
-
Oct 6th, 2013, 08:05 AM
#11
Thread Starter
Fanatic Member
Re: [RESOLVED] When are Class declarations treated as a Variable?
Thanks for the insightful information, and indeed, coming from other language where this sort of default instancing doesn't exist, it is a bit of a strange 'feature'. In my experience it is a bad idea to mix Class types and Variable names, which made me wonder why this was possible at all in VB .NET, and used so extensively as well. You all helped me greatly, and possibly others as well.
Tags for this Thread
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
|