Sideway from Sideway
Draft for Information Only


VB.NET Variable Trobleshooting
 Unable to Access Members of an Object
  Correct Approach
 Other Components Cannot Access Your Variable
  Correct Approach
 Wrong Variable Being Used
  Correct Approach
 See also

VB.NET Variable Trobleshooting

This page lists some common problems that can occur when working with variables in Visual Basic.

Unable to Access Members of an Object

If your code attempts to access a property or method on an object, there are two possible error outcomes:

  • The compiler can generate an error message if you declare the object variable to be of a specific type and then refer to a member not defined by that type.

  • A run-time MemberAccessException occurs when the object assigned to an object variable does not expose the member your code is trying to access. In the case of a variable of Object Data Type, you can also get this exception if the member is not Public. This is because late binding allows access only to Public members.

When the Option Strict Statement sets type checking On, an object variable can access only the methods and properties of the class with which you declare it. The following example illustrates this.

Dim p As Object = New System.Windows.Forms.Label
Dim q As System.Windows.Forms.Label = New System.Windows.Forms.Label
Dim j, k As Integer
' The following statement generates a compiler error.
j = p.Left
' The following statement retrieves the left edge of the label 
' in pixels.
k = q.Left

In this example, p can use only the members of the Object class itself, which do not include the Left property. On the other hand, q was declared to be of type Label, so it can use all the methods and properties of the Label class in the System.Windows.Forms namespace.

Correct Approach

To be able to access all the members of an object of a particular class, declare the object variable to be of the type of that class when possible. If you cannot do this, for example if you do not know the object type at compile time, you must set Option Strict to Off and declare the variable to be of the Object Data Type. This allows objects of any type to be assigned to the variable, and you should take steps to ensure that the currently assigned object is of an acceptable type. You can use the TypeOf Operator to make this determination.

Other Components Cannot Access Your Variable

Visual Basic names are case-insensitive. If two names differ in alphabetic case only, the compiler interprets them as the same name. For example, it considers ABC and abc to refer to the same declared element.

However, the common language runtime (CLR) uses case-sensitive binding. Therefore, when you produce an assembly or a DLL and make it available to other assemblies, your names are no longer case-insensitive. For example, if you define a class with an element called ABC, and other assemblies make use of your class through the common language runtime, they must refer to the element as ABC. If you subsequently recompile your class and change the element's name to abc, the other assemblies using your class can no longer access that element. Therefore, when you release an updated version of an assembly, you should not change the alphabetic case of any public elements.

For more information, see Common Language Runtime.

Correct Approach

To allow other components to access your variables, treat their names as if they were case-sensitive. When you are testing your class or module, make sure other assemblies are binding to the variables you expect them to. Once you have published a component, do not make any modifications to existing variable names, including changing their cases.

Wrong Variable Being Used

When you have more than one variable with the same name, the Visual Basic compiler attempts to resolve each reference to that name. If the variables have different scope, the compiler resolves a reference to the declaration with the narrowest scope. If they have the same scope, the resolution fails and the compiler signals an error. For more information, see References to Declared Elements.

Correct Approach

Avoid using variables with the same name but different scope. If you are using other assemblies or projects, avoid using any names defined in those external components as much as possible. If you have more than one variable with the same name, be sure you qualify every reference to it. For more information, see References to Declared Elements.

See also




ID: 200900010 Last Updated: 9/10/2020 Revision: 0 Ref:



  1. Active Server Pages,  ,
  2. ASP Overview,  ,
  3. ASP Best Practices,  ,
  4. ASP Built-in Objects,  ,
  5. Response Object,  ,
  6. Request Object,  ,
  7. Server Object (IIS),  ,
  8. Application Object (IIS),  ,
  9. Session Object (IIS),  ,
  10. ASPError Object,  ,
  11. ObjectContext Object (IIS),  ,
  12. Debugging Global.asa Files,  ,
  13. How to: Debug Global.asa files,  ,
  14. Calling COM Components from ASP Pages,  ,
  15. IIS ASP Scripting Reference,  ,
  16. ASP Keywords,  ,
  17. Creating Simple ASP Pages,  ,
  18. Including Files in ASP Applications,  ,
  19. ASP Overview,  ,
  20. FileSystemObject Object,  ,
  21.,  , ADO Object Model
  22. ADO Fundamentals,  ,

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna

Home 5






Hobbies 8


Chinese 1097

English 337

Reference 68


Hardware 154


Application 207

Digitization 25

Latex 35

Manim 202

Numeric 19


Web 285

Unicode 504


CSS 60


OS 422

DeskTop 7

Python 61



Formulas 8

Algebra 65

Number Theory 206

Trigonometry 18

Geometry 22

Coordinate Geometry 1

Calculus 67

Complex Analysis 21


Tables 8


Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5


Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1

Copyright © 2000-2021 Sideway . All rights reserved Disclaimers last modified on 06 September 2019