|
-
Aug 31st, 2008, 06:43 PM
#1
Thread Starter
New Member
How do you insert strings into code to act as code?
I want to use a string (which can be changed depending on the input to the sub) as part of some code and have no idea how to do it! It was possible in ruby i think so was wondering how its done in vb.net
my specific problem is creating a method which fills a two dimensional array with data from a table but the table name is the variable.
here is the code so far:
Code:
Sub fillarray(ByRef Array() As String, ByVal TableName As String)
Dim rowview As Data.DataRowView
Dim str As String
str = "BCP2DataSet." & TableName & "Row"
Dim row As str
End Sub
as you can see i want to create a row object unique to a table and i then later on in the code want to be able to do something like:
Code:
str = TableName & "BindingSource"
row = str.Current
i need to use the variable into the method to construct the code and i dont know how (i am a beginner)...could someone help?
-
Aug 31st, 2008, 07:43 PM
#2
Re: How do you insert strings into code to act as code?
You can't do that. There is no way to use a variable as a type specifier in a strongly typed language, because the type specifier tells the compiler what type of variable to expect, and you are telling it to expect a type that isn't known until runtime.
Depending on what you want to do with row, you might be able to do something like that using generics, but it is far more likely that there is a more direct way to accomplish the same thing by a different design. I'm not clear on what your ultimate objective is, since your ultimate objective wasn't part of your question, but your approach won't work, so a different route will be necessary.
One thing I would note is that a row IS unique to a table if we are talking about datarows and datatables, which does look like what you are talking about. The row will be created with the schema of the datatable, so your first example isn't really necessary in the case of datatables.
My usual boring signature: Nothing
 
-
Aug 31st, 2008, 11:20 PM
#3
Frenzied Member
Re: How do you insert strings into code to act as code?
I tried to use a string as code in a couple of programs i made. Though after posting about it i found out i must use a compiler, here is a link to some code for using the VB compiler:
VB.NET Compiler
-
Aug 31st, 2008, 11:51 PM
#4
Re: How do you insert strings into code to act as code?
 Originally Posted by noahssite
I tried to use a string as code in a couple of programs i made. Though after posting about it i found out i must use a compiler, here is a link to some code for using the VB compiler:
VB.NET Compiler
That's certainly not the answer to this issue. Compiling code at run time is something that should rarely be done. This is certainly not an occasion where it's warranted.
-
Sep 1st, 2008, 06:37 AM
#5
Thread Starter
New Member
Re: How do you insert strings into code to act as code?
hmm ok i thought it might not be possible.
basically i am writing a word document with data gathered from a database which has about 12 tables i need to get data from, so i thought to populate strings with the data and then i can easily run the code to write the word document.
the reason i wanted to be able to use strings in the code is to make a method which populates string arrays with the data from the tables as the basic method is the same, just a few words need replacing to make it work for different tables. here is an example of getting the data from one table called usr_fin_hard_copy:
Code:
Dim HardCopyRow As BCP2DataSet.usr_fin_hard_copyRow
Dim Entries As Integer
Me.Usr_fin_hard_copyTableAdapter.Fill(Me.BCP2DataSet.usr_fin_hard_copy)
Me.Usr_fin_hard_copyBindingSource.Filter = "Team = '" & SelectedTeam.ToString & "'"
Entries = Usr_fin_hard_copyBindingSource.Count
Dim HardCopy(Entries, 3) As String
For n = 0 To Entries - 1
rowview = CType(Usr_fin_hard_copyBindingSource.Current, System.Data.DataRowView)
HardCopyRow = CType(rowview.Row, BCP2DataSet.usr_fin_hard_copyRow)
Try
HardCopy(n, 0) = HardCopyRow.Critical_Process
Catch ex As Exception
HardCopy(n, 0) = ""
End Try
Try
HardCopy(n, 1) = HardCopyRow.Material
Catch ex As Exception
HardCopy(n, 1) = ""
End Try
Try
HardCopy(n, 2) = HardCopyRow._Back_up
Catch ex As Exception
HardCopy(n, 2) = ""
End Try
Usr_fin_hard_copyBindingSource.MoveNext()
Next
Now im going to have to write out identical code for the next x amount of tables but just changing table names and column names which could easily be passed into a generic method.
surely there is a way of writing a method to do this rather than have to have an extra 100 or more lines of code?
P.S. This is my first "commercial" piece of software and I've been self taught so I dont know what is acceptable or not in coding
Last edited by jaderberg; Sep 1st, 2008 at 07:43 AM.
-
Sep 1st, 2008, 08:56 AM
#6
Re: How do you insert strings into code to act as code?
No, you don't have to write out identical code. You write a method that contains the common code and then just call that method and pass in the individual values.
-
Sep 1st, 2008, 09:03 AM
#7
Thread Starter
New Member
Re: How do you insert strings into code to act as code?
 Originally Posted by jmcilhinney
No, you don't have to write out identical code. You write a method that contains the common code and then just call that method and pass in the individual values.
yes i know thats what i want to do but how do i write a method which can except different objects, i.e. usr_fin_hard_copyRow and usr_fin_materialsRow which are two different objects? whenever i write something using a passed in variable it will underline it in blue and wont build
-
Sep 1st, 2008, 09:07 AM
#8
Re: How do you insert strings into code to act as code?
You don't have to use the strongly-typed rows. Write a method that accepts a DataRow and a column name. All strongly-typed DataTables inherit the DataTable class, so they can be treated as regular DataTables. All stringly-typed DataRows inherit the DataRow class, so they can be treated as regular DataRows. The same is not true of TableAdapters, so they must be handled individually.
-
Sep 1st, 2008, 10:34 AM
#9
Thread Starter
New Member
Re: How do you insert strings into code to act as code?
 Originally Posted by jmcilhinney
You don't have to use the strongly-typed rows. Write a method that accepts a DataRow and a column name. All strongly-typed DataTables inherit the DataTable class, so they can be treated as regular DataTables. All stringly-typed DataRows inherit the DataRow class, so they can be treated as regular DataRows. The same is not true of TableAdapters, so they must be handled individually.
THanks alot... solved my problem!
if ur interested i came up with this:
Code:
Function fillarray(ByVal row As DataRow, ByVal Bsource As BindingSource, ByVal BsourceFilter As String, ByVal Columns() As Integer) As String(,)
Dim rowview As Data.DataRowView
Dim Entries, ColNum As Integer
Bsource.Filter = BsourceFilter
Entries = Bsource.Count
ColNum = Columns.Length
Dim Array(Entries, ColNum) As String
For n = 0 To Entries - 1
rowview = CType(Bsource.Current, System.Data.DataRowView)
row = CType(rowview.Row, DataRow)
For i = 0 To ColNum - 1
Try
Array(n, i) = row.ItemArray(Columns(i)).ToString
Catch ex As Exception
Array(n, i) = ""
End Try
Next
Bsource.MoveNext()
Next
Return (Array)
End Function
and you use it like this:
Code:
Me.Usr_fin_hard_copyTableAdapter.Fill(Me.BCP2DataSet.usr_fin_hard_copy)
Dim HardCopyRow As BCP2DataSet.usr_fin_hard_copyRow
Dim HardCopy(,) As String
Dim Filter As String
Filter = "Team = '" & SelectedTeam.ToString & "'"
Dim HardCopyColumns() As Integer = {2, 3, 4}
HardCopy = fillarray(HardCopyRow, Usr_fin_hard_copyBindingSource, Filter, HardCopyColumns)
im sure its not the most elegant way to do it but it works!
-
Sep 1st, 2008, 10:46 AM
#10
Re: How do you insert strings into code to act as code?
Technically, if you want to build a code string and run it, then your best option is shell execution of ASM.
-
Sep 1st, 2008, 01:45 PM
#11
Frenzied Member
Re: How do you insert strings into code to act as code?
 Originally Posted by jmcilhinney
That's certainly not the answer to this issue. Compiling code at run time is something that should rarely be done. This is certainly not an occasion where it's warranted.
Yes i thought there would be another way, i just posted the link since that was what he asked..kinda. Though i wouldn't compile code at run-time since it goes into a whole other area and is not required.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|