• Advertisement

Archived

This topic is now archived and is closed to further replies.

VB .NET question please read

This topic is 5061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I program in VB 6.0 and am learning VB .NET 2003. When i try to say: Form1.Text = "Hello" It says "Reference to a non-shared member requires an object reference." And yes the name of the form is form1. Why? Because #1 it says it in the solution Explorer and #2 i did not change the name property and it is "Form1" by defalt! What is going on?! Yet when i type ME.Text = "Hello" it works fine! I am very confused considering the book that is teaching me how to program in VB .Net 2003 said i could do it that way and you could do that same thing in VB 6.0. Can anyone help me with this problem? I hope it is something dumb that can be fixed in 2sec because it is really getting aggrivating! I am going to try installing any patches i can now. I will reply to my forum if i fix it. Any input is wanted for this stupid problem. Thanks

Share this post

Share on other sites
Advertisement
Also if i try to change the name of my form using the Name property it says i have a build error! This is crazy! Can somone please help....This is crazy. And yes i tried to rebuild the solution too!

Share this post

Share on other sites
I have contacted microsoft. Unless you help me before then i will post their responce in this forum and hopefully it will help someone else if they have the same problem in the future. But please help me asap!

Share this post

Share on other sites
Post your code. There are so many silly possibilities that your breathless frustration is more comical than anything else.

For example, I misspelled namespace (as "namspace") in a C# project once and got very cryptic errors - parsers do that. Then I placed the Main method outside of the class definition (could tell because I still saw closing braces - which belonged, it turned out, to the namespace) and I got more crpytic error messages.

So post your code. You''d be surprised how simple it is. Oh, and go ahead and post your (relevant extract of) code first in the future. It takes a fair level of training and jargon to be able to completely specify a code problem without code.

Share this post

Share on other sites
Public Class Form1    Inherits System.Windows.Forms.Form#Region " Windows Form Designer generated code "    Public Sub New()        MyBase.New()        'This call is required by the Windows Form Designer.        InitializeComponent()        'Add any initialization after the InitializeComponent() call    End Sub    'Form overrides dispose to clean up the component list.    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)        If disposing Then            If Not (components Is Nothing) Then                components.Dispose()            End If        End If        MyBase.Dispose(disposing)    End Sub    'Required by the Windows Form Designer    Private components As System.ComponentModel.IContainer    'NOTE: The following procedure is required by the Windows Form Designer    'It can be modified using the Windows Form Designer.      'Do not modify it using the code editor.    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()        '        'Form1        '        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)        Me.ClientSize = New System.Drawing.Size(292, 266)        Me.Name = "Form1"        Me.Text = "Form1"    End Sub#End Region    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Form1.Text = "hello"    End SubEnd Class

Thank you for the tip about posting the code i will always do that.

The thing is is that VB generates all of the code automatically except for the 3rd to the last line Form1.Text = "hello". . I am following what the book says and Form1.Text is a valid command. All i am adding to the generated code is one line. This makes no sense.! Any help is wanted!

MSDN says you can do this

form1.FormBorderStyle = FormBorderStyle.FixedDialog

It will not let me do this! It has the same error! Yet if i replace form1 with ME then it works! Me refers to form1 !!

Any input is greatly needed! In the meantime i am going to reinstall visual studio. That is a common troubleshooter step. I will do this later on today because it is 1:22am and i am tired! THanks For any help you can give me guys!

<SPAN CLASS=editedby>[edited by - blackdark on April 11, 2004 2:00:28 AM]</SPAN>[/source]

[edited by - Blackdark on April 11, 2004 2:13:41 AM]

[edited by - blackdark on April 11, 2004 2:14:22 AM]

Share this post

Share on other sites
Okay, this is going to take a minute...

The class name is Form1. The instance referred to in the Form1_Load event handler is implicit. The object is not created within that scope (since it is an instance method), so you refer to it generically through the special variable Me. Here's the difference:
' create the object explicitly:Dim myForm1 as Form1myForm1.Text = "Hello"  ' assume Text is a public property
This works because you are creating and naming the instance, myForm1.
' instance method, implicit object:Public Class MyForm  Inherits System.Windows.Forms.Form   Public Sub MyForm_ChangeText()    Me.Text = "New text"  End Sub
Note that here I refer to the instance through the special variable Me, because I am not dealing with any particular instance but rather generically defining operations to be carried out on the current object. If I were to try to call that function from another piece of code:
Dim myForm2 as MyForm...myForm2.ChangeText()
See the difference?

I suggest getting a good book on VB.NET and paying special attention to the chapters on object-oriented programming in VB.NET.

[Edit: Formatting.]

[edited by - Oluseyi on April 11, 2004 2:25:02 AM]

Share this post

Share on other sites
This is very strange. Visual basic generates the code itself. I did not write anything. Yet i cannot use the form1.Text property. I do have a good book on programing with OOP and this is what it says to do. I use sams teach yourself visual basic .NET 2003. See i dont have to do OOP to create a form. Visual basic does it for me. Well at least it is supossed to. I follow the book step by step... Well actually there are 2 steps...Hit new project and then all i need to write is form1.text = "hello". That is it. Visual basic does the rest. I have contacted microsoft and they will be responding to me tomorrow. I will post their reply on this forum. Thank you for all your help with trying to figure out what is wrong.

Share this post

Share on other sites
Umm...he just told you EXACTLY why it doesnt work for you....

Lord Hen

"I am free of all prejudices. I hate everyone equally." - W. C. Fields

[edited by - Lord Hen on December 2, 2008 2:32:49 AM]

Share this post

Share on other sites
*sigh*

Try Me.Text = "hello" and see what happens.

Share this post

Share on other sites
First of all, don''t reinstall Visual Studio every time there is an error you can''t solve in 5 minutes.

Second of all, when you refer to a property like this:

ClassName.Property

It assumes that it''s shared, which means every class instance shares this property. However, the Text property is different for every instance of Form1, so you have to use an instance of Form1. In your case, the instance will be Me, which means this current form that you are working with.

Think about it like this. You don''t want to change the text of every Form1 to "Hello", just the current instance.

Share this post

Share on other sites
Just go to MSDN and look up the difference between static and instance members.

Share this post

Share on other sites
man, VB .NET takes on a while different viewpoint from VB6. The moderator guy just told you exactly what to do.

Form1 is the name of the ''actual'' class not an instance of the class so it will not work. But to reference the class itself when you don''t know the name of the instance(which you won''t when you create the actual class), you use the ''Me'' keyword.

But when programming code inside of a class, and being that the text field is local you could get rid of the whole

Form1.Text = "blah blah"

and just use

Text = "blah blah" though that is bad practice...so stick to

Me.Text = "blah blah i get it now"

k?

Share this post

Share on other sites
quote:
Original post by BlackDark
I do have a good book on programing with OOP and this is what it says to do. I use sams teach yourself visual basic .NET 2003. See i dont have to do OOP to create a form. Visual basic does it for me. Well at least it is supossed to. I follow the book step by step... Well actually there are 2 steps...Hit new project and then all i need to write is form1.text = "hello". That is it.

The book has an error, if it suggests those exact steps.
Do follow Oluseyi's advice, it really is the right solution to the "problem".

-Nik

EDIT: Added double quotes to the word "problem"

[edited by - Nik02 on April 11, 2004 3:41:53 PM]

Share this post

Share on other sites
Well i guess i have to get a new book. I just got my responce from microsoft and here it is.

Hi Robert,

Thank you for contacting Microsoft. My name is Li Xiong and I will be assisting you with this service request.

If I understand correctly, you are developing a Windows application in Visual Basic .NET. You have created a form named form1. You want to change the form's title, and so you have the following code to handle the Form.Load event:

Form1.Text="hello"

This code works fine in Visual Basic 6. However, in Visual Basic .NET, this code doesn't compile. There are two error messages:

1. Reference to a non-shared member requires an object reference.

2. 'Sub Main' was not found in 'WindowsApplication6.Form1'.

You want to know why such code doesn't work in Visual Basic .NET and how to solve it. Please let me know if I have misunderstood your concerns.

Visual Basic .NET does some changes in how forms are handled. The key difference between .NET and previous versions of Visual Basic is you need an instance of a form before you can display it or work with any of its controls or properties.

In Visual Basic 6, a special default instance of each form is automatically created for you, and allows you to use the form's name to access this instance. What this means is that the Visual Basic 6.0 code Form1.Text="hello" has the effect of showing the "default" instance of Form1, but it doesn't work at all in Visual Basic .NET. In .NET there is no default instance. Form1 refers only to the class that represents your form, and this class cannot be used without creating an instance.

This is why ME.Text = "Hello" works fine while Form1.Text="hello" doesn't work. In Visual Studio .NET, form1 is just a class name. The Text property can only be referenced by instance. 'ME' represents current instance and ME.Text = "Hello" works fine.

You may think Visual Basic .NET is complicated and perhaps not as good as Visual Basic 6. However, Visual Basic .NET is a real Object Oriented programming language while Visual Basic 6 is not. In Visual Basic .NET, the Class and Instance are two separate concepts. Class describes the common properties and methods of some kind while Instance represents a real entity of a given kind. Visual Basic .NET distinguishes the name of class and instance explicitly. It is a big improvement.

In previous editions of Visual Basic, given a name called form1, it may represent a class and you can create many form1 like this:

Set myFirstForm = New Form1 ‘Form1 is class

Set mySecondForm = New Form1 ‘Form1 is class

myFirstForm.Show

mySecondForm.Show

It may represent a real form instance and you can change the title like this:

Form1.Text="my first form" ‘Form1 is instance

In such codes, a given name can represent two different things! In Visual Basic .NET, you can only create an instance from a class and you can only change the instance's property setting. The class name and the instance name must be different. This is reasonable and logical object-oriented design.

The second error, 'Sub Main' was not found in 'WindowsApplication6.Form1', is related to the project settings. You can specify whether you start your application from a form object or from a procedure. You can change this setting in project settings.

To do so, please right click your project name in Solution Explorer, choose properties to open the properties setting window. In 'General' category, you can choose the startup object. If you choose one of the existing forms, when your program starts, a new instance of the specified form is created automatically. If you choose 'Sub Main', you must define a shared Main procedure in one of your class and your program will be executed from the Main procedure.

____________________________________________________________

Well oh well guys....I have no doubt that microsoft was right and you guys were right now and that my book was wrong! What book should i get guys? Have any recomendations Oluseyi? I am sorry for doubting that you were correct its just i have a book sitting here telling me i can do it that way! I do hope you understand. Thank you for your time everyone.

[edited by - blackdark on April 12, 2004 5:12:19 AM]

Share this post

Share on other sites
/me has a feeling that was a cut & paste response.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post

Share on other sites
I recently dove into VB.NET and the book I used (which I found pretty good) was "Programming Microsoft Visual Basic .NET" by Francesco Balena. It is the "core reference" but he talks a lot about making the step from VB 6 - .NET.

Share this post

Share on other sites

• Advertisement
• Advertisement
• Popular Tags

• Advertisement
• Popular Now

• 10
• 12
• 10
• 10
• 11
• Advertisement