How to reference a class in a subfolder and different namespace? - c#

I'm creating a tiny app that has the following directory structure:
GUI
- MainWindow.cs
Model
Resources
Utilities
Program.cs
MainWindow.cs is inside GUI folder and its namespace is OrtizOL.BackupTimer.GUI , by other hand, Programs.cs is inside the root directory and its namepace is OrtizOL.BackupTimer.
BTMainWindow.cs file:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace OrtizOL.BackupTimer.GUI
{
public partial class BTMainWindow : Form
{
// code ...
}
}
Program.cs file:
using System;
using System.Windows.Forms;
using OrtizOL.BackupTimer.GUI;
namespace OrtiOL.BackupTimer
{
public class Program
{
public static void Main ()
{
Application.Run (new BTMainWindow() );
}
}
}
But, when I need to compile using this command:
csc /t:winexe /out:OrtizOLBackupTimer.exe /recurse:GUI *.cs
It doesn't work. Error message:
CS0246: The type or namespace name 'OrtizOL' could not be found (are you missing a using directive or an assembly reference?)
Do I need to make an assembly of BTMainWindow first? What is correct way to reference a class in a sub-directory?
Thanks in advance?

For the code you posted you have the namespace for BTMainWindow declared as OrtizOL.BackupTimer not OrtizOL.BackupTimer.GUI

You have to correct the namespace of class BTMainWindow from namespace OrtizOL.BackupTimer to:
namespace OrtizOL.BackupTimer.GUI

Related

Visual Studio 2013 - C# - IHttpActionResult cannot be found

Trying to build the test project below, I get this error:
Error: The type or namespace name 'IHttpActionResult' could not be
found (are you missing a using directive or an assembly reference?)
In the "Reference Manager" dialog, Assemblies/Framework, I ticked the System.Web.Http box.
Here is the source:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
namespace dotNET45_test
{
class Program {
static void Main(string[] args) { }
}
class Test : IHttpActionResult { // <-- Error
}
}
Is anything missing in the source code, or in the project settings?

Error CS0234: The type or namespace name 'CustomMarshalers' does not exist in the namespace 'System.Runtime.InteropServices'

using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.CustomMarshalers;
namespace mshtml
{
[DefaultMember("item"), CompilerGenerated, Guid("3050F21F-98B5-11CF-BB82-00AA00BDCE0B"), TypeIdentifier]
[ComImport]
public interface IHTMLElementCollection : IEnumerable
{
void _VtblGap1_3();
[DispId(-4)]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(EnumeratorToEnumVariantMarshaler))]
IEnumerator GetEnumerator();
}
}
The above code gives me the following error:
Error CS0234: The type or namespace name 'CustomMarshalers' does not exist in the namespace 'System.Runtime.InteropServices' (are you missing an assembly reference?)
Pleae advise how to fix? It's a decompiled code, and I am very beginner in C#.
You have to include CustomMarshalers.dll
Example:
Add the reference using Project -> Add Reference -> Browse -> Go to location "C:\Windows\assembly\GAC_32\CustomMarshalers\2.0.0.0__b03f5f7f11d50a3a" -> Select CustomMarshalers.dll
Sorry for resurrecting an old thread. With Framework 4.72, you can use Project -> Add Reference -> Assemblies -> Check the box next to CustomMarshalers.

The type or namespace name 'Window' does not exist in the namespace 'System.Windows'

I am trying to write an extension method for the WPF Window class. I am doing it in a class library project in my solution, so that I can use it in all my projects in the solution.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace ExtensionMethods
{
public static class MyExtensions
{
public static void ResizeToPrimaryScreenWorkingArea(this System.Windows.Window w)
{
}
}
}
When I try to compile this, I get the following error:
The type or namespace name 'Window' does not exist in the namespace
'System.Windows'
Yes, I DID add references to System.Windows and System.Windows.Forms in my class library project, and they are showing under References in the project in Solution Explorer.
What am I doing wrong?
Add PresentationFramework.dll reference to your project it contains the System.Windows namespace.
http://msdn.microsoft.com/es-es/library/system.windows.window(v=vs.110).aspx

CS0234 - Cannot Instantiate C# class in ASP.NET

I made the same ASP.NET C# project in both VS2010 and MonoDevelop using these two classes among the standard files (Site.Master, Web.Config, Default.aspx, etc.) and recieve this same error (CS0234) seen at the bottom.
Login.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using mynamespace;
namespace mynamespace
{
public partial class Logon
{
public void btnClicked(object sender, EventArgs e)
{
//ERROR IS HERE:
mynamespace.Test session = new mynamespace.Test();
//Obviously, this doesn't work either:
Response.Write(session.echoUser());
}
}
}
Test.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mynamespace;
namespace mynamespace
{
public class Test
{
public string echoUser()
{
return "foobar";
}
}
}
I recieve the same error in both IDEs, here is the MonoDevelop error:
The type or namespace 'Test' does not exist in the namespace 'mynamespace' (are you missing an assembly reference?) (CS0234) Logon.cs
Basically, the class Test refuses to instantiate. Any input is appreciated!
If you have that Test class in an ASP.Net web project, then you need to place it in the App_Code folder, not just anywhere in the site.
You need to refer the projects to each other, if you haven't done so yet. I'm guessing that you don't get the intellisense to show the class in the other namespace, right?
You can see the References on the right side (most cases) under your project view. You can right click there and choose to Add reference. Then you browse to the binaries from the pther projects. (You might be able to point to the project itself too - I don't have VS in front of me at the moment.)
Also, It's a convention to use camel case for namespaces, so it should be MyNameSpace.
If the classes are in the same project, you might want to skip using mynamespace and refer to the class by Test intead of mynamespace.Test.
If these are both in the same project, please check that both files are included in the project and have the "Build Action" property set to "Compile". Please also check that all of these namespaces have exactly matching spelling and casing.

The type or namespace name 'form2' could not be found (are you missing a using directive or an assembly reference?)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 myform = new Form2();
myform.ShowDialog();
}
}
}
The following code should "launch" form2. They are in the same project, and have the same namespace. I don't understand. I have tried changing the .NET framework from version 4 client to version 4.
Any other ideas?
right click on Form2, chose "resolve" and pick one of the resolutions, alternatively move it to the WindowsFormsApplication3 namespace.
In my case I created form2, but renamed it later.
I found that although the name of the form changed, the name of the class remained as I originally created it.
For example:
1. Project->Add Windows Form (Setup.cs)
2. File->Save Setup.cs as... (frmSetup.cs)
3. Add code: frmSetup SetupForm = new frmSetup();
However, the actual name of the Setup class did not change with the file name, so changing my code to:
Setup SetupForm = new Setup();
So I was referencing the class name and not the file name solved the problem for me.
You can solve this issue in two wais:
You remove the namespace outside your form's class code or
Add a "using projectName" at the top of the code in your form.
Both forms should have the same namespace

Categories