Solved

VB 2008  - Err Msg says variable is incorrect

Posted on 2009-07-12
15
203 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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