What causes the errors - CS0246 The type or namespace name 'Lib' could not be found (are you missing a using directive or an assembly reference?)

BenTam-3003 686 Reputation points
2022-08-05T13:34:10.633+00:00

Hi All,

I put a set of commonly used routines into a file, i.e., Library. I create a BaseControlLibrary and plan to sub-class all common controls. I find that putting the following statement in any c# file of the controls of the BaseControlLibray, it will cause an error. I want to know what cause the error and how to fix it?

Lib Lib = new Lib();

Error

228554-basecontrollibrary.gif

BaseControlLibrary

228536-library.gif

Developer technologies C#
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-08-05T15:51:32.123+00:00

    This might help

    Using the following, CustomerDatabaseLibraryEntityFramework project references a class project CustomerDatabaseLibrary. I want to use a using alias in DataUnitTestProject.cs

    using Common = CustomerDatabaseLibraryEntityFramework;  
      
    namespace DataUnitTestProject  
    {  
      
        [TestClass]  
        public partial class EntityFrameworkTest1 : TestBase  
    

    Then use Common as follows

    [TestMethod]  
    [TestTraits(Trait.AllRounds)]  
    public void GetContactTypesTest()  
    {  
        // act  
        IReadOnlyList<Common.Models.ContactTypes> contactTypes =   
            Common.Classes.DataOperations.ContactTypesList();  
      
        // assert  
        Check.That(contactTypes.Count).Equals(13);  
    }  
    

    228540-figure1.png


  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-08-06T09:05:20.603+00:00

    Let's look at it from a different angle. Create using statement first so you know you can access code.

    using System;  
    using System.Windows.Forms;  
      
    // for Repeater  
    using static TeamHelpers.StringHelpers;  
    // using alias  
    using D = WindowsFormsLibrary.Classes.Dialogs;  
    

    Next, I tend to follow Microsoft recommendations to use static classes and methods when appropriate before dealing with creating a new instance of a class.

    Example method to mimic your method, no assertion

    namespace TeamHelpers  
    {  
        public class StringHelpers  
        {  
            public static string Repeater(char value, int count) => new(value, count);  
        }  
    }  
    

    Then let's use it on a Label which counts on using static TeamHelpers.StringHelpers;

    label1.Text = Repeater('_', 30);  
    

    All of this is in the following GitHub repository. Focus on the following

    228753-f1.png


  3. AgaveJoe 30,126 Reputation points
    2022-08-08T14:35:11.59+00:00

    Your repo is at...

    https://github.com/benyltam

    Unfortunately, you did not commit all the code. The code you have committed is hard to understand so I'm still not sure what you're goal is. However, I can create an instance of the Lib class in the BaseForm without an issue.

    private void BaseForm_Load(object sender, EventArgs e)  
    {  
        Lib l = new Lib();  
    }  
    

    My best guess is you are trying to access the Lib class from the BaseControlLibrary project. The BaseControlLibrary is missing from your GitHub repo so I cannot see the code. If this is what you're doing then you're not designing the Visual Studio solution correctly. A library project should never reference the main project otherwise you'll end up with a circular reference.

    I think the Lib class (Library.cs) should be in a separate class library project that both the BaseControlLibrary and NewTims projects reference. Basically, a library project creates a DLL that is copied to the main project's bin folder. I assume the main project is NewTims. The NewTims' project bin is where all the shared DLLs end up. This way the NewTims' EXE and BaseControlLibrary's DLL can find the Lib's DLL within the bin folder. That is, if you create a new class library project (Utilities) for the Lib class (Library.cs).

    Anyway, learn Visual Studio project and solution basics.

    Introduction to projects and solutions
    What are solutions and projects in Visual Studio?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.