locked
Testing a web class library .dll RRS feed

  • Question

  • User113730038 posted

    I have created a class library .dll and added a web project to use for testing (may not be the best option, I have also tried a console app).  I am using VS 2017 community and I am having an issue accessing the class library.

    Here is my code (part of the class library):

    namespace MyCode
    {

        public class MyCode
        {    
            PageHelper.SharedUtil util;

        }

    //constructor
    public MyCode()
    {
    util = new PageHelper.SharedUtil();
    }

    OTHER METHODS HERE THAT ARE PUBLIC

    }

    ----- - here is how i am calling the constructor ----

    MyCode.MyCode tc = new MyCode.MyCode();

    tc.MethodNameToCall();

    I am getting object reference error on the line above.  I have put breakpoints all over the library and it never gets hit.  

    Any help is greatly appreciated.

    Thanks,

    Mark

    Wednesday, July 31, 2019 6:56 PM

All replies

  • User475983607 posted

    I am getting object reference error on the line above.

    What is the actual error?  A null reference error?  The error message usually shows what item is null.

    I have put breakpoints all over the library and it never gets hit.  

    Make sure the project/solution is configured for debug.  

    Wednesday, July 31, 2019 7:03 PM
  • User113730038 posted

    Object Reference error - it's like it isn't finding the dll

    Wednesday, July 31, 2019 7:11 PM
  • User475983607 posted

    Object Reference error - it's like it isn't finding the dll

    You are not provided enough information. 

    Did you make a project reference to the library project?   Are you missing an using statement at the top of the cs file?  

    Your library has a hard dependency on something named PageHelper.SharedUtil();  

    util = new PageHelper.SharedUtil();

    What is this a factory?

    Anyway, see the follow documentation which explains how to create a library project in Visual Studio and create a reference to the library.  You do not have to use .NET standard and can pick whatever framework you like.

    https://docs.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio

    https://docs.microsoft.com/en-us/dotnet/core/tutorials/consuming-library-with-visual-studio?tabs=csharp

    Wednesday, July 31, 2019 7:24 PM
  • User113730038 posted

    Yes i added a reference to MyCode from the web project.  I tried to put the using to it as well AFTER i added the reference and it was greyed out so i removed that using.  I tried removing the hard dependency as well with no luck.  I just put a return "Hello World" and it's still giving me a object reference. 

    One thing i forgot to mention, the library was 2 classes in our main app and all the classes were static.  I copied them to the new project and changed them ALL to public then created this new class library app.  I then added the web project and added a default.aspx to test.  Let me know if you need any more information and thanks for the help.

    Wednesday, July 31, 2019 7:39 PM
  • User475983607 posted

    I tried to put the using to it as well AFTER i added the reference and it was greyed out so i removed that using.

    That just means the library was not used in the code.  You need to using statement.

    I just put a return "Hello World" and it's still giving me a object reference. 

    I assume you are referring to a null object reference. Is there anyway you can provide the actual error so we don't need to guess?

    Wednesday, July 31, 2019 7:56 PM
  • User113730038 posted

    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2147467261
    HelpLink: null
    InnerException: null
    Message: "Object reference not set to an instance of an object."
    Source: "MyCode"
    StackTrace: " at MyCode.MyCode.GetToken() in C:\\Code\\CLQ\\Tools\\MyCode\\MyCode\\MyCode.cs:line 210\r\n at MyCodeTester.Default.Button1_Click(Object sender, EventArgs e)"
    TargetSite: {System.String GetToken()}

    Strange thing is i put a break point on that line (210) and it never gets hit.  I am trying to call GetToken() and i have a breakpoint at the beginning and it never hits.

    Wednesday, July 31, 2019 8:10 PM
  • User113730038 posted

    Is this normal to throw an error before i get into the method?  I commented out the code above and it's throwing another error in the same method.

    Wednesday, July 31, 2019 8:14 PM
  • User475983607 posted

    skister77

    Is this normal to throw an error before i get into the method?  I commented out the code above and it's throwing another error in the same method.

    Execution cannot get to the method because the actual object is null.  You're code is doing this.

    null.GetToken();

    The original class posted is not syntactically correct.  I assumed you were showing bits of the class but maybe not...???

    Here is a basic class pattern.

        public class MyCode
        {
            private string _classMember = string.Empty;
            public MyCode(string classMember)
            {
                _classMember = classMember;
            }
    
            public string GetToken()
            {
                return _classMember;
            }
        }

    Implementation.

    MyCode code = new MyCode("Hello World");
    string result = code.GetToken();

    Result

    Hello World

    Use the Visual Studio debugger to check what is not by placing a break point on the line that cause the error. Then place you cursor over each item and see which is null.  You can also use the watch window.  The following link explains how to use the debugger.

    https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019

    C# Programming guide

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes

    Wednesday, July 31, 2019 8:57 PM
  • User113730038 posted

    Thanks, I will try this.

    Wednesday, July 31, 2019 9:39 PM