Changing VB.NET code programmatically - vb.net

I want to open an existing VB class file, add a few properties and close it again.
Simple enough, I thought: Take the CodeDom, a VBCodeProvider, parse the code (using the Parse-method), then identify the location where I want my stuff added (doubtless using some nifty LINQ expressions), add a bit of code and then have it generated and here we go.
Now I see that Microsoft apparently added the Parse method only for the fun of it but never implemented it.
What's the story here? Can I only generate code from scratch? Is it not possible to load existing code?
Does anyone know of any solutions?

You say "class files" and then you say "parse". I think you meant "parse and modify".
Our DMS Software Reengineering Toolkit with its Visual Basic Front End can do what you need on VB.net source code files.
DMS provides general parsing, AST-building, generic analysis and AST transformations, and is able to regenerate source text in compilable form. The Visual Basic Front End enables DMS to process VB.net, VBScript or VB6 and carry out any of these activities.
DMS's Source-to-Source transformations can be used to make changes using "if-you-see-this, replace-it-by-that" patterns.

Related

VB.NET Localization of strings

I've been successful in using form.location.resx files to localize the strings associated with form controls. However, I don't see how to (safely!) add strings to the .resx file(s) and then access them for such things as message boxes.
If I try to add a string to the .resx file using Visual Studio (2017), I get a warning dialog pop-up telling me that this could corrupt the "project item" (form) or my changes could be lost if I change the associated form.
Also, if I use this method, would I need to manually add my strings to each language file separately?
Alternatively, I could create a bunch of Labels to the form with visible=false and then edit their strings in the .resx files and use something like:
msgbox(LabelSampleMessage.text,MsgBoxStyle.Information,LabelSampleMessage2.text)
But that seems like it would be massively inefficient.
For Windows Forms, the best way to do this is to go into the Designer and set the Language property on the form. This will create a formName.Designer.languagecode.resx file for you. Then you just edit the Text properties of the controls. The changes will go to the resx file for the language you're editing. You can even have different layout and control spacing for each language (useful if a label is short in English but translates to something long in German).
For MessageBox messages, you do the same thing with the Resources.resx file. Just put all the messages in your Resources and duplicate the resx file for each language. Then use the Resource editor to translate the message in the other languages. You can then look up the message using My.Resources.Default.SomeMessageKey
#David: Note that I'm the author of a commercial ".resx" localization program for VS (in the interest of full disclosure). Yes, dwilliss is correct, though the names he used are a little off. For Windows forms, you can't manually update the ".resx" files yourself (directly), hence the warning from VS (don't know why they did things this way - go figure). You won't get that warning for strings you put in "Resources.resx" however. Just manually add "Resources.[LangCode].resx" and update it on your own. You have to add the keys yourself (again, no warnings). You can then simply access each string via the static properties seen in the "code-behind" file "Resources.Designer.cs" (one static property exists for each string you add). These are what MSFT calls "strongly typed" resources. If you access, say, "YourApp.Properties.Resources.SomeMessage" for instance (from "Resources.Designer.cs"), it will be returned in whatever language is currently set in "System.Threading.Thread.CurrentThread.CurrentUICulture" (assuming that language's satellite assembly is installed of course). This is all a pain and highly error-prone for other reasons as well, hence the reason I wrote my program (shameless plug but MSFT's way does work, though it's very primitive for handling translation in general - a professional organization relying on an external translator will save a lot of problems and $ in the long run by using a 3rd-party translation program, not mine necessarily though it would be my tool of choice of course, but anything is better than handling it manually).

active reports in C /CLI

Can we use Active Reports 7.0 in C++/CLI? I have just started using active reports. I tried building a report in C# without any problem. I tried doing the same in C++/CLI, but I am unable to use the Active reports toolbox. And also when running the application, it is giving licensing errors.
The designer that generates code-based won't work with C++/CLI. You also won't be able to write "script" inside the reports with C++. However, you can design reports as the XML-based reports (rpx) instead and then you shouldn't have any problem instantiating and calling on those from C++ via the SectionReport class (for example). Something like the following:
GrapeCity::ActiveReports::SectionReport ^sectionReport = gcnew GrapeCity::ActiveReports::SectionReport();
System::Xml::XmlTextReader ^xtr = new System::Xml::XmlTextReader("..\\..\\rptScript.rpx");
sectionReport->LoadLayout(xtr);
xtr->Close();
viewer1->LoadDocument(sectionReport);
...
Keep in mind ActiveReports users are almost exclusively C# & VB.NET users so you won't find any C++ code samples, but it should be pretty trivial to translate the code from C# to C++/CLI.
The example is based on SectionReport not PageReport, but PageReport is entirely xml-based so it should work easily too.
So to clarify, if you want a C++ only solution, you need to do the following:
Create your reports as XML-based reports (*.rpx files) using the "Standalone" designer application that is installed in the start menu when you install ActiveReports. Since you can save your reports as a standalone, independent .rpx file, you will not need to use any C#/VB.NET DLL. As shown in the code example above, you can just load the .rpx files from a file directly from C++ (for example).
Although this technique does not require any C# or VB.NET DLL/EXE, if you use the scripting feature inside the standalone .rpx report file, the script will have to be either C# or VB script. However, you won't have to compile that yourself, ActiveReports deals with the script internally.

Is there a Cut & Paste as New Class File (Macro)?

I'm looking for a free Visual Studio feature, extension or macro. that can help with the following situation.
When I prototype I tend to keep all my classes in one file (bad practice I know, but yeah it a prototype). Then comes the point the where the files is too hard to navigate. So I breakout the classes into separate files inside the project, the folder structure reflecting the namespaces.
To achieve the is;-
1. Add new Folder
2. Add new Class
3. Name class
4. Cut and paste corresponding section into new class file.
For me, Steps 2 through 4 are prime fodder for a new Menu entries.
Cut Class as New Class File
Cut as New Partial Class File.
I've seen this feature in C# but not VB.net.
So does know any how to achieve this for VB.net?
Here's a macro that does what you want in C#... looking at the code it's probably fairly straight forward to modify it to work in VB...
http://plisky.net/main/macros/documentation
Also, I'm pretty sure all the commercial refactoring tools (Resharper, CodeRush, etc.) support this...
Resharper can do this using Move Type to Another File or Namespace
I just stumbled across this and can point you to an updated version of the macro that scrappy kindly linked. Its at http://plisky.net/main/plisy.net-visual-studio-productivity-macros.
If you still want it and wish to test it for VB I can happily make the changes to support VB.net but as I don't use VB I'd need a tester :) As its a while since this post you probably have something working already though.

Include sql scripts in a VB6 application

I am maintaining an old VB6 application, and would like to include SQL scripts directly in part of the project. The VB6 application should then extract the text of this script and execute it on the server.
The reasons for this approach are various - among others, we want to deliver only an updated executable rather than a complete update/installation package. Hence, the SQL scripts need to be compiled into the application just like a resource file. And, obviously, one has to be able to get at the content from code, in order to send it to the database server.
Does anyone have a good way to do this?
The simplest solution is to just create a VB module with the scripts as strings.
If you want to use a resource file instead, you can do that too. You can associate a resfile with a VB project (I don't remember how to do this directly in the VB IDE but the VBP file supports a ResFile32 parameter).
EDIT: It seems like the issue here is mostly about formatting -- you don't want to store SQL queries as one long string, but formatting the query nicely inside VB is tedious because you have to add quotes, add string concatenation operators to join the lines together, etc.
I would recommend placing the SQL in a text file and formatting it in whatever way you like. Write a script that will take the text and convert it into a VB module. The build process would be modified to always apply this script first before compiling the application.
For scripting, use your favorite scripting language; if you don't have a favorite scripting language, this is an easy enough task that you could do it in VB, C#, or any other language. If it were me, I'd probably use awk (gawk) or Python.
If you want to use a resource (.RES) to store your SQL, go to the menu:
Add-ins > Add-in Manager...
and select VB 6 Resource Editor. Configure the add-in to be loaded and to load at startup.
From the editor add-in, VB provides a simple interface to add resource strings. You will refer to these using the provided constant values. To load the strings at runtime, use the LoadResString function:
Public Const SQL_INSERT As Integer = 101
Dim strSQL As String
strSQL = LoadResString(SQL_INSERT)
(replace "101" with the constant value of the string you wish to load)
Just another thought on your approach. Because I find myself tweaking the program's behavior or UI for customers I might be in the middle of a change that either is not ready or has not yet been tested and approved. So if I have properties that change from time to time, but I want to maintain control of, for instance connection settings to our ftp server, I will create a resource only dll exposing my properties and use a resource file in the dll to supply the values. When my network manager changes something on the ftp server I change the strings in the resource maanger, recompile the dll and release just the updated dll. I'm sure there are many more solutions, but that is how I do it. If you don't think you might have to change your SQL scripts at the same time you are changing you exe this probably only complicates your work. It has worked well enough for me that now this is pretty much standard for me.

Is there a utility to create VB.NET classes from an XSD file?

Is there a utility out there that will create VB.NET classes from a Dataset.xsd file? And I don't mean like the XSD.exe utility does - all that does is convert the XML of an XSD file into classes in a .vb - it doesn't append any "extended" functionality.
I'm basically looking for something that will generate the beginnings of a business layer from the XSD file. Like creating a partial class for each datatable, then create a property for each of the datatable's columns as the right datatype and finally the basically CRUD methods as well.
This is something I have to do manually over and over again for each project. (I do lots of little projects and use VistaDB so I can't use Linq-To-SQL - wish I could)
I think that xsd.exe will do what you need it to. Here's and example to convert purchaseorder.xsd to a vb class in the Purchasing namespace:
xsd.exe -c -l:vb -n:Purchasing purchaseorder.xsd
Type xsd.exe /? from a visual studio command prompt to get all of the options.
You can find more info here.
Try taking a look at T4 and Code Generation tools in Visual Studio. It's like "writing code that writes code", and it's incredibly powerful.
A great video, really an "aha experience" for me
http://www.pnpguidance.net/Screencast/T4TemplatesVisualStudioCodeGenerationScreencast.aspx
MSDN:
http://msdn.microsoft.com/en-us/library/bb126445.aspx
Rob Conery has written an intro:
http://blog.wekeroad.com/blog/make-visual-studio-generate-your-repository/
... and so did Scott Hanselman:
http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx
I understand it's probably not exactly what you're hoping for, but when you want more flexibility and NOT having to write the same code over and over again, it really sounds like T4 could be a solution.
You'll write a template, that analyses your XSD file and generates the vb files directly in your project.
I know this doesn't strictly answer the question, but it looks like VistaDB either does, or will soon, have a provider that can be used with Linq to Entities - see here
Liquid studio XML Data Binder looks like it does what you want and has a 30 day trial you can download.