Jump to content
  • Advertisement

Archived

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

BlackDark

VB .NET question please read

This topic is 5328 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


Link to 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


Link to 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


Link to 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


Link to 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 Sub
End 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


Link to 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 Form1
myForm1.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


Link to 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


Link to 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


Link to 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


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!