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...
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


Argh. Okay, will check all installed versions and look for a matching book. Best I know, all that came with the VS install a week or so ago, so I'm thinking they should all be okay. But in the meantime, is this exercise never going to work? or can someone simply tell me what 'using' to add? If no, I'll back off until I have all my versions sorted out.
 
Good then that book should work with what you are trying to do with that book. There shouldn't be too many language changes since C# 3.0 and C# 7.3 that would be shockingly different -- just a lot on syntactic sugar that makes C# 3.0 less verbose.

Anyway to answer your question if you had written:
C#:
namespace Lesson5.Exercise2
{
    public class Button : Window
    {
        public Button(int top, int left) : base(top, left)
        {
        }
    }
}

then in your other code you would use using Lesson5.Exercise2.
 
s this exercise never going to work? or can someone simply tell me what 'using' to add?

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 of the pain out of it..
 
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 of the pain out of it..

Pleased to announce got it to run after adding a namespace at the very top of my code and hunting down one last stray ')' that was throwing some pesky punctuation errors. Something about that suddenly allowed it to recognize 'Button()', which was my biggest headache and the reason for my original post. C# is very picky that way, and part of my learning is to use all the clues that are available in the code editor to help me. VBA has similar-but-different dummy aids, just have to get used to the new VS universe. Thanks and bye to all for now, have to go find a newer book.
 
Solution

Latest posts

Back
Top Bottom