Community
Showing results for 
Search instead for 
Do you mean 
Reply

Create a variable using a Field value

Copper Super Contributor
Posts: 88
Country: USA

Create a variable using a Field value

Is it possible to create a new Variable using a value from a Field?

 

For instance, if a Field value is "Address1" I like to create a new Variable called Address1

 

"Dim [Variable Name] as string = [Field value]"

 

In Visual FoxPro, I could use a macro expression to create a new variable:

 

 "&[Variable Name] = [Field value]"

 

 

Does this work in vb.NET? If so, would you show a code snippet to do this?

 

Thanks!

Employee
Posts: 1,163
Country: USA

Re: Create a variable using a Field value

You could, but I'm not clear on whether we're wanting to create a variable with the value from a field (ie. the data in the field) or if we want to use the name of the field descriptor ("Address1").

 

If you did want to assign the value of a field to a variable you could do so like this:

 

Contact currContact = ActApp.ApplicationState.CurrentContact;

DBFieldDescriptor myField = ActApp.ActFramework.Contacts.GetContactFieldDescriptor("TBL_CONTACT.USER1");

object fu = myField.GetValue(myField, currContact);

 

There may be some syntax issue, I just did this from memory. 

Matthew Wood
Act! SDK Support
Community Moderator
Copper Super Contributor
Posts: 88
Country: USA

Re: Create a variable using a Field value

Thanks for the reply.

 

This is more of a vb.NET question than an ACT! SDK question, but it directly involves ACT! data in the Contact table. I have already read the field names and field values into a database. Now, I want to create a series of new Variables based on the records in the FieldName column (below).

 

Sample Database fields - 2 columns in the database:

 

FieldName                        FieldValue

"Address1"                        "1234 Main St"

"Address2"                        "Suite 200

"City"                                   "Anytown"

 

I'd like to end up with 3 variables, named "Address1", "Address2" and "City", with the FieldValue column values as the variable values.

 

An even better solution would be if I could have the created Variables named something like "co_Address1", co_Address2", "co_City"... to make sure that they are unique as Variables.

 

Any suggestions?

Bronze Super Contributor
Posts: 1,231
Country: USA

Re: Create a variable using a Field value

No, the VB / c# Common Language Runtime (CLR) does not support macro substitution like the VFP.  Some VFP developers have used reflection but I think that’s the wrong path for what you are trying to accomplish. I started with FP 2.6 for MAC before it was a Microsoft product. The first few months with .NET I was always looking for the macro substitution also.

 

Why not use a dictionary or an array list to store your variables. Here is an example. It’s not tested but it should give you the idea of how to use a dictionary.

 

Private dicVariables As New Dictionary(Of String, String)
….
  	' Add the variableValues to the dictionary
        Me.addVariableValue("co_Address1", "1234 Main St")
        Me.addVariableValue("co_Address2", "Suite 200")
        Me.addVariableValue("co_City", "Anytown")

        ' Get the value from the dictionary
        Dim MyVariableValue As String = Me.getVariableValue("co_Address1")
        ' NOTE - MyVariableValue should now equal "1234 Main St"

     Private Sub removeVariableValue(ByVal key As String, ByVal value As String)
        Try
            If Me.dicVariables.ContainsKey(key) Then
                ' Update he current value
                dicVariables.Remove(key)
            End If

        Catch ex As Exception
            ' DO somthing 
        End Try

    End Sub

    Private Sub addVariableValue(ByVal key As String, ByVal value As String)
        Try
            If Me.dicVariables.ContainsKey(key) Then
                ' Update he current value
                dicVariables.Item(key) = value
            Else
                dicVariables.Add(key, value)
            End If

        Catch ex As Exception
            ' DO somthing 
        End Try

    End Sub

    Private Function getVariableValue(ByVal key As String) As String
        Try
            If Me.dicVariables.ContainsKey(key) Then
                Return dicVariables.Item(key)
            Else
                Return String.Empty
            End If

        Catch ex As Exception
            ' DO somthing 
        End Try

        Return String.Empty
    End Function

 

 

Hoep this helps

-- Jim Durkin

Nickel Contributor
Posts: 175
Country: USA

Re: Create a variable using a Field value

This is just to provide another path that I think could help, in addition to Jim's.

 

You could use a DataTable to store the values. This would be more useful if you are storing values for multiple contacts and not just one.

 

'Creation
dim dt as New Data.DataTable
dt.Columns.Add("Column1",GetType(String))
dt.Columns.Add("Column2",GetType(Integer))

'Filling
dt.Rows.Add(New Object() {"Value1",1})
dt.Rows.Add(New Object() {"Value2",2})

'Selecting
dt.Rows(0)("Column1").Value  'Gets "Column1" value of first row

'Searching, per se, returns an array of DataRow objects
dim dr() as Datarow = dt.Select("Column1='" & SearchVariable & "'")

 

Again, depending on the need, this could be helpful too, as the DataTable object is the "base" object used to represent an actual database table. In my above examples, I would think that you'd want to use the values from the FieldDescriptor objects to "build" the table. HTH