473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

pass datatable in event or grab from property or pass in function?

Which would be the proper way or the reason for using any of the following
or combinations of the following? These are the 3 ways I've figured I can
do what I want to do, I just don't know which would be best:

#1
' Everything is create upon instantiation. Once completed, datatable can be
obtained from the property.
Public Class A

Private dt as DataTable

Public Sub New()
Load()
End Sub

Private Sub Load()
' Do stuff to create a datatable
dt = MyCreatedDataTable
End Sub

Public ReadOnly Property YourDataTable() As DataTable
Get
Return dt
End Get
End Property

End Class
'************************************************* *********************
#2
' Everything created upon instantiation. DataTable is passed in the
Complete event.
Public Class A

Private dt as DataTable
Public Event Complete(ByVal dt As DataTable)

Public Sub New()
Load()
End Sub

Private Sub Load()
' Do stuff to create a datatable
dt = MyCreatedDataTable
RaiseEvent Complete(dt)
End Sub

End Class
'************************************************* *********************
#3
' Nothing created upon instantiation. Expose Load as a function to return a
datatable.
Public Class A

Private dt as DataTable

Public Sub New()
' Do nothing
End Sub

Public Function Load() As DataTable
' Do stuff to create a datatable
Return MyCreatedDataTable
End Sub

End Class
Thanks for you input,

Raymond Lewallen
Nov 20 '05 #1
7 1633
Sorry, in #3 disregard that Private dt As DataTable. That should not be
there.
Nov 20 '05 #2
Raymond,
What specifically is it that you want to do?

As each method is useful in its own regard, with out really knowing
specifically what you are attempting to do, its hard to say which of the
three are better, or even if #4 or #5 would be better...

Hope this helps
Jay

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:u$**************@TK2MSFTNGP10.phx.gbl...
Which would be the proper way or the reason for using any of the following
or combinations of the following? These are the 3 ways I've figured I can
do what I want to do, I just don't know which would be best:

#1
' Everything is create upon instantiation. Once completed, datatable can be obtained from the property.
Public Class A

Private dt as DataTable

Public Sub New()
Load()
End Sub

Private Sub Load()
' Do stuff to create a datatable
dt = MyCreatedDataTable
End Sub

Public ReadOnly Property YourDataTable() As DataTable
Get
Return dt
End Get
End Property

End Class
'************************************************* *********************
#2
' Everything created upon instantiation. DataTable is passed in the
Complete event.
Public Class A

Private dt as DataTable
Public Event Complete(ByVal dt As DataTable)

Public Sub New()
Load()
End Sub

Private Sub Load()
' Do stuff to create a datatable
dt = MyCreatedDataTable
RaiseEvent Complete(dt)
End Sub

End Class
'************************************************* *********************
#3
' Nothing created upon instantiation. Expose Load as a function to return a datatable.
Public Class A

Private dt as DataTable

Public Sub New()
' Do nothing
End Sub

Public Function Load() As DataTable
' Do stuff to create a datatable
Return MyCreatedDataTable
End Sub

End Class
Thanks for you input,

Raymond Lewallen

Nov 20 '05 #3
I think that all really depends on what your requirements are.

For example, is 'A' valid without having a valid datatable internally? If
it is not valid, then #3 is out, because it relies on the developer to call
Load manually - and only then making that instance of A valid. The object
should be valid and ready to be used after the constructor fires. Though you
are not storing a reference to the datatable in the object, so perhaps it is
not an issue.

Another example, if the developer might need to grab the datatable at
various times (not just once after the construction of the object), then #2
is out, since the Complete event fires just then. A bigger problem is, I
dont' see how you can attach an event handler, to an object that doesn't
exist. You can only do it after the constructor finishes - but at this point
the Complete has fired already, and it is too late.

I would say #1 is pretty typical when the DataTable is part of the object
and needs to be instantiated as part of the construction of A. That way you
ensure that it gets created, and give the developer a way to access it if
need be.

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:u$**************@TK2MSFTNGP10.phx.gbl...
Which would be the proper way or the reason for using any of the following
or combinations of the following? These are the 3 ways I've figured I can
do what I want to do, I just don't know which would be best:

#1
' Everything is create upon instantiation. Once completed, datatable can be obtained from the property.
Public Class A

Private dt as DataTable

Public Sub New()
Load()
End Sub

Private Sub Load()
' Do stuff to create a datatable
dt = MyCreatedDataTable
End Sub

Public ReadOnly Property YourDataTable() As DataTable
Get
Return dt
End Get
End Property

End Class
'************************************************* *********************
#2
' Everything created upon instantiation. DataTable is passed in the
Complete event.
Public Class A

Private dt as DataTable
Public Event Complete(ByVal dt As DataTable)

Public Sub New()
Load()
End Sub

Private Sub Load()
' Do stuff to create a datatable
dt = MyCreatedDataTable
RaiseEvent Complete(dt)
End Sub

End Class
'************************************************* *********************
#3
' Nothing created upon instantiation. Expose Load as a function to return a datatable.
Public Class A

Private dt as DataTable

Public Sub New()
' Do nothing
End Sub

Public Function Load() As DataTable
' Do stuff to create a datatable
Return MyCreatedDataTable
End Sub

End Class
Thanks for you input,

Raymond Lewallen

Nov 20 '05 #4
> What specifically is it that you want to do?

Do to the nature of the program and my employer, I cannot attach source
code. However, I can tell you the datatable is being used to populate a PDF
form.

Let me ask you this: take the following 2 examples #10.1 and #10.2, given
the code already posted #1 where the property is used to get the datatable.
Would you go ahead and cache the datatable locally or continue to use the
property? Consider the datatable is ALWAYS a single row, with anywhere from
25 to 200 columns, depending on the report we are populating. In previous
post example #3, Load() must be called manually to get a datatable, and this
will always need to be called, validating the need of instantiating A, so we
will always get a datatable returned and cached locally. Would this be
preferable to using the property, if you are going to choose #10.1 below as
the more appropriate approach? FYI, currently the implementation is #1 from
the previous code and #10.1 below, but compiles larger with more IL code.
#3 with #10.1 compiles with the least amount of IL code. To me, readability
is the same regardless of which combinations.

#10.1
Class B

Public Sub MakeForm()
Dim myA As New A
Dim dt As DataTable = A.YourDataTable
' use dt to do various things, repeatedly using dt. One example is
this loop.
For Each column As DataColumn in dt.Columns
' Do something
Next
End Sub

End Class
'*********************************************

OR

#10.2
Class B

Public Sub MakeForm()
Dim myA As New A
' use A.YourDataTable to do various things, repeatedly using
A.YourDataTable. One example is this loop.
For Each column As DataColumn in A.YourDataTable.Columns
' Do something
Next
End Sub

End Class
'*********************************************

Nov 20 '05 #5
> I think that all really depends on what your requirements are.

Please read my response to Jay B. Harlow. Your input and following of this
post is greatly appreciated. This may help clarify your first 2 paragraphs
of your initial response.

For example, is 'A' valid without having a valid datatable internally?
No, it is not.
Though you are not storing a reference to the datatable in the object, so perhaps it is not an issue.
This is what I was thinking, but wasn't sure.
Another example, if the developer might need to grab the datatable at
various times (not just once after the construction of the object), then #2 is out, since the Complete event fires just then. A bigger problem is, I
dont' see how you can attach an event handler, to an object that doesn't
exist. You can only do it after the constructor finishes - but at this point the Complete has fired already, and it is too late.

Actually, the event does fire and all works great when the calling class
does:

Dim myA As A
AddHandler myA.Complete, AddressOf UseMyDataTable
myA = New A

Sub UseMyDataTable(ByVal dt As DataTable)
' This does get executed.
End Sub

UseMyDataTable is being executed with the complete event firing.
I would say #1 is pretty typical when the DataTable is part of the object
and needs to be instantiated as part of the construction of A. That way you ensure that it gets created, and give the developer a way to access it if
need be.


#1 is the current implementation. The developer will always have a need to
access the datatable.

Again, please read my post to Jay, as it is directed to the both of you.

Thanks,

Raymond Lewallen
Federal Aviation Administration
Nov 20 '05 #6
After further examination of the IL, it doesn't appear to matter whether you
cache the datatable locally or use the property, in this instance, because
either way it is storing the datatable to a System.Collections.IEnumerator
on the stack and using that, never returning back to the code for the
initial datatable. Perhaps just calling the property would be better,
eliminating the extra local on the stack created to store the datatable
locally.

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:%2*****************@TK2MSFTNGP10.phx.gbl...
What specifically is it that you want to do?
Do to the nature of the program and my employer, I cannot attach source
code. However, I can tell you the datatable is being used to populate a

PDF form.

Let me ask you this: take the following 2 examples #10.1 and #10.2, given
the code already posted #1 where the property is used to get the datatable. Would you go ahead and cache the datatable locally or continue to use the
property? Consider the datatable is ALWAYS a single row, with anywhere from 25 to 200 columns, depending on the report we are populating. In previous
post example #3, Load() must be called manually to get a datatable, and this will always need to be called, validating the need of instantiating A, so we will always get a datatable returned and cached locally. Would this be
preferable to using the property, if you are going to choose #10.1 below as the more appropriate approach? FYI, currently the implementation is #1 from the previous code and #10.1 below, but compiles larger with more IL code.
#3 with #10.1 compiles with the least amount of IL code. To me, readability is the same regardless of which combinations.

#10.1
Class B

Public Sub MakeForm()
Dim myA As New A
Dim dt As DataTable = A.YourDataTable
' use dt to do various things, repeatedly using dt. One example is this loop.
For Each column As DataColumn in dt.Columns
' Do something
Next
End Sub

End Class
'*********************************************

OR

#10.2
Class B

Public Sub MakeForm()
Dim myA As New A
' use A.YourDataTable to do various things, repeatedly using
A.YourDataTable. One example is this loop.
For Each column As DataColumn in A.YourDataTable.Columns
' Do something
Next
End Sub

End Class
'*********************************************

Nov 20 '05 #7
Raymond,
I normally write "correct" code first, by "correct" I mean follow the Object
Thinking (Object Oriented) paradigm. Only after a routine has proven to have
a performance problem via Profiling will I worry about optimizing that
routine.

Remember the 80/20 rule: 80% of the time of your code falls within 20% of
your code.

It doesn't really make sense to optimize the 80% of your code where only 20%
of the time is spent, does it?

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware...timization.pdf

For a list of Martin's articles see:

http://martinfowler.com/articles.html

Info on the CLR Profiler:
http://msdn.microsoft.com/library/de...nethowto13.asp

http://msdn.microsoft.com/library/de...anagedapps.asp

Hope this helps
Jay
"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:%2*****************@TK2MSFTNGP10.phx.gbl...
What specifically is it that you want to do?
Do to the nature of the program and my employer, I cannot attach source
code. However, I can tell you the datatable is being used to populate a

PDF form.

Let me ask you this: take the following 2 examples #10.1 and #10.2, given
the code already posted #1 where the property is used to get the datatable. Would you go ahead and cache the datatable locally or continue to use the
property? Consider the datatable is ALWAYS a single row, with anywhere from 25 to 200 columns, depending on the report we are populating. In previous
post example #3, Load() must be called manually to get a datatable, and this will always need to be called, validating the need of instantiating A, so we will always get a datatable returned and cached locally. Would this be
preferable to using the property, if you are going to choose #10.1 below as the more appropriate approach? FYI, currently the implementation is #1 from the previous code and #10.1 below, but compiles larger with more IL code.
#3 with #10.1 compiles with the least amount of IL code. To me, readability is the same regardless of which combinations.

#10.1
Class B

Public Sub MakeForm()
Dim myA As New A
Dim dt As DataTable = A.YourDataTable
' use dt to do various things, repeatedly using dt. One example is this loop.
For Each column As DataColumn in dt.Columns
' Do something
Next
End Sub

End Class
'*********************************************

OR

#10.2
Class B

Public Sub MakeForm()
Dim myA As New A
' use A.YourDataTable to do various things, repeatedly using
A.YourDataTable. One example is this loop.
For Each column As DataColumn in A.YourDataTable.Columns
' Do something
Next
End Sub

End Class
'*********************************************

Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer)...
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
0
by: Chris Ericoli | last post by:
Hi, I am working with an 'in session' ado dataset with an asp.net application. My dataset is comprised of two tables, one of which maintains a few calculated datacolumns. For some reason these...
8
by: darrel | last post by:
I'm still trying to fully understand how best to pass variables between pages/usercontrols/each other. On a current site I've done, I've had one userControl do the logic and set the variable,...
2
by: Daylor | last post by:
hi. i have 2 combobox on form. after i load the form. when i select the first combobox , i want to filter the datatable that attached to the second combobox. i wrote :
2
by: Mark | last post by:
Hello - From an Access VBA module, I'm trying to pass a Table to a VB.Net class. I'm getting the following error running the VBA: "Unable to cast object of type System._ComObject to type...
3
by: rodchar | last post by:
hey all, i have a gridView with editing available. when a user clicks the update link for a row, how do i pass the entire data table as a parameter to my business layer? thanks, rodchar
2
by: DC | last post by:
Hi, I am using a GridView to present data in a DataTable, which I store only in ViewState and when the user hits the "OK" button the rows in the DataTable will be used to execute transactions. ...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.