Solved

class question

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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