|
-
Nov 2nd, 2004, 11:05 AM
#1
Thread Starter
PowerPoster
Non-Shared Member reason
Hi,
In a recent thread something similar to the following was posted as giving the error message "Reference to a non-shared member requires an object reference"
formMain is the startup object
As Me.Text="Testing" is OK and if formMain had been instantiated in a module, with the module Sub Main being the starrtup object, formMain.Text=Testing would have been OK, can anyone please explain why the error message arises? I have never attempted to refer to a form by it's own name from within itself, so I have never noticed this before.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Nov 2nd, 2004, 11:16 AM
#2
Because as far as it is concerned there is no instantied form object named formMain.
Its just not the way you do it. It a rule of OOP. Dont really know a better way to explain that.
-
Nov 2nd, 2004, 11:23 AM
#3
VB6 creates an instance of the form with the same name (essentially). However in .net it works different.
When you are creating an instance of your form in your sub main, you might be doing this:
Dim frmForm as new formMain
then if you try this
formMain.Text="lkj" you are trying to set the value of a class definition (formMain is the definition) not the actual instance.
The instance is actually frmForm so you would call this instead:
frmForm.Text="ljhkjh"
-
Nov 2nd, 2004, 12:03 PM
#4
in VB6 it is called "Default instance" each form you make in VB has a default instance so you can just reference it by the name you called the form.. you can also dim a variable of that form as well
in .NET the second option in the only option, as they have done away with default instances... it may seem odd at first, i know it did to me, but as soon as you work with it once, it becomes clear and you realize that there is no reason to have default instances
-
Nov 2nd, 2004, 12:28 PM
#5
Thread Starter
PowerPoster
Thanks guys, I follow that but what puzzles me is that when you use formMain as the startup object, arn't you getting an automatic instance of it? If not just what have you got?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Nov 2nd, 2004, 01:52 PM
#6
within the form itself you should always use the Me keyword.. that is why it is there.
-
Nov 2nd, 2004, 01:59 PM
#7
Yes you do get an instance, the only difference is you dont have a reference to it in a global way, you would have to create it yourself. I have not seen a way of getting a global handle to it anyway.
In the case described above you could on the statup of the form, set a reference in a module to be self (newly created form).
I personally would use a sub main, as you can then add other functions, such as a global error handler. You can still call a form just like you would if it was the startup object, and you have a global reference to it. I can dig up and example if you like.
-
Nov 2nd, 2004, 05:15 PM
#8
Thread Starter
PowerPoster
Hi,
Many thanks. That has cleared the matter up. I will stick to using a Module Sub Main as the startup object, it's much cleaner.
With regard to "Me" I can see you have to use it to reference form properties in the quoted situation, but I have always omitted it when referencing contained objects.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Nov 2nd, 2004, 07:28 PM
#9
yeah it is purley optional... but it comes in handy when your form has like 100 controls and subs and you cant remember them all by name
-
Nov 2nd, 2004, 07:42 PM
#10
Thread Starter
PowerPoster
Originally posted by kleinma
yeah it is purley optional... but it comes in handy when your form has like 100 controls and subs and you cant remember them all by name
AHHH! I guess you are referring to mass access to objects for clearing etc. OK.
Where does your reference to Subs come in ?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
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
|