Dynamically Create a Structure

P

pmclinn

I was wondering if it is possible to dynamically create a structure.

Something like this:

public sub main
sql = "Select Col1, Col2 from Table a"
dim al as new arraylist
al = LoadOracleData(sql)
'____Do amazing things

end sub

Private Function LoadOracleData(SQL as string) as arraylist
dim al as new arraylist

'...Parse out SQL Column names
'Use these names as the objects in the structure

'Structure Sample
'Dim Col1 as string
'Dim Col2 as string

'Load data into Structure sample and add to arraylist
'OLE/ODBC Connection
for...


al.add(sample structure)
Loop

return arraylist 'of structure
end function

I understand that datatypes should also be implimented...but for the
purposes of learning how to do this I have left it out....

-Peter
 
J

Jay B. Harlow [MVP - Outlook]

pmclinn,
If you want to "dynamically" create a "structure" that contains SQL data I
would strongly suggest a DataSet & DataTable as they are "dynamic
structures" that contain data...
public sub main
sql = "Select Col1, Col2 from Table a"
dim table As DataTable
table = LoadOracleData(sql)
'____Do amazing things

end sub

Private Function LoadOracleData(SQL as string) as DataTable Dim table As New DataTable
'Structure Sample
'Dim Col1 as string
'Dim Col2 as string
table.Columns.Add("Col1", GetType(String))
table.Columns.Add("Col2", GetType(String))

For a complete explanation of Datasets, DataTables, & DataAdapters along
with how to use them. I would strongly recommend you read David Sceppa's
book "Microsoft ADO.NET - Core Reference" from MS Press. As it is a good
tutorial on ADO.NET as well as a good desk reference once you know ADO.NET.

Hope this helps
Jay
 
P

pmclinn

Specifically I want to try to dynamically create a structure, or
class(.dll) in memory that I can refrence. I understand how the
datatables/rows... work but that is not what I'm looking for.... Thank
you though for your response.
 
J

Jay B. Harlow [MVP - Outlook]

pmclinn,
I suspected that you "think you want" to dynamically create a Class or
Structure. I was just testing the waters to see how persistent you are :)

I have to ask, once you dynamically create the assembly, how are you going
to actually do "Do amazing things" in the code?

In order for your code to use the assembly you need to reference the
assembly where the type is, however if you dynamically create the assembly &
type you do not have the assembly to reference! The Seperated Interface
Pattern may be useful here.

http://martinfowler.com/eaaCatalog/separatedInterface.html

I'm asking these questions not so much to scare you off, just to get you to
think about what you really getting into... ;-)

You can use either System.CodeDom or System.Reflection.Emit to dynamically
create an Assembly & Type. I would use CodeDom more for tools, where I
create the assembly once, then use it many times (many program executions).
I would use Reflection.Emit where I create the assembly & use it during the
execution of the program only... Of course I may also choose one over the
other if one provided easier then the other for the task at hand...

I have not looked deeply yet, but these article may be a good starting point
for Reflection.Emit & your task at hand:

http://www.codeproject.com/useritems/autocaster.asp

http://www.codeproject.com/useritems/threadsafeforms.asp

Post if you need CodeDom samples.

However I am still recommending a DataTable over a dynamic assembly, as I
don't see anything in your "requirements" that really warrents a dynamic
assembly... Hence the "think you want" comment above...

Hope this helps
Jay
 
P

pmclinn

It just seems like I should be able to write .net code and save it to a
string variable and then compile the class (Found in string). The
compile would only have to be done once before the data pull. It would
then save the new class to a local variable.
 
J

Jay B. Harlow [MVP - Outlook]

pmclinn,
It just seems like I should be able to write .net code and save it to a
string variable and then compile the class (Found in string). The
I believe I stated both CodeDom & Reflection.Emit support creating &
compiling code dynamically.

My point is it a Chicken & Egg problem. How are you actually going to use
the dynamically created class?
It would
then save the new class to a local variable.
You will actually wind up with a variable for the assembly itself, then a
variable for the Type (class or structure) itself, then a variable for an
instance of the type.

My point is to define the variable for an instance of the type may need to
be of type Object. IMHO variables of type Object "should" be avoided as
Strongly Typed classes & Early binding identify programs at compile time,
where as "As Object" tends to leave harder to find runtime problems. With
dynamically created code, the runtime problems may be even harder to find...

Using Early Binding (Option Strict On) you need a reference to the assembly
to get the Type, if the Type is dynamically created how are you going to
reference it. If you are using Late Binding, the Dataset is late bound so
"what's the point" of the dynamic class. It appears you want to create a
dynamically create a "data row", well .NET already does this. Its called
System.Data.DataRow.

Currently your "Requirements" appear that you, yourself, want to implement
the Record Set pattern using Dynamically generated code.

http://www.martinfowler.com/eaaCatalog/recordSet.html

I'm suggesting rather then dynamically create types that implement the
Record Set pattern, simply use System.Data.DataSet as it already does the
implementation for you!


Don't get me wrong there are times when dynamically created types are very
useful, you may have requirements that are not reflected within this
thread... Hence my pointing you to the two places that are needed... I'm
considering Reflection.Emit to dynamically create assemblies from XML based
"control programs", in my case the dynamically created types would implement
one or more Seperated Interface Patterns to help ensure early binding...

Hope this helps
Jay
 
P

pmclinn

Jay,

The problem may be just the example I'm using here to understand a
programming concept. I'm just trying to see how to this coding works
in a simple to understand format. I chose an programming example that
I am familuar with just so I could get a grasp of a concept.

Pretend like all I want to know is how to create a virtual (Class)dll
and then refrence it from within a windows form application. Just make
the class and example very limitted so that I can catch on....

Something as simple as: 'Add a number to a total
Class Simple
private var as int
Get..

Set...
end class

;) Then, I'll ask for help on when to use it in the future...Trust me.
 
J

Jay B. Harlow [MVP - Outlook]

pmclinn,
Did you look at the two links I gave earlier?

Both included examples of what you are asking?

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top