Solved

VB 2008  - Err Msg says variable is incorrect

Posted on 2009-07-12
15
196 Views
Last Modified: 2012-05-07
At runtime I get this error on the 4th line, "if ActiveOval.Visible = False Then"
   Error Message="Object reference not set to an instance of an object."

There will ALWAYS be  ActiveOval1  thru  ActiveOval10

I don't know why the error occurs or how to overcome it.
OvalOK = 0

        For i As Integer = 1 To 10

            Dim ActiveOval As Control = GetControlByName("OvalShape" & i)

            If ActiveOval.Visible = False Then

                OvalOK = OvalOK + 1

            End If

        Next

Open in new window

0
Comment
Question by:ljcor
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24834183
>There will ALWAYS be  ActiveOval1  thru  ActiveOval10
according to the error message, this is not true.
you will have to double check.

note: your code says:
GetControlByName("OvalShape" & i)

and you write ActiveOvalxxx

please double check
0
 
LVL 4

Expert Comment

by:samierana
ID: 24834198
Use line 4 like this:
Dim ActiveOval As New Control = GetControlByName("OvalShape" & i)
0
 

Author Comment

by:ljcor
ID: 24834204
With the statement:             Dim ActiveOval As Control = GetControlByName("OvalShape" & i)
I thoughtActiveOval would always be referencing OvalShape1 (OvalShape2, etc.)

That was my intention.  Perhaps I am just not writing the code correctly or understanding it.

(Just as I was writing this I came to think that OvalShapes are not the same as other "normal" controls.  They are PowerPack controls.  I think they have to be handled differently; not by GetControlByName.)
0
 
LVL 7

Accepted Solution

by:
Valleriani earned 500 total points
ID: 24834237
Try this out.
Public Class Form1
 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim ovalOK As Integer

        ovalOK = 0

        For i As Integer = 1 To 10

            Dim ActiveOval As PowerPacks.Shape = GetShapeControlByName("OvalShape" & i)

            If ActiveOval.Visible = False Then

                ovalOK = ovalOK + 1

            End If

        Next

        MsgBox(ovalOK)

    End Sub
 

    Private Function GetShapeControlByName(ByVal shapeName As String) As PowerPacks.Shape

        For Each ShpCnt As PowerPacks.ShapeContainer In Me.Controls.OfType(Of PowerPacks.ShapeContainer)()

            For Each Shp As PowerPacks.Shape In ShpCnt.Shapes

                If Shp.Name.ToLower = shapeName.ToLower Then

                    Return Shp

                End If

            Next

        Next

        Return Nothing

    End Function

End Class

Open in new window

0
 

Author Comment

by:ljcor
ID: 24834252
Valleriani,

I had just dug out that identical routine.  It is one that IdleMind wrote for me for another powerPack control and purpose a few weeks ago.

Jack
0
 
LVL 7

Expert Comment

by:Valleriani
ID: 24834257
Oh and that would VBRocks's/IdleMind way of doing it.

But yeah, I believe GetControlByName doesn't work in this case though.
0
 
LVL 7

Expert Comment

by:Valleriani
ID: 24834259
Ah! Haha :) Glad to see that then. Does it work fine then? I tested it quickly and it seemed to be ok.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:samierana
ID: 24834263
The in your question is caused by NOT USING New keyword. Have you tried this?
Dim ActiveOval As Control = GetControlByName("OvalShape" & i)
on line 4? if yes what is the result?
0
 

Author Comment

by:ljcor
ID: 24834271
Valeriani -

Thanks to you for this line in your Form_Load example:
               Dim ActiveOval As PowerPacks.Shape = GetShapeControlByName("OvalShape" & i)

I would have struggled for that because I did not recall that part.  Although IdleMind wrote that a while back I ended up not using it because I made a substantial change to the previous form.

0
 

Author Comment

by:ljcor
ID: 24834283
Sameriana,

I did not try the line you suggested because I believe I had previously attempted it without result.  
0
 

Author Comment

by:ljcor
ID: 24834284
Sameriana,

I did not try the line you suggested because I believe I had previously attempted it without result.  
0
 
LVL 4

Expert Comment

by:samierana
ID: 24834288
The Error in your question is caused by NOT USING New keyword. Have you tried this?
Dim ActiveOval As Control = GetControlByName("OvalShape" & i)
on line 4? if yes what is the result?
0
 
LVL 7

Expert Comment

by:Valleriani
ID: 24834293
sam: Dim ActiveOval As New Control = GetControlByName("OvalShape" & i)  Didn't seem to work either.

Dim ActiveOval As Control = GetControlByName("OvalShape" & i) isn't even getting the first control properly by the looks of it though.

Most of my info came from this: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_24524105.html

Seems like Idle states to go with GetShapeControlByName for shapes, and everything else uses GetControl..etc

This is from idlemind:

"It looks like the PowerPacks components were included in the Visual Studio Service Pack 1 install.

See: http://msdn.microsoft.com/en-us/library/cc488275.aspx

    "When a Line or Shape control is added to a form or container, an invisible ShapeContainer object is created. The ShapeContainer acts as a canvas for the shapes within each container control; each ShapeContainer has a corresponding ShapeCollection that enables you to iterate through the Line and Shape controls."

My GetControlByName() function utilizes Controls.Find() and the reason it is failing is that the invisible ShapeContainer does NOT use the standard Controls() collection like all of the other intrinsic .Net controls.  Microsoft has done something fancy and instead holds all of the Shapes in a ShapeCollection:
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.powerpacks.shapecollection.aspx

So really the problem lies in the "trickery" of Microsoft...  =\

I'll have to play around with it a bit to find a workaround or a different approach to the problem."

That is what he wrote though.

Can read it here:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_24524105.html
0
 

Author Closing Comment

by:ljcor
ID: 31602564
I do believe that IdleMind wrote that but it is possible that VBRocks was the author.  I just don't recall right now.  At any rate your suggestion of it may help some other poor programmer soul at some point.  I thank everyone for their help on this problem and solution.
0
 

Author Comment

by:ljcor
ID: 24834324
Valeriani - Your final line reference to the IdleMind solution is the solution to the question I asked about.  As you can see, it took awhile for that answer to develop.  I am happy that we (together) brought it back to life.  Of course, I NEVER should have had it slip my mind in the first place.  Again, thanks for your help.  And IdleMind's.  Not to mention all of the other great experts who have so markedly helped me over these humps.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Object to array 7 29
Total in textboxes 9 36
Form design in vb.net 7 21
Disable and re-enable a dynamic System.Timers.Timer 6 28
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now