Solved

Duplicating asp:Table object with VB (ByVal, not ByRef)

Posted on 2011-09-12
2
483 Views
Last Modified: 2012-06-22
I have an asp:Table object (PlanTable) which is set dynamically. And I have a Table shared variable (_Plan2). What I want is to duplicate one into another. I've tried many things:

1) _Plan2 = PlanTable (seems to work, but doesn't load anything).
2) For a = PlanTable.Rows.Count - 1 To 0 Step -1
  _Plan2.Rows.Add(PlanTable.Rows(a))
Next  (doesn't work. Adds the rows, but removes from the other table)
3)   Protected Sub DuplicateTables(ByVal T1 As Table, ByRef T2 As Table)
    For a = T1.Rows.Count - 1 To 0 Step -1
      T2.Rows.Add(T1.Rows(a))
    Next
  End Sub (doesn't work either. I thought by sending one of the tables with ByVal argument, it wouldn't be changed, but it is)

I don't know how I should do this correctly. I've thought about cycling through the cells as well, creating a new TableCell variable and copying the attributes one by one, but not only is that kinda extensive (there are quite a few number of cells I have to copy), some cells actually have controls inside them. Some may have Labels, some Buttons and some Textboxes and MaskedEditValidators. To use a loop for the cells, I would have to check for controls and create copies of them, even without knowing which type they are.

Anyway, anyone have a good suggestion for this? My biggest problem is leaving the source table intact (especially because, later in the code, they switch places). Seeing as I already have the Sub created, we might use the above coded DuplicateTables(ByVal T1 As Table, ByRef T2 As Table).
0
Comment
Question by:Cluskitt
2 Comments
 
LVL 5

Accepted Solution

by:
Rainverse earned 500 total points
ID: 36524276
I think for this type of object, you'll have to clone that manually.  

Dim myClonedTable As HtmlTable
Dim myClonedTableRow As HtmlTableRow
Dim newCell As HtmlTableCell

For Each row As HtmlTableRow In myExistingTable
    myClonedTableRow = New HtmlTableRow()

    For Each cell As HtmlTableCell In row
        newCell = New HtmlTableCell
        newCell.ID = cell.ID
        newCell.Width = cell.Width
        ' yadda yadda yadda...
        myClonedTableRow.Cells.Add(newCell)
    Next
    myClonedTableRow.Cells.Add(newCell)
    myClonedTable.Rows.Insert(1, myClonedTableRow)
Next

Open in new window


-MJC
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 36524533
Hello,

When you're upgrading a VB6 app to VB.NET manually, a difference in the way the languages pass argument can cause a nasty bug. Here's what to watch for.

There are two ways to pass arguments to a procedure in Visual Basic: ByVal and ByRef. This refers to whether a copy of the argument is passed to the procedure (ByVal - the value is passed) or whether the actual argument itself is passed (ByRef - a reference to the argument is passed). The default for VB6 is ByRef.

There are also two types of procedures in Visual Basic:

Function
Sub

The difference is that a Function returns the a value assigned to the function name and a Sub doesn't. So if you want to return a value to the calling routine, there are two ways to do it. In VB6, the code that returns a value could look like this for a Sub:
Code to return a value from a Sub procedure in VB6

Public Sub CallingSub()
   CallingArg = "ABCDEF"
   CalledSub CallingArg
   MsgBox (CallingArg)
End Sub

Public Sub CalledSub(CalledArg)
   CalledArg = "987654"
End Sub

Code to return a value from a Function procedure in VB6:


Public Sub CallingSub()
   CallingArg = "ABCDEF"
   CalledFunc CallingArg
   MsgBox (CallingArg)
End Sub

Public Function CalledFunc(CalledArg)
   CalledArg = "987654"
   CalledFunc = "Any Return Value"
End Function
The result of both the Sub and the Function code above is a window that displays the value "987654" returned as a result of changing the value of the argument.

Note that in this coce, the main difference is that the function has just one more statement to assign a return value to the function name. In VB6, the difference was sometimes considered meaningless and programmers often used subroutines rather than functions out of habit or just to save a statement.

A few other "shortcuts" in VB6 should be pointed out.

No variables are declared.
All variables in VB6 are of type "Variant" - similar to "Object" in VB.NET.
No scoping or typing is done in the argument list.
Everything uses the default settings.
The procedure call doesn't use any parentheses or a Call keyword.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

772 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