c# function not in namespace - c#

I have declared a file (UnstuckPackage.cs) in a folder with other cs files.
In another cs file I reference this file (Using folder/UnstuckPackage.cs).
When I call the class in this file Visual Studio does not give an error but when I compile the solutions it yields an error and says me that the class that I have declared is not in a namespace or is not a referenced assembly.
In this folder there are too many cs files that are in the same program, but I recall only this (the one I added after) is not recognized.
Extract of code:
The UnstuckPackage.cs:
using game.Core;
using game.Domain;
namespace game.GameObject.CommandPackets
{
[PacketHeader("$Unstuck", PassNonParseablePacket = true, Authority =
AuthorityType.User)]
public class UnstuckPacket : PacketDefinition
{
public static string ReturnHelp()
{
return "$Unstuck";
}
}
}
And an extract of cs that recall UnstuckPackage.cs:
using game.Domain;
using game.GameObject;
using game.GameObject.CommandPackets;
namespace game.Handler
{
public class CommandPacketHandler : IPacketHandler
{
// <summary>
// $Unstuck Command
// </summary>
// <param name = "unstuckPacket" ></ param >
public void Unstuck(UnstuckPacket unstuck_element)
{
// CODE HERE
}
}
}

You need to reference the class using it's namespace/folder name.
namespace project.namespace
{
class test
{
}
}
in the other class you have to do the following
using project.namespace;
namespace project.namespace2
{
class test2
{
//access test class here
}
}
If the class exist in another project or dll you have to add a reference to them using project properties then add the using line to the class namespace.
Edit: class namespace should be the same as folder name, make sure you didn't do any changes on it and if so use it instead of your folder name.

Related

c# - My .dll File isn't showing Up

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.

How to access to root namespace in debug

How to access to root namespace in debug while we are inside the namespace with duplicated name?
For example:
Root namespace: Project
Namespace which caught a breakpoint: Project.Models.Project (notice duplicated Project segment).
Code:
using Project.Models.Project;
namespace Project
{
class Program
{
static void Main(string[] args)
{
new Foo().Bar();
}
}
namespace Models
{
namespace Project
{
public class Foo
{
public void Bar()
{
//breakpoint! in this moment I want to access to Bar class (there is no `using Project.Models.Issue;` above)
//var boo = new Boo(); Cannot resolve symbol 'Boo'
}
}
}
namespace Issue
{
public class Boo { }
}
}
}
Watch window:
There is access only to nested Project namespace (Project.Models.Project), but I'm not able to access to the root namespace with the same name and go to Project.Models.Issues.Boo.
Any ideas?
You can use the global namespace alias (global::) to indicate that you want to start from the root namespace:
var boo = new global::Project.Models.Issues.Boo()
As #rory.ap has correctly pointed out, in this case you can do without the global namespace alias, but sometimes it is your only choice. Consider this example:
namespace Foo {
class Bar {}
namespace Bat {
namespace Foo {
class Bar {}
}
static class Test {
public static void AccessNS() {
//The global:: alias is necessary here to avoid a creating a Foo.Bat.Foo.Bar()
var fooBar = new global::Foo.Bar();
}
}
}
}
You can do this:
var boo = new Models.Issue.Boo();
The compiler is slightly confused because of the multiple Project namespaces, but within that scope, you only need to qualify it starting with the Models namespace.
The other thing you could do to help the compiler discern between the different Project namespaces is utilize an alias to Project in your using directive:
using Project.Models.Project;
using p = Project;
Then this will work:
var boo = new p.Models.Issue.Boo();
Yet another perfectly good option is provided by Sefe's answer.

namespace not being recognized?

I created a very very simple domain layer in visual studio (2010). I then used the new test wizard to create a basic unit test. However when I try to put in the using statement so that I can test my code.. it says my namespace could not be found... This is my first time using visual studio so I am at a loss as to what I am doing wrong.
My code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Home
{
class InventoryType
{
/// <summary>
/// Selects the inventory type and returns the selected value
/// </summary>
public class InventorySelect
{
private string inventoryTypes;
public String InventoryTypes
{
set
{
inventoryTypes = value;
}
get
{
return inventoryTypes;
}
}
/// <summary>
/// Validate that the inventory is returning some sort of value
/// </summary>
/// <returns></returns>
public bool Validate()
{
if (InventoryTypes == null) return false;
return true;
}
}
}
}
My Test Code
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Home.InventoryType.InventorySelect;
namespace HomeTest
{
[TestClass]
public class TestInventoryTypeCase
{
[TestMethod]
public void TestInventoryTypeClass()
{
InventorySelect select = new InventorySelect();
select.inventoryTypes = "Collection";
if (Validate() = true)
Console.WriteLine("Test Passed");
else
if (Validate() = false)
Console.WriteLine("Test Returned False");
else
Console.WriteLine("Test Failed To Run");
Console.ReadLine();
}
}
}
I'm assuming your test class is in its own project, so you need to add a reference to that project. (A using statement doesn't add a reference, it merely allows you to use a type in your code without fully qualifying its name.)
using refers to a namespace, not a specific class (unless you add an alias for the class name). Your using statement should only include the word Home.
using Home.InventoryType.InventorySelect;
//becomes
using Home;
Here is a link to MSDN on using directive: using Directive (C#)
Declare InventoryType class as public
InventorySelect class can be private rather than public
When you create a "multi-project" in a solution (by adding projects to any existing solution), the projects do not know about each other.
Go to your test project on Solution Explorer and under "References", right click and select "Add Reference". Then select "project" tab and you will be able to add your project's reference to the test project.
Also, make sure you define the classes in the project as "public" to be able to access them in test project.
namespace Home
{
public class InventoryType
{
...
}
}
Note that you still need the "using" keyword on top of your C# test class:
using Home;
namespace HomeTest
{
public class TestInventoryTypeCase
{
...
}
}

Equivalent of php include in c#?

Is there any way to import classes and other files in c# like "include" in PHP ?
There is no include in C#. The closest you can get is creating classes that reside in namespaces, then generate a using for that namespace.
For example, say you create a class in a separate file of your project and assign it to a namespace:
namespace ProjectNamespace.SecondNamespace
{
public static class HelloWorld
{
public static string Greet { get { return "Hello, World!"; } }
}
}
Now, in your main project, you can reference that namespace in two ways. Let's assume your main project files look something like this:
namespace ProjectNamespace
{
public class WhatWeDo
{
public void WeGreetTheWorld()
{
// Here, we greet the world
}
}
}
Now, to put our HelloWorld class into the above code, we can either reference it directly, or add a using to the top of the page. For fully referenced method, substitute the comment above for:
public void WeGreetTheWorld()
{
string greeting = ProjectNamespace.SecondNamespace.HelloWorld.Greet;
}
Or, we can add using ProjectNamespace.SecondNamespace to the top of the file, then just reference the class directly.
using ProjectNamespace.SecondNamespace;
namespace ProjectNamespace
{
public class WhatWeDo
{
public void We GreetTheWorld()
{
string greeting = HelloWorld.Greet;
}
}
}
In .NET you create assemblies that may be referenced in other projects. Those assemblies contain classes that are declared inside namespaces which are brought into scope with the using directive:
using NamespaceName;
You should declare in the top of your file, this:
using System.Collections;

C# namespace data sharing

I am new to .Net C# coding. I have my aspx.cs file reading in an XML File and I want to use this data to create a chart in a silverlight application which is part of the same project but the aspx c# website and silverlight application have different namespaces. How can I send my XML read in data to silverlight application in the same project but different namespace?
The namespace does not present any data boundary; it is simply just a part of the name. So if you have a class defined in your project like so:
namespace Some.Namespace
{
class FirstClass
{
string SomeValue { get; set; }
}
}
...and then want to use this class within another class in a different namespace in the same project, you just need to refer to FirstClass using the namespace:
namespace Some.Other.Namespace
{
class SecondClass
{
void SomeMethod()
{
Some.Namespace.FirstClass temp = new Some.Namespace.FirstClass();
temp.SomeValue = "test";
}
}
}
One alternative, which will make your code a bit more brief is to add a using directive in the code file for SecondClass, which enables you to refer to FirstClass without including the namespace each time:
using Some.Namespace;
namespace Some.Other.Namespace
{
class SecondClass
{
void SomeMethod()
{
// note how the namespace is not needed here:
FirstClass temp = new FirstClass();
temp.SomeValue = "test";
}
}
}
This means that if you have a class that handles accessing the xml file, and that is located in some namespace of your project, you should be able use that same class both in the aspx page and the silverlight application.

Resources