Namespace not found - c#

I have defined the following code for my interface:
using Android.Widget;
using Android.Graphics;
using Xamarin.Forms;
using Android.Content.Res;
using MyApp.Droid;
[assembly: Dependency(typeof(MeasureString))]
namespace MyApp.UWP
{
public class MeasureString : IMeasureString
{
public double calculateWidth(string text)
{
Rect bounds = new Rect();
TextView textView = new TextView(Forms.Context);
textView.Paint.GetTextBounds(text, 0, text.Length, bounds);
var length = bounds.Width();
return length / Resources.System.DisplayMetrics.ScaledDensity;
}
}
}
This is my interface class:
using System.Threading.Tasks;
namespace MyApp
{
public interface IMeasureString
{
public double calculateWidth(string text);
}
}
I'm getting the error "Type or Namespace MeasureString not found". Possible missing using directive or assembly reference".
I thought I'd copied everything over from my template interface which works fine and contains just the same namings.
How would I go on investigation what I'm missing?

Your attribute is applied outside the namespace block, so that class isn't actually in scope.
You can either add a using statement for your own namespace or fully qualify the classname.

Related

ASP.NET Core richtexteditor devexpress blazor

My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DevExpress.AspNetCore.RichEdit;
using DevExpress.AspNetCore;
using DevExpress.Web.Mvc.UI;
using DevExpress.Blazor.RichEdit;
using System.Runtime;
namespace TextEditor.Pages
{
public static RichEditBuilder RichEdit(this BuilderFactory factory,
string name
);
}
I get these errors:
Error CS1106
Extension method must be defined in a non-generic static class
Error CS0116
A namespace cannot directly contain members such as fields or methods
Error CS0501
'.RichEdit(BuilderFactory, string)' must declare a body because it is not marked abstract, extern, or partial TextEditor
You need to have a public static class RichEditUtils which then contains this method of yours - you cannot have that method directly in the namespace level...
namespace TextEditor.Pages
{
public static class RichEditUtils
{
public static RichEditBuilder RichEdit(this BuilderFactory factory, string name)
{
// do something here that ends up returning a "RichEditBuilder" ...
}
}
}

How to avoid having to write namespace before class C#

So I made a .dll which I added to my project everything works, but when I try to use any of the class from my .dll. I have to specificly use namespace.classname instead of being able to just say Classname even when I put at the top of my project
using namespace
using System;
using MyTestClassLibrary;
using System.IO;
using YangHandler;
namespace UsingMyclassdll
{
class Program
{
static void Main(string[] args)
{
YangHandler.YangHandler yangh = YangHandler.YangHandler.Parse("Rawtext");
Console.ReadKey();
}
}
}
At the line of using Yanghandler visual studio says
Using directive is unnecessary
Isn't this what using is exactly used for to use other namespaces?
YangHandler code
using System;
using System.IO;
namespace YangHandler
{
public class YangHandler
{
public string YangAsRawText { get; private set; }
public static YangHandler Parse(string YangAsRawText)
{
YangHandler handlerToReturn = new YangHandler();
handlerToReturn.YangAsRawText = YangAsRawText;
return handlerToReturn;
}
I know that it could be solved by using namespace aliases under the namespace "UsingMyclassdll" like
using YangHandler = YangHandler.YangHandler;
But isn't there a more normal solution?
Check this very interesting piece of documentation from Microsoft: https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-namespaces
DO NOT use the same name for a namespace and a type in that namespace.
For example, do not use Debug as a namespace name and then also provide a class named Debug in the same namespace. Several compilers require such types to be fully qualified.
So your work around is basically defining the fully qualified name as the type and namespace are of the same name.
No work around for this. The compiler can't know if you mean the one or the other.

When importing namespace compiler returns error message type or namespace name could not be found

When I'm trying to import a namespace from another file, the compiler gives me the error message, "type or namespace name could not be found". Take a look at my code below. Both files are in the same directory, so I don't understand what's wrong.
Setup.cs (the main file)
using System;
using ScreenTextLine; // Error is here.
namespace Setup
{
namespace Screen
{
class Text
{
static void Main(string[] args) { }
}
}
}
Problem.cs (the file with the namespace I'm trying to import)
using System;
namespace Setup
{
namespace ScreenTextLine
{
class WelcomeText
{
static void Main(string[] args){}
}
}
}
You need to use their fully-qualified name when referencing them by separating each namespace by a dot using the following format...
[Top_level_namespace].[nested_namespace]
So, this nested structure...
namespace Setup
{
namespace Screen
{
}
}
It's referenced by a using statement this way...
using Setup.Screen;

I am unable to access any private variable inside of the same class. Anyone know why I cannot access the double "job"?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class test
{
private double job = 4.2; // <-- declared it here
job = 5.7; // Giving me an "Error CS0103 The name 'job' does not exist in the current context."
}
class Program
{
static void Main(string[] args)
{
}
}
}
The double variable "job" or any other variable that I create (ex: public or static) I cannot use in the class. This is happening in Visual Studio 2015. I have not seen this before nor do I know what could cause this so any help would be appreciated.
You can't change the variable inside the class itself. You're only allowed to change it inside a function:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class test
{
private double job = 4.2; // <-- declared it here
void changeJob() {
job = 5.7; // Changed the line to be inside a function
}
}
class Program
{
static void Main(string[] args)
{
}
}
}
Right inside the 'Test' class is declaration level. 'job=5.7' is not declaration. You can always use it inside any method, but not at declaration level.

Missing directive of assembly reference

I added an existing form (and its references) to another project and I am trying to show the new form. There are no coding issues, just a reference error:
The type or namespace 'frmEmail' could not be found (are you missing a
using directive or an assembly reference?)
I cannot figure out what "using" or reference I failed to use when importing the other form. Any ideas?
Here is the code causing the error:
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 Notify_Setup
{
public partial class frmNotifications : Form
{
public frmNotifications()
{
InitializeComponent();
pbBlue.MouseEnter += new EventHandler(pbBlue_MouseEnter);
}
private void pbGreen_Click(object sender, EventArgs e)
{
frmEmail frmEmail = new frmEmail();
frmEmail.Show();
this.Hide();
}
}
}
You need to pull the namespace in. For example, if this was how your form currently was in the other project:
namespace Your.Form.Namespace { // this is important
public class YourForm : Form {
// stuff
}
}
Then in the project you're adding it to.. you need to add your assembly as a reference, then import the namespace in like this:
using Your.Form.Namespace; // import the namespace
namespace Other.Project {
public class OtherClass {
YourForm _form; // this is fine now
}
}
The other options is to fully qualify the type. What this means, is to use the entire namespace and type name in the declaration. It would be like this:
namespace Other.Project {
public class OtherClass {
Your.Form.Namespace.YourForm _form; // this is fine too
}
}
I added an existing form (and it's references) to another project and I am trying to show the new form.
It looks like the form you added was frmNotifications, but is creating an instance of frmEmail. Did you also added it?

Categories