Solved

class question

Posted on 2004-08-24
4
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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