• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

VB 2008 - Err Msg says variable is incorrect

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
ljcor
Asked:
ljcor
  • 7
  • 4
  • 3
  • +1
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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
 
samieranaCommented:
Use line 4 like this:
Dim ActiveOval As New Control = GetControlByName("OvalShape" & i)
0
 
ljcorAuthor Commented:
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
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
VallerianiCommented:
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
 
ljcorAuthor Commented:
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
 
VallerianiCommented:
Oh and that would VBRocks's/IdleMind way of doing it.

But yeah, I believe GetControlByName doesn't work in this case though.
0
 
VallerianiCommented:
Ah! Haha :) Glad to see that then. Does it work fine then? I tested it quickly and it seemed to be ok.
0
 
samieranaCommented:
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
 
ljcorAuthor Commented:
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
 
ljcorAuthor Commented:
Sameriana,

I did not try the line you suggested because I believe I had previously attempted it without result.  
0
 
ljcorAuthor Commented:
Sameriana,

I did not try the line you suggested because I believe I had previously attempted it without result.  
0
 
samieranaCommented:
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
 
VallerianiCommented:
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
 
ljcorAuthor Commented:
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
 
ljcorAuthor Commented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now