0

I have to integrate someone's code from a Unity project.

They created a file with the definition of a class that's an array of points, and another file that uses the first file's class.

Problem: I don't see any "using (nameOfTheFile)" at the beginning of the second file: how is this possible? How can I use the first file in the second?

Alessandro
  • 103
  • 3
  • 11
  • This is a duplicate of [this question](https://stackoverflow.com/questions/41421472/how-to-include-multiple-classes-in-one-project-with-one-namespace-how-to-use-th) (I picked the wrong question in the dialog!) – Ruzihm Jul 22 '19 at 01:28

2 Answers2

3

Without seeing the files in particular I can assume they are likely in the same namespace.

A class implicitly has access to everything in the same assembly under the same namespace as itself when they have internal or public visibility, so these two classes are likely in the same namespace and have access to one another.

// brain.cs
namespace Brain {
   public class A {
      public B InstanceOfB { get; set;}
   }
   public class B { }
}

This will still work if the two classes are in separate files, so the above is the same as:

/// brain.A.cs
namespace Brain {
   public class A {
      public B InstanceOfB { get; set;}
   }
}


/// brain.B.cs
namespace Brain {
   public class B { }
}

Ultimately you just need to add the using statement for the namespace they use in their code when adding the files to your project (using Brain; for the example above).

Default Namespaces edit:

Default namespaces are set for classes that don't specify this in the code file. You can find the default namespace (called RootNamespace) in the *.csproj files in a <PropertyGroup> in the <Project> element (usually it's the first PropertyGroup in the file):

<!-- Brain.Common.csproj -->
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  ...
  <PropertyGroup>
    ...
    <RootNamespace>Brain.Common</RootNamespace>
    <AssemblyName>Brain.Common</AssemblyName>
    ...


Tom 'Blue' Piddock
  • 2,131
  • 1
  • 21
  • 36
  • I'm sorry, namespace is not used and the files are too big to be written here. Do you think there's another method or keyword that I should be looking for? – Alessandro Jul 21 '19 at 21:42
  • There is a default namespace used in assemblies when it's not specified in your code, I've edited the answer to show you how to find them. – Tom 'Blue' Piddock Jul 22 '19 at 10:14
0

An alternative might also be to use the partial keyword on a kind of super class and implement your classes as subclasses like e.g.

ScriptA.cs

public partial class SuperClass
{
    public class A { }
}

ScriptB.cs

public partial class SuperClass
{
    public class B 
    {
        public A = new A();
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115