how to declare the function below

Hi Experts,

May I know how to declare the function below.
Thanks,

Turbot

    Function BuildTable(ByVal RowsNumber As Integer, ByVal ColumnsNumber As Integer)
        Dim i, j As Integer
        Me.Table1.Rows.Clear()
        For j = 1 To RowsNumber
            Dim row As New TableRow
            For i = 1 To ColumnsNumber
                Dim Cell As New TableRow
                'Cell.t = Rnd()
                Cell.BorderColor = Color.Blue
                Cell.BorderWidth = Unit.Pixel(1)
                row.Cells.Add(Cell)
            Next
            Me.Table1.Rows.Add(row)
        Next
        Me.Table1.BorderColor = Color.AliceBlue
        Me.Table1.BorderStyle = BorderStyle.Groove
        Me.Table1.BorderWidth = Unit.Pixel(1)
    End Function
turbot_yuAsked:
Who is Participating?
 
S-TwilleyConnect With a Mentor Commented:
If by declare you actually mean call, then you can run use the following line:

BuildTable(5, 3)

... but as the others have said, the function doesn't return anything so either change it to a Sub, or add the lines to return something whether it be a reference to the table created or a boolean expression saying whether it was a success or not:

==================

DECLARE AS A SUB:

    Sub BuildTable(ByVal RowsNumber As Integer, ByVal ColumnsNumber As Integer)
        Dim i, j As Integer
        Me.Table1.Rows.Clear()
        For j = 1 To RowsNumber
            Dim row As New TableRow
            For i = 1 To ColumnsNumber
                Dim Cell As New TableRow
                'Cell.t = Rnd()
                Cell.BorderColor = Color.Blue
                Cell.BorderWidth = Unit.Pixel(1)
                row.Cells.Add(Cell)
            Next
            Me.Table1.Rows.Add(row)
        Next
        Me.Table1.BorderColor = Color.AliceBlue
        Me.Table1.BorderStyle = BorderStyle.Groove
        Me.Table1.BorderWidth = Unit.Pixel(1)
    End Sub

---
    Call the sub:      BuildTable(5, 3)

==================

DECLARE AS FUNCTION WITH REFERENCE TO A TABLE:

    Function BuildTable(ByVal RowsNumber As Integer, ByVal ColumnsNumber As Integer) As Table
        Dim i, j As Integer
        Me.Table1.Rows.Clear()
        For j = 1 To RowsNumber
            Dim row As New TableRow
            For i = 1 To ColumnsNumber
                Dim Cell As New TableRow
                'Cell.t = Rnd()
                Cell.BorderColor = Color.Blue
                Cell.BorderWidth = Unit.Pixel(1)
                row.Cells.Add(Cell)
            Next
            Me.Table1.Rows.Add(row)
        Next
        Me.Table1.BorderColor = Color.AliceBlue
        Me.Table1.BorderStyle = BorderStyle.Groove
        Me.Table1.BorderWidth = Unit.Pixel(1)
        Return Me.Table1
    End Function

---
    Call the function:
          Dim myTable as Table
          myTable = BuildTable(5, 3)
          ' Then do something with myTable, which is a reference to Table1

*I'm assuming the type is TABLE and not something else here


DECLARE AS FUNCTION WITH REFERENCE TO A SUCCESS OR FAILURE:

    Function BuildTable(ByVal RowsNumber As Integer, ByVal ColumnsNumber As Integer) As Boolean
        Dim i, j As Integer
        Try
            Me.Table1.Rows.Clear()
            For j = 1 To RowsNumber
                Dim row As New TableRow
                For i = 1 To ColumnsNumber
                    Dim Cell As New TableRow
                    'Cell.t = Rnd()
                    Cell.BorderColor = Color.Blue
                    Cell.BorderWidth = Unit.Pixel(1)
                    row.Cells.Add(Cell)
                Next
                Me.Table1.Rows.Add(row)
            Next
            Me.Table1.BorderColor = Color.AliceBlue
            Me.Table1.BorderStyle = BorderStyle.Groove
            Me.Table1.BorderWidth = Unit.Pixel(1)
            Return True
        Catch
            Return False
        End Try
    End Function

---
    Call the function:
          If BuildTable(5, 3) = True Then
               'Table was rebuilt correctly
          Else
               ' Something went wrong
          End If
          ' Then do something with myTable, which is a reference to Table1
=========================

One thing to note is the because you've used the "Me" operator, you can only use this code, and call this code within the document class you are working in (or within other classes that at least contain an object called Table1).

If I'm running ahead of myself here, it's because as the others say, you have declared it, and I'm just taking a guess what you actually meant
0
 
Ramesh SrinivasConnect With a Mentor Technical ConsultantCommented:
I dont understand, you have declared it!
0
 
RonaldBiemansConnect With a Mentor Commented:
I agree with saleek, the only thing I can see wrong with it, is that the function doesn't return anything
0
 
DhaestConnect With a Mentor Commented:
I agree with the 2 others.
Another little remark is: is it a private or public function ?
Why use a function and not a sub if it returns nothing ?
0
 
farsightConnect With a Mentor Commented:
S-Twilley: Good, thorough answer!  I'm following up on your comment "because you've used the "Me" operator ...".

Here's another variation.  I renamed to FormatTable because I think of "Build" as creating from nothing.  This routine doesn't create the table, it uses the existing table Me.Table1.  What it does is resize the table (row & columns) and apply some color/border formatting.

Also, I passed in Me.Table1 to the TheTable parameter.  Now the routine can be used on ANY existing table.

==================

DECLARE AS A SUB:

    Sub FormatTable(ByVal TheTable As Table, ByVal RowsNumber As Integer, ByVal ColumnsNumber As Integer)
        Dim i, j As Integer
        TheTable.Rows.Clear()
        For j = 1 To RowsNumber
            Dim row As New TableRow
            For i = 1 To ColumnsNumber
                Dim Cell As New TableRow
                'Cell.t = Rnd()
                Cell.BorderColor = Color.Blue
                Cell.BorderWidth = Unit.Pixel(1)
                row.Cells.Add(Cell)
            Next
            TheTable.Rows.Add(row)
        Next
        TheTable.BorderColor = Color.AliceBlue
        TheTable.BorderStyle = BorderStyle.Groove
        TheTable.BorderWidth = Unit.Pixel(1)
    End Sub

---
    Call the sub:      FormatTable(Me.Table1, 5, 3)
0
All Courses

From novice to tech pro — start learning today.