Tags

, , ,

Finally I’ve cracked the CSV file problem, sort of, by creating a file that’s shared across multiple Visual C# projects. It took some debugging and trial-and-error, but it works, and it could be applied to other situations where multiple projects need to access a shared C# class. The following method is also way easier than the CSV thing.

As the starting point, we’ll assume a solution with several projects has already been created. Right-click on the root entry in the Solution Explorer, and add a new Class Library project to the solution.

create-empty-class

An empty C# class is automatically generated by default. The first task is to create a namespace that other projects will reference the variables from. Change the autogenerated class to a ‘public static class’, and add a few variables inside it. Personally I’m declaring everything as a ‘public static string’.

In my case, one of the variables uses the Playback.EncryptText() function, which is called from Microsoft.VisualStudio.TestTools, so that library must be imported. When done, the code should look something like:

newclass

Additionally the imports also require ‘assembly references’. Right-click on the References entry for the C# item in Solution Explorer, then select ‘Add Reference…’.

add-reference

The Reference Manager will display an exhaustive list of libraries available to Visual Studio, and we want to find those with the same namespaces as the imports. For example, as I’ve imported Microsoft.VisualStudio.TestTools.UITesting, I want to select that in the Reference Manager.

referenceManager

At this point, we now have a C# class storing variables accessible to all the projects. The next step is to point those projects to the object.

The projects access the shared C# class/object in almost the same way they access Visual Studio’s library objects. Each therefore requirees an assembly reference to the shared C# file and pointers to its namespace.
For each project in the Solution Explorer, add a reference to the namespace. This will be listed in the Solution menu in the Reference Manager.

referenceManager-our-namespace

Finally the project’s source can be modified to actually use the namespace and access the global/public variables. e.g.

calling-parameters

Now all that’s left to clean up by removing the old variable declarations within the individual projects.

Advertisements