Bob Tway
Bob Tway

Reputation: 9603

Static class not available in certain other classes

I apologise for this - it's an idiot question and I hate myself for asking it, but I can't figure out a reason for this behaviour.

I have a namespace which contains a mixture of static and non-static classes.

namespace MyNameSpace.UI.Helpers
{
    public static class OrderHelper
    { // static methods
    }
}

namespace MyNameSpace.UI.Helpers
{
    public class CountToCampaignConverter : IMultiValueConverter
    { // non-static methods
    }
}

These being helper classes, they're used throughout the application. In this class:

using MyNameSpace.UI.Helpers;

namespace MyNameSpace.UI.ViewModels
{
    public class QuickCountViewModel : BaseViewModel
    {
        private void BuildOrderExclusionsOnCount()
        {
            CurrentAvailability.OrderExclusions = CountHelper.BuildOrderExclusionAsCsv(ordersToExclude);
        }
    }
}

However in this class:

using MyNameSpace.UI.Helpers;

namespace MyNameSpace.Service.Services
{
    public class FulfillJobs : BaseService
    { // stuff
    }
}

When I try to use my helpler classes, I've got access to the non-static ones, but none of the static ones.

I can't see any naming clash here - if that were the problem, surely I wouldn't be able to get the non-static ones either?

So where else can I look to resolve the issue?

Upvotes: 2

Views: 1847

Answers (2)

Steven Calwas
Steven Calwas

Reputation: 64

This is an old post, but no fix was discovered and this issue just happened to me. Here's my particular scenario and the resolution.

I had a Visual Studio 2019 C# solution with two projects: a .NET Standard 2.0 class library and a .NET Core 3.1 console app that used the library. Everything worked fine until I changed the library from .NET Standard 2.0 to 2.1. Afterward, new static items (classes, properties, methods) defined in the library would not be recognized in the app or Visual Studio. If I reset the library back to .NET Standard 2.0 then the static items became visible, but that was not a viable solution for me.

To solve the problem, I set the library's target framework back to .NET Standard 2.1. Then I opened the app project's Reference Manager (Solution Explorer > App Project > Dependencies (right click) > Add Project Reference). I cleared the existing reference to the library and clicked OK. Then I repeated the process, but restored the reference to the library. Suddenly, the app and Visual Studio recognized all the new static items in the library.

Upvotes: 1

Eric Lemes
Eric Lemes

Reputation: 581

The reason is because in the second sample, the class is in a different namespaceMyNameSpace.Service.Services.

If you add a "using" for the namespace MyNameSpace.UI.Helpers in your file, it will be visible.

You can also reference your class as UI.OrderHelper from your services, since both share the root "MyNamespace".

Upvotes: 0

Related Questions