Creating C# Common Modules for Scripts?

Jerry-Malcolm wrote on 7/20/2024, 8:35 PM

I'm primarily a Java programmer. But I can hold my own, at least somewhat, with C#. I'm working on several Vegas scripts. I have a bunch of common code that crosses many of the scripts. I just hate duplicating large blocks of common code in every script. Is there a way in the Vegas C# scripts to create a common class to reference in all of the scripts? I assume I can get a full C# dev environment and build DLLs, etc. But I'm just curious if there's another way to just create a common C# class file in the Vegas scripts folder and just call/reference the common class in a bunch of scripts? Any suggestions are welcome. Thx

Comments

jetdv wrote on 7/20/2024, 10:05 PM

Yes, you can. I actually have a "common.cs" file that I use. Here's how to do it:

Common.cs:
 

using System;
//Use whatever "using" statements you need...
using System.Collections;
using System.Text;
using System.IO;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using ScriptPortal.Vegas;

namespace MyProjectNamespace
{
    public class Common
  //Can be whatever you want, just make sure you're using the same thing everywhere
    {
        private Vegas myVegas;
        
        public Common(Vegas vegas)
        {
            this.myVegas = vegas;
        }

        //put any other routines and variables needed here

        //Sample Routine for an Example to show how to access and call it
        public void OpenLogFile(string LogFile)
        {
            if (DoLog)
            {
                LogStream = new FileStream(LogFile, FileMode.OpenOrCreate);
                LogWriter = new StreamWriter(LogStream);
            }
        }

    }
}

Any other section of my code (in the same namespace):

            Common common = new Common(myVegas);
            common.OpenLogFile(LogPath);

I'll add this to my list of tutorials to make... My current series I'm working on my work out to be used as a good example of how to do this.

Naturally, also, the variables used by the "OpenLogFile" routine would also need to be defined... this is just to show how you would call a routine in the "Common" file.

Jerry-Malcolm wrote on 11/12/2024, 7:36 AM

Hi, I've been swamped recently, and I am just now getting back to my scripting tasks. First of all, thank you so much for your response. I'm just having one problem. Definitely due to my java background and minimal C# background. But I'm still missing something related to C# namespaces. In my common module, I added "namespace myProjectNamespace;" as shown in your example, and I just dropped the common .cs file in the same folder with all of my other scripts. I tried adding "using myProjectNamespace;" to my referencing class. But I get that "type or namespace 'myProjectNamespace' not found (are you missing...?" error popup. Again, my limited C# infrastructure knowledge. Java has 'classpath' that tells it where to look for classes and namespaces. I'm assuming there's something like that required here. But I'm not sure how it all interacts with Vegas being the caller of the script. Do the common modules have to be in a specifically-named folder and/or what is the correct way to reference and locate the common module in the referencing class?

Corollary question.... how can I access your tutorials that you mentioned.

Thanks again so much for you assistance.

jetdv wrote on 11/12/2024, 12:29 PM

@Jerry-Malcolm I made it a tutorial:

Jerry-Malcolm wrote on 11/13/2024, 6:42 AM

Thank you so much, @jetdv. That one little sentence about the common class needing to be compiled was the gotcha for me. Now to expose my total C# ignorance.... what is the IDE you are using? I should have led with that question months ago when I started converting from jscript to CS. It's been a royal pain just writing these scripts in a text editor and then running them in vegas to determine compiler errors, etc. Definitely need that IDE.

jetdv wrote on 11/13/2024, 7:49 AM

@Jerry-Malcolm, I'm using Visual Studio Community 2015.

https://visualstudio.microsoft.com/vs/older-downloads/

I also like 2015 because I can have the download fully downloaded and just install it on a new machine. The newer versions have a "stub" that you download. Then running that downloads all of the other pieces individually each time you want to install.

Why 2015? Because it works for me without some of the issues I've discussed in the newer versions. Do the newer versions work? Absolutely. I find it's just harder to find things in the newer versions. I discuss that here:

And here when trying to find the correct option for Custom Commands:

Jerry-Malcolm wrote on 11/14/2024, 6:10 PM

Edward, thank you so much for your time in assisting me through this. My life for 30 years has been Java. Where the Java vs. C# language syntax is about 95% common, I'm being forced to learn that the similarities end quickly at with C# infrastructure. I'm almost there. But I've hit a wall. I created a solution with a common class and a main script class. Set the reference to Vegas's portal dll as you explained. Did a build. Then told Vegas to run a script, went to the solution bin folder and double-clicked the solution .dll file, and I get this popup. It seems to imply that I don't have the correct .net infrastructure runtime installed. But I went to the .net install page and downloaded everything and every version they offered. Yet, no luck. I'm sure this will end up being something obvious. But I'm not seeing it. Does this exception message mean anything to you? No worries if it's not anything you've encountered. I just thought maybe I could speed things along if it's something you're familiar with.

I'm going to owe you a case of beer with this is all over with.... 😀

jetdv wrote on 11/15/2024, 9:18 AM

Make sure you're building on .NET 4.8.

Jack S wrote on 11/16/2024, 10:31 AM

@jetdv Hi Edward. I've just watched your tutorial and have one question. I have an application extension that contains several .cs modules. If I create a common .cs module for my common routines, where do I place it in my solution explorer, which currently only has tab controls? Do I just create it under all the other .cs modules in the solution?

My system
Genshin Infinity Gaming PC
Motherboard Gigabyte H610M H: m-ATX w/, USB 3.2, 1 x M.2
Power Supply Corsair RM750X
Intel Core i7-13700K - 16-Core [8P @ 3.4GHz-5.4GHz / 8E @ 2.50GHz-4.20GHz]
30MB Cache + UHD Graphics, Ultimate OC Compatible
Case Fan 4 x CyberPowerPC Hyperloop 120mm ARGB & PWM Fan Kit
CPU Fan CyberPowerPC Master Liquid LITE 360 ARGB AIO Liquid Cooler, Ultimate OC Compatible
Memory 32GB (2 x 16GB) DDR5/5200MHz Corsair Vengeance RGB
MSI GeForce RTX 4060 Ti 8GB - Ray Tracing Technology, DX12, VR Ready, HDMI, DP
System drive 1TB WD Black SN770 M.2 NVMe PCIe SSD - 5150MB/s Read & 4900MB/s Write
Storage 2 x 2TB Seagate BarraCuda SATA-III 6.0Gb/s 7200RPM
Windows 11 Home (x64)
Monitors
Generic Monitor (PHL 222V8) connected to GeForce RTX 4060 Ti
Generic Monitor (SAMSUNG) connected to iGPU

Camcorder
SONY Handycam HDR-XR550VE

jetdv wrote on 11/16/2024, 3:59 PM

Yes. Just make it the same as any tab.

Jack S wrote on 11/17/2024, 4:14 AM

Thanks Edward.

My system
Genshin Infinity Gaming PC
Motherboard Gigabyte H610M H: m-ATX w/, USB 3.2, 1 x M.2
Power Supply Corsair RM750X
Intel Core i7-13700K - 16-Core [8P @ 3.4GHz-5.4GHz / 8E @ 2.50GHz-4.20GHz]
30MB Cache + UHD Graphics, Ultimate OC Compatible
Case Fan 4 x CyberPowerPC Hyperloop 120mm ARGB & PWM Fan Kit
CPU Fan CyberPowerPC Master Liquid LITE 360 ARGB AIO Liquid Cooler, Ultimate OC Compatible
Memory 32GB (2 x 16GB) DDR5/5200MHz Corsair Vengeance RGB
MSI GeForce RTX 4060 Ti 8GB - Ray Tracing Technology, DX12, VR Ready, HDMI, DP
System drive 1TB WD Black SN770 M.2 NVMe PCIe SSD - 5150MB/s Read & 4900MB/s Write
Storage 2 x 2TB Seagate BarraCuda SATA-III 6.0Gb/s 7200RPM
Windows 11 Home (x64)
Monitors
Generic Monitor (PHL 222V8) connected to GeForce RTX 4060 Ti
Generic Monitor (SAMSUNG) connected to iGPU

Camcorder
SONY Handycam HDR-XR550VE

Jerry-Malcolm wrote on 11/18/2024, 2:25 PM

Edward... thanks again for all of your help. I finally got everything working using a common module. I may still have a few other questions. But it appears I'm good to go for now.

Jerry-Malcolm wrote on 12/5/2024, 9:27 AM

Edward, one more question.... is there a way to attach the VS IDE debugger to Vegas and run/debug scripts (i.e. breakpoints / single step) in VS while actually executing the script inside a real Vegas project?

jetdv wrote on 12/5/2024, 8:03 PM

@Jerry-Malcolm