Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

class question

Posted on 2004-08-24
4
Medium Priority
?
174 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

670 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