I am new to Visual Studio (2022) and was building my first windows form. It worked perfect until today when I got this error where the namespace "System" is recognized as a local variable. I also searched the web for solutions but couldn't find any I was trying to get rounded corners to my form. The part of code and screen-shot of error list is included below.
public partial class Dashboard : Form
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // width of ellipse
int nHeightEllipse // height of ellipse
);
public Dashboard()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
Region = **System**.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
*Sorry if I am using the wrong terms and Thanks in advance
UPDATE: I tried re-installing Visual Studio (checked with the targeting component as well) and made a new project. Still the Problem persists.
The compiler thinks System
is a variable or a function. You probably have somewhere in the project a variable or a function named System. You can rename that variable or a function, or you can prepend this System use with global::
:
Region = global::System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
There is probably a naming conflict where some variable, class or function in your code is called the same as the namespace System
. There are multiple things you could do here. The obvious thing would be to try and find and rename the thing that is conflicting with the namespace. If you can't you could do one of the following (chose the one that fits your use case and style best):
global
keyword everywhere in your code when you want to access the System
namespace:Region = global::System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
using Sys = global::System;
// ...
Region = Sys.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
System.Drawing
namespace so you don't have to type it everywhere in your code:using global::System.Drawing;
// ...
Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
however this may not work if you have a custom class that is called Region
too. In this case we can use aliasing again. For example a while ago I had to convert a Bitmap
from System.Drawing
to an AvaloniaUI Bitmap
. In these conflict cases you can use using-aliasing in the following way:
using ava = Avalonia.Media.Imaging;
using sys = System.Drawing;
// ...
public static ava::Bitmap ToAvaloniaBitmap(this sys::Bitmap bm)
{
// ...
}
or if you use one of them all the time you could alias the one you use less often and just import the other one normally
using Avalonia.Media.Imaging;
using sys = System.Drawing;
// ...
public static Bitmap ToAvaloniaBitmap(this sys::Bitmap bm)
{
// ...
}
Therefore aliasing is useful as it can spare you having to type all those (possibly very long) namespace names everywhere in your code.
Another thing
if it isn't a naming conflict it might be in rare cases that your IntelliSense is a bit borked. That depends if System
is the only namespace it doesn't find or if it just underlines everything with red and even stops recognizing keywords like class
, etc. In the latter case a quick clean rebuild of the solution or project reload or restarting VS might be everything needed to fix this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.