3

I have created a C# Class Library. In it I insert two classes. For example:

Apple.cs Orange.cs

namespace FoodLibrary
{
 namespace Apples
 {
    public class Apples
    {
        public string type { get; set; }
        public string colour { get; set; }
        public string size { get; set; }
    }
 }
}

Almost an exactly identical one is formed for Orange.cs (The namespace Apples and class Apples would be turned into "Orange").

EDIT (Here is the requested Orange.cs):

namespace FoodLibrary
{
    namespace Orange
    {
        class Orange
        {
          public string colour { get; set; }
          public string type { get; set; }
          public string size { get; set; }
        }
    }
}

After building/rebuilding any combination I will get a .dll in the debug folder. Upon referencing this DLL it appears that I only have access to ONE namespace/class (ie. Apple). It allows me access to the first class I create in my Class Library. It doesn't matter how many classes I make, I only get one in my DLL.

I have had the same results in:

Visual Studios 2010 Visual Studios Express 2008 (C#)

Side Note: If I update the one class that works (ie. add a new property) it will change the DLL when I build. I have tried "clear","rebuild", and "build".

EDIT: Evidently I'm an idiot and didn't realize the orange wasn't public. Once I changed it, it worked. Not really sure why the class generated by a new project is "public" but when adding a new class it isn't or vice versa.

Thanks for the suggestions everyone.

  • What is the code for the second class? – maximpa Mar 19 '13 at 23:47
  • 2
    Can you post the code for Oranges.cs? – nick_w Mar 19 '13 at 23:47
  • 1
    Why do you have an `Apples` namespace for the `Apples` class? Why not just have `Apples` and `Oranges` in `FoodLibrary`? – Reed Copsey Mar 19 '13 at 23:51
  • I have added said Orange.cs. Please keep in mind these are just examples of a dumbed down project. I have created this example (with Apples and Oranges) and both projects are producing this result. I simply used this one for ease of understanding. – user2188733 Mar 20 '13 at 00:13
  • 1
    @ReedCopsey they always say you can't compare apples and oranges, so they clearly don't belong in the same namespace! :) – Dmitriy Khaykin Mar 20 '13 at 00:21

3 Answers3

5

Your Orange class is not declared as public like your Apples class is. Try declaring Orange as public - you should be able to access it then.

nick_w
  • 14,758
  • 3
  • 51
  • 71
  • Wow I'm a moron. Thank you, I will be on my way now. I assumed creating a class made them public by default. I won't make that mistake again. – user2188733 Mar 20 '13 at 00:20
  • Why when creating a new project is the class public but when adding a new one, not? – user2188733 Mar 20 '13 at 00:21
  • @user2188733 There's a good discussion of why this happens in this [question](http://stackoverflow.com/questions/824555/why-visual-studio-doesnt-create-a-public-class-by-default). This [question](http://stackoverflow.com/questions/700086/how-do-you-default-a-new-class-to-public-when-creating-it-in-visual-studio) gives you information on how to change this behaviour. – nick_w Mar 20 '13 at 00:27
  • For anyone wondering, the default for a class is `internal` when no access modifier is specified. – Issung Mar 16 '21 at 04:06
0

In Visual Studio, select the Orange.cs file in the Solution Explorer, and look at the Properties pane. Is the Build Actions set to Compile? If not, the file isn't being included as source code.

Visual Studio, for some odd reason, sometimes changes the build action to something else when I create a new file. Check this guy's blog for a screenshot and similar problem.

http://dimarzionist.wordpress.com/2008/07/24/strange-vs-2008resharper-behaviour-buildaction-property/

Nick VanderPyle
  • 2,939
  • 3
  • 26
  • 33
  • Unfortunately this is not the issue as both Class files have said property set to "Build". – user2188733 Mar 20 '13 at 00:09
  • It was worth a shot. :) I don't suppose you can take a screenshot with the Orange.cs file open? Someone else might see the problem and have a good answer. – Nick VanderPyle Mar 20 '13 at 00:13
0
  1. Keep your namespace names different to your class names ie namespace = AppleS class = Apple
  2. Change namespace ClassLibraryTest to FoodLibrary
Chris McKelt
  • 1,378
  • 2
  • 17
  • 38