Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

class question

Posted on 2004-08-24
4
Medium Priority
?
175 Views
Last Modified: 2012-05-05
im writing a vb app and having most of my functions a part of different classes.

my question is, if i know every form i send this function will have the same control, why wont it let me compile my code?

for example, heres a fake function

Public Function DoSomething(ByVal count As Integer, ByRef frm As Form, ByRef grid as DataGrid)

     'this is the code i want but wont work
     if frm.grid.Columns.Recordcount = 10 Then
          'blah
     End If

End Function

thank you!

~b
0
Comment
Question by:bramsquad
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 11886604
You could try this approach instead:

    Public Function DoSomething(ByVal count As Integer, ByRef frm As Form)
        Dim c As Control
        Dim grid As DataGrid

        ' find the DataGrid control on frm having the name "grid"
        For Each c In frm.Controls
            If c.Name = "grid" And c.GetType.ToString = "System.Windows.Forms.DataGrid" Then
                grid = c
                Exit For
            End If
        Next

        ' if it was found...
        If Not (grid Is Nothing) Then
            ' ...do something with it
            grid.Visible() = Not grid.Visible()
        End If
    End Function
0
 
LVL 5

Expert Comment

by:TRUENEUTRAL
ID: 11889138
easy... you may know it will be there, but the compiler doesn't.

Idle_Mind's code should do the trick.

OR

you should be able to form your function like this, as long as the form is included in your project.

Public Function DoSomething(ByVal count As Integer, ByRef frm As myForm, ByRef grid as DataGrid)

where myForm is your form name

but I would go with Idle_Mind's code... safer and more re-usable
0
 
LVL 10

Expert Comment

by:123654789987
ID: 11890857
U cannot access the controls of other forms directly like this.

1. In this case if u want only the Record count of the particular datagrid then u can pass that as a parameter to the function.

2. If u want the datasource of the datagrid for some other operations as well. U can store this datasource(Eg: datatable) as a static public property in the parent form of the datagrid control. U can then access the property from other forms
0
 
LVL 2

Expert Comment

by:Mehrdad_Y
ID: 11895993
You have to use Interfaces to solve this problem:

Public Interface [Your Interface Name]
   Public ReadOnly Property [Your Control Name] As [Your Control Type]
End Interface

Then you have to make all your forms to implement this interface:

Public Class Form1
   Inherits System.Windows.Forms.Form
   Implements [Your Interface Name]
   ...

   Public ReadOnly Property [Your Control Name] As [Your Control Type] Implements [Your Interface Name].[Your Control Name]
      Get
         Return Me.[Your Control Name]
      End Get
   End Property
...
End Class

Now all your forms that are implementing this interface, will have this control in their member list.
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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Integration Management Part 2
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

916 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