Solved

VB 2008  - Err Msg says variable is incorrect

Posted on 2009-07-12
15
199 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with checking if excel exist in client's computer 9 43
Help with error message for ReportViewer in VS2015 4 44
Spacing between controls 4 30
VS.net 2010 11 24
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

896 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

15 Experts available now in Live!

Get 1:1 Help Now