-2

I want to use the Rect Struct and I write Rect aaa = new Rect(); and I get the type or namespace could not be found error. I try adding all the usings as in the example of the web:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

and I get in that the last three imports a the type or namespace Shapes does not exist in the namespace System.Windows. (are you missing an assembly reference?).

At the webpage mentioned, it says the assembly is WindowsBase.dll. And now I don't know how to go on...

UPDATE: I've been told to add a reference, but it's a Core project and that's not possible.

I should install a package but I don't see any package containing this dll. The package WindowsBase is not valid for .net 5, but in the documentation above it says this Struc can be used indeed with .net 5.

I want to use this struct.

E_net4
  • 27,810
  • 13
  • 101
  • 139
xavier
  • 1,860
  • 4
  • 18
  • 46
  • 1
    Have you added WindowsBase.dll as a reference to your .csproj file? – Babak Naffas Nov 18 '20 at 17:39
  • Add a reference to ```WindowBase```. – devsmn Nov 18 '20 at 17:39
  • How? Where? `` ? – xavier Nov 18 '20 at 18:10
  • I followed these instructions https://stackoverflow.com/a/20366962/11241615 but WindowsBase doesn't appear in the COM tab... – xavier Nov 18 '20 at 18:13
  • My project is Core and in Core I can't add assembly references: https://stackoverflow.com/a/56774071/11241615 I have to install a package and I don't know which one... :-( – xavier Nov 18 '20 at 18:27
  • Consider posting the relevant code snippets of your .csproj, to help us all get more context. Anyway, perhaps all you need is to [set `true`](https://stackoverflow.com/a/58383648/3791245) in a property group in your .csproj, and then WindowsBase becomes auto-referenced. – Sean Skelly Nov 18 '20 at 18:48
  • 1
    The very few Project ypes that need Rect already have it included. It is strange and suspect when you need to add it to something else. Also beware that your project will no longer be cross platform and will not run on a server. – H H Nov 18 '20 at 18:58
  • So spell out what your project is about and what (you think) you need Rect for. – H H Nov 18 '20 at 18:59
  • @HenkHolterman: I think this is the problem, indeed. I just wanted a class to work with Rectangles and I found in some StackOverflow they recommended to use Rect Struct. I look at the methods it contained and they were exactly what I needed, but I didn't realize this class was actually for something else. Now I saw there is the Rectangle Struct https://learn.microsoft.com/en-us/dotnet/api/system.drawing.rectangle?view=net-5.0 and I could include it without a problem. – xavier Nov 18 '20 at 19:02
  • Still, I often find difficult to find which is the package needed to include some functionalities, such as extension methods, etc. In particular, if I really wanted to use Rect (not anymore), I still don't know what I have to include to use it, but I'll leave it for another occasion... Thank you for your help! – xavier Nov 18 '20 at 19:04
  • Either the docs or the using declarations point you to the namespace. Which are all documented on docs and are usually the same as the package name. – H H Nov 18 '20 at 19:08
  • Hmmm... you point to `System.Drawing`, that is exactly the namespace (and assembly) to avoid. – H H Nov 18 '20 at 19:10
  • Well, I remember sometimes I have been lost looking for the namespaces and assemblies and the package had nothing to do with their names. That's why I wanted to know if there was a better way to know it. Sadly now I don't remember the case. Now: why `System.Drawing` is to avoid and which rectangle could I use instead? (actually, I had already created my own rectangle class that does exactly what I need, but I was looking for an already implemented library because I thought it would be probably more efficient than my class, and my code would be shorter...) :-O – xavier Nov 18 '20 at 19:14
  • Ok, I saw an article about the problems of System.Drawing... :-( – xavier Nov 18 '20 at 19:19

1 Answers1

1

Short answer: As @imsmn commented, add a reference to WindowsBase. Do that by right clicking on the "References" entry in the Solution explorer and select "Add Reference".

Long answer (or more accurately, suggestion):

Easy way to get the answer in the future: For "built-in" classes that are not ambiguous, you can try this. In Visual Studio, in the code window (C# editor) type:

Rect r = new Rect();

Then hold your mouse over the red squiggly line that appears under the word "Rect". A box will pop-up and one of the options will be "Show Potential Fixes". If you click that, one of the options is often to add the reference (if necessary) and the using directive (if necessary).

JesseChunn
  • 555
  • 3
  • 7
  • Yes, I did the hovering option, but I only get 3 suggestions that have nothing to do with my rectangle structure. – xavier Nov 18 '20 at 18:15
  • Also, when I go to "Add Reference", I don't see the Windows Base in the COM list – xavier Nov 18 '20 at 18:16
  • It is not COM. On the left, select the "Assemblies" entry and then on the search box on the top right type windowsbase. – JesseChunn Nov 18 '20 at 18:19
  • I don't have "Assemblies". I'm using VS 2019 version Version 16.8.1. Perhaps I can add it directly in the csproj file? – xavier Nov 18 '20 at 18:22
  • Ok, I know why. It's a Core project: https://stackoverflow.com/a/56774071/11241615 – xavier Nov 18 '20 at 18:26
  • But still, I don't know which package I have to install, then – xavier Nov 18 '20 at 18:26
  • Until you work out how to add a reference using the VS 2019 interface the last thing you should be doing is manually editing the csproj file. – JesseChunn Nov 18 '20 at 18:27
  • I don't know the VS 2019 interface because I always modify manually the csproj file. But when it comes to add a reference, I never know which package I have to install, and that's why I ask... :-( – xavier Nov 18 '20 at 18:30
  • I guess it will be this one: https://www.nuget.org/packages/WindowsBase/ – xavier Nov 18 '20 at 18:31
  • No, it is not. This one is not valid for .net 5, and Rect is accepted in .net 5... :-( – xavier Nov 18 '20 at 18:34