I have created a .cs files that contain the following:
namespace SetUp
{
class Config
{
public static object SetConfig(int code, bool print)
{
//My Code
}
}
}
Compiled it and added the reference to my main project called 'CSharp Side', for example. Added it to my project and everything is great. But my question is how do I access 'SetConfig()'? Because it doesn't recognize 'SetUp' or 'Config' in my code.
Simply make your class as public.
namespace SetUp
{
public class Config
{
public static object SetConfig(int code, bool print)
{
//My Code
}
}
}
You can reference code in a different assembly by fully qualifying:
SetUp.Config.SetConfig(1, true);
or include the namespace with a using directive:
using SetUp;
class SomeClass
{
void SomeMethod()
{
Config.SetConfig(1, true);
}
}
Also, both the class and the method in the referenced assembly need the public modifier. Otherwise they won't be visible outside the assembly where they are defined.
Related
I have Created one ConsoleApplication to understand Access Specifiers.
Below is my code for internal, I can access this class from outside the Assembly.
namespace Assembly_1 //This is first assembly.
{
public class Base
{
//internal class
internal class B
{
public static void fnB()
{
Console.WriteLine("fnB");
}
}
}
}
namespace Assembly_2 //This is second assembly.
{
public class Derived : Assembly_1.Base
{
public class D
{
public void fnD()
{
B.fnB();//how can I access this class?
}
}
}
}
And this is where I am Accessing it.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Assembly_2.Derived.D d = new Assembly_2.Derived.D();
d.fnD();
}
}
}
My Question
Right now I can Access Class B and it's methods like fnB() in Derived.
Everything works fine. but How?
How can I access the B Class outside Assembly_1?
As I worte in the comments:
You are confusing the namespace and assembly terms.
You can read about it here:(Assemblies and Namespace)
Many namespaces can be defined in a single assembly.
If you would like to check and understand the internal modifier,
then you would have to create a new class library project (that will compile into a different assembly), define the Base class there
and add a reference to it in your main console application.
Then you will see that you don't have access to it anymore and the code will not compile.
How can I access the B Class outside Assembly_1?
Because you're confusing namespaces and assemblies. An assembly is a collection of one or more namespaces, contained within a .dll or .exe file.
See also: MSDN: Assemblies in the Common Language Runtime and Understanding and Using Assemblies and Namespaces in .NET.
What you call Assembly_1 and Assembly_2 are namespaces within the same assembly.
Because internal members are visible within the same assembly, you can use Assembly_1.B from Assembly_2.D, because both namespaces reside in the same assembly.
I Have created two .cs files with namespaces ,classes and methods . I want to call the classes of one .cs file into another .cs file. Can u help me how to declare namespace and use the namespace so that i can call the classes of the preceding .cs file.
Please forgive if my explanation is not correct.
Suppose i have the following code.
ClassFile1
using system
namespace namespace1
{
class c1
{
Methods()
}
}
ClassFile2
using system
//here i need to declare the namespace1 .Can u help me how to declare namespace1 in this ClassFile2//
namespace namespace2
{
class c2
{
Methods()
}
}
You can reference the fully-qualified name of the class:
namespace SecondNamespace
{
public class SecondClass
{
private FirstNamespace.FirstClass someObject;
}
}
Or you can add a using directive to the file (note, this is at the file level, not the class level) to include a specific namespace when resolving type names:
using FirstNamespace;
namespace SecondNamespace
{
public class SecondClass
{
private FirstClass someObject;
}
}
Taken from here:
namespace SampleNamespace
{
class SampleClass
{
public void SampleMethod()
{
System.Console.WriteLine(
"SampleMethod inside SampleNamespace");
}
}
// Create a nested namespace, and define another class.
namespace NestedNamespace
{
class SampleClass
{
public void SampleMethod()
{
System.Console.WriteLine(
"SampleMethod inside NestedNamespace");
}
}
}
class Program
{
static void Main(string[] args)
{
// Displays "SampleMethod inside SampleNamespace."
SampleClass outer = new SampleClass();
outer.SampleMethod();
// Displays "SampleMethod inside SampleNamespace."
SampleNamespace.SampleClass outer2 = new SampleNamespace.SampleClass();
outer2.SampleMethod();
// Displays "SampleMethod inside NestedNamespace."
NestedNamespace.SampleClass inner = new NestedNamespace.SampleClass();
inner.SampleMethod();
}
}
}
Note also that sometimes in addition to the "using" entry (I'm not quite clear on how you app is structured, if it's all one project this is probably moot) you may also need to add the reference. Also not sure what environment you're using. From VSExpress while in the project/file that's the recipient click on Project - Add Reference, select solution and then select your namespace.
I have a solution that has many projects in it.
The SqlSmoke.Objects project references the SqlSmoke.Data project.
I set a reference in SqlSmoke.Objects to the solution SqlSmoke.Data. I am then able to compile the entire solution.
However,SqlSmoke.Data does not appear in the intellisense as shown below. However, when I change my namespace to SqlSmoke.Fred, I do see SqlSmoke.Data in intellisense.
I don't see any circular references or other warnings in the Output window that suggest that something else is going on.
What might I look for to understand why I cannot reference objects in the Data project from the Objects project?
using SqlSmoke.Data;
namespace SqlSmoke.Objects
{
public class Class2
{
public void Junk()
{
SqlSmoke.Data. //No intellisense
}
}
}
If I change the Namespace, I get Intellisense:
using SqlSmoke.Data;
namespace SqlSmoke.ObjectsChangedNamespace
{
public class Class2
{
public void Junk()
{
SqlSmoke.Data.CodeObjectData.AddCodeObject("Test"); //Now I see intellisense
}
}
}
You have a [sub]namespace/class name collision. Use:
public void Junk()
{
global::SqlSmoke.Data.
}
and it will work.
Question:
Is it possible to reference public static methods held within the CustomAction class, Votive generates for creating C# managed Custom Actions, from other libraries within the same solution?
I'm having trouble getting a reference to the class and method inside my C# library for the C# Custom Action when trying to create a test bed for the CA.
namespace TestInstaller.InstallCA
{
public class CustomActions
{
[CustomAction]
public static ActionResult InstallUIStart(Session session)
{
//Stuff
return Begin(<Constructed DataClass>);
}
public static ActionResult Begin(DataClass dc)
{
//Stuff I want to test
}
}
}
...
namespace TestInstaller.InstallerTest
{
static class Program
{
Static void Main()
{
//Stuff
//This line is not valid.
TestInstaller.InstallCA.CustomActions.Begin(<Constructed DataClass>);
}
}
}
Despite me adding a reference to InstallCA I cannot add a using statement for TestInstaller.InstallCA or InstallCA, and the compile time error only suggests adding a reference, which I have done.
Is this anything to do with Votive protecting its DLLs somehow?
I have a dll that I'm working with, it contains a class foo.Launch. I want to create another dll that subclasses Launch. The problem is that the class name must be identical. This is used as a plugin into another piece of software and the foo.Launch class is what it looks foe to launch the plugin.
I've tried:
namespace foo
{
public class Launch : global::foo.Launch
{
}
}
and
using otherfoo = foo;
namespace foo
{
public class Launch : otherfoo.Launch
{
}
}
I've also tried specifying an alias in the reference properties and using that alias in my code instead of global, that also didn't work.
Neither of those methods work. Is there a way I can specify the name of the dll to look in within the using statement?
You'll need to alias the original assembly and use an extern alias to reference the original assembly within the new one. Here's an example of the use of the alias.
extern alias LauncherOriginal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace foo
{
public class Launcher : LauncherOriginal.foo.Launcher
{
...
}
}
Here's a walkthrough that explains how to implement that.
Also, you'd mentioned that you tried to use an alias before and encountered problems but you didn't say what they were, so if this won't work then please mention what went wrong.
as Chris said, you can use an alias on your original assembly.
If you can't you that, then you might be able to cheat by using a 3rd assembly
Assembly1.dll (your original)
namespace foo {
public class Launch {}
}
Assembly2.dll (dummy)
namespace othernamespace {
public abstract class Dummy: foo.Launch {}
}
Assembly3.dll (your plugin)
namespace foo{
public class Launch: othernamespace.Dummy{}
}
I'm not even proud of this!
Class name can be identical if it's defined in another namespace, but it boggles the mind why anybody would want to do that to themselves.
Maybe you need to use extern aliases.
For example:
//in file foolaunch.cs
using System;
namespace Foo
{
public class Launch
{
protected void Method1()
{
Console.WriteLine("Hello from Foo.Launch.Method1");
}
}
}
// csc /target:library /out:FooLaunch.dll foolaunch.cs
//now subclassing foo.Launch
//in file subfoolaunch.cs
namespace Foo
{
extern alias F1;
public class Launch : F1.Foo.Launch
{
public void Method3()
{
Method1();
}
}
}
// csc /target:library /r:F1=foolaunch.dll /out:SubFooLaunch.dll subfoolaunch.cs
// using
// in file program.cs
namespace ConsoleApplication
{
extern alias F2;
class Program
{
static void Main(string[] args)
{
var launch = new F2.Foo.Launch();
launch.Method3();
}
}
}
// csc /r:FooLaunch.dll /r:F2=SubFooLaunch.dll program.cs