Solved

class question

Posted on 2004-08-24
4
169 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 85

Accepted Solution

by:
Mike Tomlinson earned 125 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 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