Stephen Pefanis
Stephen Pefanis

Reputation: 365

VB.NET Dynamically creating variables at runtime

I would like to create variables at run time when my application launches, or when called from within. The situation for this requirement is that I am reading data from various devices across the site and currently this is hard coded within the application. What I would like to do is create an application that loads a list from a database of the required variables and creates and assign them as required.

I am using the INGEAR driver package to read the data, I will require to declare multiple PLC's and for each of those PLC's I will need to declare Multiple ScanGroups and each ScanGroup will have multiple Tags for it. The read data will then be written to a database on each read.

Declaring each of this at design time (from a sample application INGEAR provides) is

    ' *****************************
' * Initialize Controller class
Public MyPLC As New Controller

' ******************************
' * Initialize TagGroup class
Public scanGroup As New TagGroup

' ******************************
' * un-initialized Tag class
Dim WithEvents newTag As Tag

Using the "WithEvents" they are able to create the tag's dynamically, but not 100% sure on how that works and how to implement this further.

When they add the tag when the application is running, they do something along:

newTag = New Tag(MyPLC, _tagName.Text)

and also

AddHandler newTag.Changed, AddressOf newTag_Changed

Which I assume is used to capture the change (and invoke from the thread)

Is there anyone who can give me some guidance on:

I assume that the example is going down the right track, but was not 100% sure what each item is trying to achieve. If anyone would like some more details on the application/sample please let me know.

ADDITIONAL

So working through this code, I'll assume that in my database I have 3 tables.

PLC Table - PLC_ID, PLC Name, PLC_IPaddress
SCAN Group - GROUP_ID, Group_Name, Group_Time
TAGS - TAG_ID, PLC_ID, GROUP_ID, Tag_Value

Variables:

Dim ALLPLCs As New Dictionary(Of String, Controller)
Dim ScanGroups As New Dictionary(Of String, TagGroup)
Dim AllTags As New Dictionary(Of String, Tag)

Creating the "Controllers" is simple enough using:

    For Each row In PLCTable.Rows
        ALLPLCs.Add(row("PLC_ID"), New Controller(row("PLC_IP"), 1000))
    Next

Creating my Groups is below, but starting to get worried about the tracking of items. This will create it with a name like '1_1', '2_4' with the first number being the PLC_ID and the second the GROUP_ID

    For Each row In ScanGroupTable.Rows
        For Each thePLC In ALLPLCs
            Dim tg As New TagGroup
            tg.Controller = thePLC.Value
            tg.ScanningMode = TagGroup.SCANMODE.ReadOnly
            tg.Interval = Convert.ToInt16(row("GROUP_TIME"))
            ScanGroups.Add(thePLC.Key & row("GROUP_ID"), tg) 'Add the 1 second scangroup
        Next
    Next

Now the next query is how to add my Tags to the dictionary for each PLC_ID and GROUP_ID. Any Direction on this? When creating a static tag, i would do something along the lines of below, but how to do this correctly by selecting the correct PLC and Scangroup?

 Dim tg As New Tag
    tg.Controller = "CONTROLLER"
    tg.Name = "TAG_NAME"
    ScanGroups.Add(tg)

after that, the system scangroups will automatically do the threads to update on it's interval. In the sample project, when adding a tag dynamically, the use the following:

AddHandler newTag.Changed, AddressOf newTag_Changed

This is where i'm a little stuck. How to add the handler for each of the tags at runtime correctle and capture their changes

Upvotes: 0

Views: 1496

Answers (1)

Martin Noreke
Martin Noreke

Reputation: 4136

You could use a dictionary or similar structure to keep track of all of the items you create.

public Dictionary<string, Controller> AllPLCs { get; set; }
public Dictionary<string, TagGroup> ScanGroups { get; set; }
public Dictionary<string, WithEvents> AllNewTags { get; set; }

From here, you can load from the database and add to the dictionaries in a loop, using the name as the string key.

foreach(var foundItem in LoadedData)
{
    AllPLCs.Add(foundItem.Name, new Controller());
    ScanGroups.Add(foundItem.Name, new TagGroup());
    AllNewTags.Add(foundItem.Name, new WithEvents());
}

Later you can access each one as follows:

foreach(var foundItem in LoadedData)
{
    Controller currentController = AllPLCs[foundItem.Name] as Controller());
    TagGroup currentGroup = ScanGroups[foundItem.Name] as TagGroup;
    WithEvents currentEvent = AllNewTags[foundItem.Name] as WithEvents;
}

My code examples are in C#, but they should get you on the right track.

Upvotes: 2

Related Questions