?
Solved

How to Create Instance of Existing Object via Casting

Posted on 2014-03-02
6
Medium Priority
?
210 Views
Last Modified: 2014-03-03
I am creating a DataGridView dynamically at run-time, and cant seem to be able to instantiate it as an object during casting in another (Form1) subroutine, so that I can change row labels.


Here is how I define the DataGridView in the Form1 Load procedure:


Dim DataGridView1 As New DataGridView

Open in new window


Later, in a subroutine, when I want to change the row labels (numrows is the number of rows), I can't get the cast to work correctly because the cast won't allow me to use "As New" -- as follows:

Dim DataGridview1 As DataGridView = CType(Me.Controls("DataGridView1"), DataGridView)
For i = 1 To numrows
        DataGridview1.Columns.Add(i, "V" & i)
Next

Open in new window



What is the correct way instantiate as an object a control that was spawned dynamically?
0
Comment
Question by:lep1
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 39900042
what error u got ?
0
 

Author Comment

by:lep1
ID: 39900177
The error is that DataGridView1 is Nothing
0
 
LVL 19

Accepted Solution

by:
Melih SARICA earned 1000 total points
ID: 39900217
try

me.controls.find("datagridview1")
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:lep1
ID: 39900343
It worked out that the the syntax

Dim DataGridView1 As New DataGridView

does not add the name "DataGridView1" to the control, so therefore it can't be found in Me.Controls().

I had to use:

DataGridView1.Name="DataGridView1" -->which corrected the problem of not finding the control.

Question: is there a difference between

Dim DataGridView1 As DataGridView

and

Dim DataGridView1 As New DataGridView

in terms of the Name property?
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 39900379
Dim DataGridview1 As DataGridView = CType(Me.Controls("DataGridView1"), DataGridView)

in this line, ur trying to find a control that is already on the page ? or trying to add a new one ?

I guess ur trying to find a newly created one..

so u ave to set a name, other wise ssytem ll gave a name automaticly.
0
 

Author Comment

by:lep1
ID: 39900396
ok, thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, left to enroll

864 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