C# Error: The type or namespace name ‘List’ could not be found (are you missing a using directive or an assembly reference?)

OMG are your Kidding Me!

OK, I just spent an hour on this thing while trying to get started on another Tim Corey C# Developer tutorial for creating Microsoft Excel files.

When creating a custom model/object for your C# project and try to use it to build a List that made of that custom object type, you’ll be greeted with an error.

C# Error CS0246: The type or namespace name ‘List<>’ could not be found

Error: The type or namespace name ‘List’ could not be found (are you missing a using directive or an assembly reference?) 

Error seen when creating a List of custom objects without using including the using generics statement.

The missing element is a reference to using System.Collections.Generic

Code Example

using OfficeOpenXml;
using System;
using System.IO;
using System.Collections.Generic;


namespace ExcelDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Set the EPPlus license context to nonCommercial so we can play with it :-)
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            var file = new FileInfo(@"c:\scripts\demo.xlsx");
        }

     static List<PersonModel> GetSetupData() 
        {
            List<PersonModel> output = new()
            {
                new() { Id = 1, FirstName= "rick", LastName = "cable" },
                new() { Id = 2, FirstName="bob",LastName= "marley" },
                new() { Id = 3, FirstName="willie",LastName= "nelson" }
            };

            return output;
        }
    }
}

References

As always, give credit where credit is due. In this case, a big thank you to David Morton for the post on the Microsoft Visual Studio forums.

I hope this blog post helps somebody!

Cyber Abyss!

How to Run Visual Basic Scripts as Admin from the Command Line. VBScript Tips and Tricks!

I’ve been using Visual Basic, VBScript, for many years and have used VBScript for a great many things.

I’ve found a lot of value in VBScript over the course of my long IT career. I’ve even saved my employer well over $1,000,000 with a fairly simple VBScript in one instance. Got employee of the month for that one!

If you’re looking for ways to run vbs from cmd, here are 3 ways you can run VBScript files on a Windows PC including running VBScript files as an Administrator via the command line.

3 Methods of Running VBScripts

  1. Run VBSCript in Windows Explorer
  2. Run VBScript from Windows Command Line
  3. Run VBScript as a Windows Administrator

For more VBScript info, try these excellent VBScript resources:

1. Run VBScript in Windows Explorer

  • Locate the VBScript file using Windows Explorer or place a copy on your Desktop
  • Identify VBScript files by the .vbs file extension
VBScript files in Windows Explorer
  • Double click the VBScript file.
  • You may or may not see anything depending on the program’s design.
  • To check to see if VBScript is running.
  • Run Windows task Keys Ctrl+Shift+Esc then sort by Image Name. Look for “wscript.exe”.
  • To kill a VBScript process, right click over “wscript.exe” and select “end process”.
  • You may see more than one “wscript.exe” on the task manager list as it can be run multiple times and new instances will execute.

2. Run VBScript from Windows Command Line

  • Click Windows Start
  • Open Command Window by Typing “CMD” and hit Enter
CMD exe from Windows Start Menu
  • Enter the full file path at the Command Line and hit Enter

3. Run vbs from cmd: Run VBScript as a Windows Administrator

Create a Windows Batch File that uses WScript.exe to open the .vbs file (See example below)

Note this for reference. There is another script host file called, CScript.exe, that can also execute VBScript files.

CScript.exe is in the same directory as WScript.exe so if you need to reference it, just replace by name in the examples below.

If I recall correctly, use CScript.exe if something the VBScript has to do something that is 64 bit OS related with the Windows Meta (WMI).

Windows Management Instrumentation (WMI) is the infrastructure for management data and operations on Windows-based operating systems. 

Right click and select option to Run Windows Batch file as Admin and Admin Rights will pass through to the VBScript

Example Windows Batch File to RUN VBS from the COMMAND Line

Save the file below with a .cmd file extension. Then right click on it and select “Run as Admin”. Admin rights will be passed on to the VBScript

You can download an example from my GitHub repo.

@echo off
pushd %~dp0
C:\Windows\System32\WScript.exe "My_VBScript.vbs"

How to Concatenate a String using VBScript

How to Concatenate a String Value in VBScript

To concatenate string values using VBScript we use the ampersand (&) character to piece together our composite string value.

Code Example:

Srting1 = "Word 1"
String2 = "Word 2"
String3 = String1 & String2
or 
String3 = "Word 1" & "Word 2"
msgbox(String3)

The output value would be “Word 1 Word 2”.

If you’re more familiar with JavaScript, you might be used to concatenating strings using the plus character (+).

In VBScript you can only use the + operator on numeric values.