Beginner C# "using" Question

ellpee

Active member
Joined
Apr 16, 2023
Messages
27
Programming Experience
10+
Hi all! I'm a fairly experienced VBA programmer but in the VERY early stages of getting acquainted with C# and Visual Studio. I'm just going through some exercises in one of the how-to books and something has stumped me. The exercise in question starts with the 'using System;' statement that is pretty standard so far in my explorations. Later on, the exercise tries to create a series of Window objects: A Window(), a ListBox(), and a Button(). All is well until the Builder gets to the Button(), at which point it tells me I may be missing the 'using' statement it needs to recognize 'Button'. I thought that Button was just another variation of a Window, so I don't see how the first two pass the test but Button() fails.

I've looked at the VS help and googled a bit, and found lots of things that explain what the 'using' thing is all about, but nothing that tells me which one I need in order to to recognize 'Button'. First question, therefore, which one do I need to add? And beyond that, where, in VS or elsewhere, can I find a straightforward list of all the 'using' possibilities? Or is there a way to get VS help to tell me which one I need to add, instead of teasing me with "you need one"? Apologies for this kindergarten question, but hey, we all gotta start somewhere.
 
Solution
We don't even really know what you're trying to do. It would help to see the exercise. Right now it looks to be getting you to reimplement a bunch of stuff that already exists and running the risk of generating a pile of conflicts you'll struggle to sort out, so I'm not "down with it", even from an academic viewpoint.

To be clear, the latest version of C# will support all the stuff you see in the book; nothing had been thrown away but C# 3 is positively prehistoric in developmental terms. Also, there have got to be easier ways to learn than typing a book into a code editor; at the very least use your cellphone to OCR the code and Skype it to yourself but seeking an electronic version of a more up to date book would surely take some...
When you look at the documentation for most classes, they will specify which namespace the class lives in. It is that namespace that you will want to add a using declaration for. The alternative is to fully qualify the class name with the namespace.

As a quick aside, using does not go up or down hierarchies. So if you have these three namespaces:
C#:
namespace BlueSun
{
    public class SpaceShip { }
}

namespace BlueSun.BioTech
{
    public class HeartMonitor { }
}

namespace BlueSun.BioTech.Psionics
{
    public class MindReader { }
}

If you have using BlueSun.BioTech;, you will only tell the compiler about HeartMonitor, but not about any of the other classes.
 
Thanks for that, and apologies in advance for more kindergarten stuff. I promise I spent some time trying to DIY before posting this.

So it's not just a "using Xxxxxx;" that I'm missing, it's a namespace : xxxxxxxx or a using xxxxx : yyyyy or something else. Sheeesh! At the barely into Chapter 3 stage I admit I'm lost. Researching 'Button()' tells me the namespace should be System.Windows.Forms, but where the heck do I put that? Just adding it at the top as 'using System.Windows.Forms' doesn't work, still get the same gripe from the Builder except now it doesn't recognize 'Forms' either. C# simple and intuitive? Not so much....
 
Last edited by a moderator:
Post the exact error your are getting.
 
If you are not able to import a namespace then you most likely haven't referenced an assembly in which any members of that namespace are declared. I suggest that you read this to learn something about assemblies and namespaces.
 
Post the exact error your are getting.

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

The Builder repeats this message three times, as the example I'm trying to run refers to Button() more than once in the code. I have only 'using System;' at the beginning of my code, but if I add 'using System.Windows.Forms' I get an additional error, same wording but complaint about 'Forms'.
 
That would be indicative of you not have added the assembly that contains the namespace like noted in post #5.
 
If the System.Windows.Forms namespace is unavailable then that would suggest that you haven't created a WinForms project in the first place, so why are you trying to use WinForms controls? If you create a WinForms project then the required assembly(ies) will be referenced by default.
 
Later on, the exercise tries to create a series of Window objects: A Window(), a ListBox(), and a Button().

There's no such thing as a Window in Windows Forms. The top-level containers are called forms, not windows. They are called windows in WPF and some other XAML-based technologies, but I would have expected a Button control to be available in those as well. What type of project did you create in the first place? What type of project does the book tell you to create? Are you using the same version of VS as the book refers to?
 
Show us the tutorial you're following. It might be making your life harder work than it needs to be ..
 
Show us the tutorial you're following. It might be making your life harder work than it needs to be ..

Small change of course here. To clarify, I'm not working with an actual form project, just typing in code from my how-to book. But looking closely at it just now, I noticed something that might shed light.

Following is the start of the 'Button()' section of my code:

1 reference
public class Button : Window
{
0 references
public Button(int top, int left) : base(top, left)
{
)

Note the little VS-inserted comments about references. Creating the class generates a connection to 1 reference, but the next code line that should be creating a Button() object has no associated references (nor, for that matter, does it have the 'new' that I think should be there when instancing an object. Also don't understand the empty curly brackets.

So I checked the how-to book again and this piece of the code is 100% correctly typed. Is there maybe an error in the BOOK that is causing my frustration?
 
No, new should not be within the that class itself. It some other code where you will call something like new Button(20, 30) which will cause Visual Studio to see that you have a reference to that constructor.
 
Also don't understand the empty curly brackets.

Some sections of code in c# are allowed to exist, but do nothing. This constructor of yours calls the base constructor but does nothing itself hence the empty body (nothing between { } braces). It is like this "if age greater than 100, do nothing"

C#:
if(age > 100){

}

If you're working with windows Ui , creating your own class called Button is likely to bring you a world of pain. Call it something else
 
Well, I'm working with Visual Studio 2022, but at the moment am only trying to build a C# console app, i.e., write some code, build it, and run it in terminal view. The how-to book I'm using, though, is an old edition of "Programming C#" by Jesse Liberty, I suppose it's possible that some things in the book have not kept up with the many upgrades to VS? For sure the code snippet I posted is EXACTLY what's shown in the book....
 
Ok. Promise me you'll chuck that book after you're done with the console app, and you'll replace it with something modern and targeting at least .NET 5.. you're currently working from a book that is at most .net 4, as far as I can see, and that's around 12 years before .net 5. The .net numbering has been wacky in recent years because two product lines were running side by side, but things are essentially settling on .net core 6+ now, with .net framework probably not advancing beyond 4.8

Try and find a book that is .net 6; it's so vastly different to .net 4 that you're cutting yourself off at the knees learning old stuff

Ps, helpfully or confusingly, c# has its own version numbering, as does VS. Use VS2022, C# 10 at least and .net 6

 

Latest posts

Back
Top Bottom