Trouble with references except on rebuild - c#

I have a solution with two projects, A and B. A contains three classes, each in a different namespace, and B depends on and references A.
In B, I can use two of the classes in A, but it can't see the third namespace. The exception is if I do a rebuild of A, in which case the third namespace starts showing up in intellisense and everything works until the next time I do a full build or publish.
Any ideas? This is C# code in Visual Studio 2013.

It would have been easier to answer a more specific question. So I just take a shot from the hips...
Clashing namespaces can cause such an error. Intellisense can then resolve a method but you can't compile it. For example this seems correct, but won't compile:
namespace Business.Apartment.HR
{
public class Class1
{
}
}
namespace Apartment.HR.Area
{
public class Class2
{
}
}
namespace Business.Apartment
{
public class Caller
{
public Caller()
{
var c1 = new HR.Class1();
var c2 = new Apartment.HR.Area.Class2();
}
}
}

Related

How to Import Old code from a Different namespace into My Current Project?

I want to use old code in a new project in c#.
This is the old code. It is saved in an arbitrary folder on my PC.
namespace OldCode{
class HellowWorld
{
public static void SayHello()
{
Console.writeline("Hello World")
}
}
}
I want to use this in a new project called new code.
using OldCode (?)
namespace NewCode
{
class SayHelloNew
{
HelloWorld.SayHello();
}
}
My question is, how do I link to my old project? Did I do it right with using OldCode? Or is there some other dependency file I need to set up?
Yeap, Let me describe for you,
First you should make public your class to access it in other sources by adding public modifier like this:
using System;
namespace Stackoverflow.OldCode
{
public class HelloWorld // <----------- make it public
{
public static void SayHello()
{
Console.WriteLine("Hello World!!!");
}
}
}
Then I suggest adding a prefix in every other namespaces to show they are related to each other, like
namespace Stackoverflow.OldCode;
namespace Stackoverflow.NewCode;
The NewCode project and the OldCode should be in one solution, if not now, right-click on the solution name and choose: Add>New Project... or Add>Existing Project...
Then you should add the NewCode project as a reference to the OldCode project:
step1:
step2:
Now you can access old project methods easily, like:
using Stackoverflow.OldCode; // <---- add the reference
namespace Stackoverflow.NewCode
{
class SayHelloNew
{
public static void SomeRandomName()
{
HelloWorld.SayHello(); // <----- call it
}
}
}
That is almost correct. The colon (:) needs to be removed from the namespace declaration and to access the HelloWorld class externally, it must be public.
OldCode\HelloWorld.cs
namespace OldCode
{
public class HelloWorld
{
public static void SayHello()
{
Console.writeline("Hello World")
}
}
}
NewCode\SayHelloNew.cs
using OldCode;
namespace NewCode
{
class SayHelloNew
{
HelloWorld.SayHello();
}
}
You will also need to ensure the compiler has access to OldCode on compilation, or, if using Visual Studio, add a Reference to the OldCode project in the NewCode project.
Add references in Visual Studio

C# class from library not found or does not exist

I have a C# WinForms program and I created also a C# library. I added in the main project a reference to the library project. Oversimplifying, the code looks like following:
The Library:
namespace nsDiscovery
{
public class Foo1 { }
public class ClassDiscovery
{
public class Foo2 { }
...
The Program:
using nsDiscovery;
namespace nsMDNS
{
public partial class Form_MDNS : Form
{
private void TstFooA( Foo1[] f1_, ClassDiscovery.Foo2[] f2_ ){}
private void TstFooB()
{
Foo1[] f1 = new Foo1[ 0 ];
ClassDiscovery.Foo2[] f2 = new ClassDiscovery.Foo2[ 0 ];
}
...
The function TstFooB() has no problems but function TstFooA() triggers at compile time two errors:
The type or namespace name 'Foo1' could not be found;
The type name 'Foo2' does not exist in the type 'nsDiscovery.ClassDiscovery
The even more annoying thing is that I have also created a separate dummy solution just to experiment all the above and there it works. So in the real project(s) I must have done something wrong but I can't figure out what. It's true that the real project and the real library are actually much more complex.

How can I avoid typing the full namespace hierarchy for c# objects in Monodevelop ("using" isn't working)?

I have a hierarchy of namespaces like My.Namespace.MyObject in a library. My understanding is that if I include using My.Namespace; at the top of a source file that I should be able to use Object directly. Unfortunately, it only works if I type out the entire My.Namespace.MyObject, neither Namespace.MyObject nor MyObject alone will work. In trying to research this I've found that it can happen when classes and namespaces share names but this is not the case for me. It's really hard to Google for "using not working" so I haven't been able to find much else that might be relevant.
A full example is as follows. In one project I do:
namespace My {
namespace Namespace {
public struct MyObject {}
}
}
Then I build this which produces a dll file. In a second project I add the dll as a reference and then do:
using My.Namespace;
public class AnotherObject
{
public static void Main()
{
//results in a compilation error, while My.Namespace.MyObject doesn't
MyObject a;
}
}
Project A, compiled into LibraryTest.dll:
namespace FirstLevel
{
namespace SecondLevel
{
public class LibraryClass
{
public LibraryClass()
{
}
static void Main()
{
}
}
}
}
Project B, just a console app, using Project A library:
using FirstLevel.SecondLevel;
namespace ConsoleTest
{
class MainClass
{
public static void Main (string[] args)
{
LibraryClass test = new LibraryClass();
}
}
}
Important: Make sure your library is added to References in the other project
right click on References -> Edit References...
switch to .NET Assembly
click on Browse...
select your library
See also attached screenshot.

how to use a new class method in other apps?

I used this code in c++ to use a class (that I defined before)in my other Apps.
#include class_name ;
How can I define a public class that could be used in all apps?
Thanks
To access classes from external assemblies you must add a reference to an external assembly. This will allow you to access public classes from the external assembly.
To specify a class from a namespace outside your current scope you must prefix the class's type specifier with its namespace name. To avoid this overhead, you can "include" the external namespace with the using directive.
Multiple namespaces can exist within a single assembly.
Assembly Fruit:
namespace Common
{
public class Strange
{
var mystery = new Mystery() // Won't compile, no reference to Mystery.
}
}
namespace Fruit
{
public class Orange
{
}
}
Assembly Vegetables:
References Fruit
namespace Common
{
public class Mystery
{
}
}
namespace Fungi
{
public class Mushroom
{
}
}
namespace Vegetables
{
using Common;
public Class Carrot
{
var strange = new Strange() // Compiles correctly.
var mystery = new Mystery() // Compiles correctly.
var orange = new Orange() // Won't compile, what's an Orange?
var orange = new Fruit.Orange() // Compiles correctly.
var mushroom = new Mushroom() // Won't compile, what's a Mushroom?
var mushroom = new Fungi.Mushroom() // Compiles correctly.
}
}
You need to create a Class Library project, which compiles to a DLL file.
You can then add a reference to it in other projects.
You have to include the namespace as below and also add any references if in another project.
using class_namespace;
If the other class is public or internal (and in the same assembly if they are internal), is in the same project, and has the same namespace, then you don't need to do anything at all. You will be able to refer to the other class by just using it's class name.
If they are in different namespaces then you can use a using statement (at the top of the file) to bring the other namespace into scope, or you can refer to the other class using the fully qualified name (i.e. OuterNamespace.InnerNamespace.ClassName) every time you use the class. (Which almost nobody ever does, everyone just uses using statements because they are so much more convenient.)
If the class is in another project entirely then you will need to add a reference to that class through visual studio. If you are creating a project that is designed to be referenced by other projects then it's project type should be a "class library".
Every class is created under a namespace
namespace abc{
public MyClass{
//functionality
}
}
To use your class on a different application,you need to import the namespace.
using abc;
public class usingClass{
MyClass obj = new MyClass();
}

C# Compiler says function is not defined, when it is

I just need another pair of eyes... I don't see anything wrong with the following. In fact, I swear I had something just like this not long ago, and it worked.
In my Collections.dll:
namespace Collections
{
public class CSuperAutoPool
{
public static CSuperAutoPool ActivateByType(Type typeToBeActivated, params object[] activatedArguments)
{
//...
}
}
}
In another DLL, I have referenced the collections DLL project, and use it in this function:
namespace Organization
{
public class CBaseEntity : CSuperAutoPool
{
protected static CBaseEntity Create()
{
//...
CBaseEntity created = (CBaseEntity)CSuperAutoPool.ActivateByType(callingType); //Error here.
//...
}
}
}
Error: 'Collections.CSuperAutoPool' does not contain a definition for 'ActivateByType'
I have used ActivateByType, within CSuperAutoPool, in a different function, and that one does not have errors. The Collections DLL compiles without errors. In the same DLL where the Organization namespace exists, have used various other aspects of the CSuperAutoPool class in other ways, without compiler errors.
There must be something missing from your example, or you are not using the version of the code that you think you are using, e.g. could it be that there is another class called CSuperAutoPool in your project, possibly in a referenced assembly?
The following snippets compiles without errors:
namespace Collections
{
public class CSuperAutoPool
{
public static CSuperAutoPool ActivateByType(
Type typeToBeActivated, params object[] activatedArguments)
{
//...
return null;
}
}
}
namespace Organization
{
using Collections;
public class CBaseEntity : CSuperAutoPool
{
protected static CBaseEntity Create()
{
Type callingType = null;
//...
CBaseEntity created =
(CBaseEntity)CSuperAutoPool.ActivateByType(callingType);
//...
return created;
}
}
}
Found it! 0xA3 gave me the hint I needed with: "you are not using the version of the code that you think you are using"
When I added the Collections reference to the Organization project, it did not checkmark the Collections project to compile in the Configurations Manager. In other words, my Collections DLL was not compiling unless I did it by hand.
Thank you, that's what I meant by an extra set of eyes. :-)

Categories