Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello! Thanks for stopping by.

I'm currently working through a module, and the question I am working through is as follows;

"Define a class Welcome which has one public method called WelcomeMessage(), and should print "Welcome to OOP" when called."

The code I'm given to work from is;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a Welcome object with the same name
            
            
            welcome.WelcomeMessage();
        }
    }
    
    class Welcome
    {
        //complete the class, add WelcomeMessage() method
        
    }
}


And what I currently have is;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a Welcome object with the same name
           Welcome.WelcomeMessage();
        }
    }
    
    class Welcome
    {
        //complete the class, add WelcomeMessage() method
        public void WelcomeMessage()
        {
            Console.WriteLine("Welcome to OOP");
        }
    }
}


I believe I've built the class and method correctly, but I'm unsure what exactly it's asking for when asking me to create a "Welcome object with the same name".

I'm not asking for a direct answer, but I recognize that I'm struggling to understand the module, and having taken 3 days of fiddling on something so simple I'd really appreciate a little help!

The error I currently have is CS0120, after capitalizing the "W" in the first "welcome" and moving past CS0103 (though I'm not convinced that was the right move either!)

Thanks in advance.

What I have tried:

I've tried running it with a capital and lower case "W" in the method call, to try and get away from error CS0103.

I've tried adding "public string welcome;" both in and outside the class.

I feel that I'm trying different things without an understanding of why I'm doing it, which is why I'm asking for help!
Posted
Updated 15-Aug-21 15:14pm
Comments
[no name] 16-Aug-21 1:04am    
The instructions were to "add" code; not "change" and add. That's important when you're told that (which might include an employer).

Your code is almost ok, but you need one more line to create an instance of the class:
//create a Welcome object with the same name
var welcome = new Welcome();
welcome.WelcomeMessage();

Also see: Classes | Microsoft Docs[^]
 
Share this answer
 
v2
To add to what Rick has said, think of classes as cars: I have a car, you have a car - but they aren't the same vehicle as mine is a black Mercedes, which your's is a blue BMW.

You can have passed a test to drive a car, but unless you select the right vehicle you can't drive to the shops: you have to select YourCar not MyCar because the key will only fit one vehicle.

Car is a class: your car and my car are instances of the Car class that are held in different variables: YourCar and MyCar, as well as in ThisCar and ThatCar.
"This car" may also refer to your car, "that car" may also refer to my car, and anything you do to ThisCar will affect your actual vehicle: this is called a reference to the actual instance

But first you have to create the appropriate instances which needs the new keyword:
C#
Car MyCar = new Car("Mercedes", "A180", Black");
Car YourCar = new Car("BMW", "M5", "Blue");
Car ThisCar = YourCar;
Car ThatCar = MyCar;
Console.WriteLine(ThisCar.Color);
Console.WriteLine(YourCar.Color);
ThisCar.Color = Green;
Console.WriteLine(ThisCar.Color);
Console.WriteLine(YourCar.Color);
Console.WriteLine(ThatCar.Color);
Console.WriteLine(MyCar.Color);
Will show:
Blue
Blue
Green
Green
Black
Black
Because both ThisCar and YourCar refer to the same actual vehicle, which is independent of MyCar.

Make a little more sense now?
 
Share this answer
 
Comments
Eve-lyn 15-Aug-21 13:47pm    
It does, but I'm still a little confused where the "var welcome = new Welcome();" comes from.

Does the variable need to have the same name as the class - or is that something that might be confusing me as I don't see necessarily WHY I need to create a "welcome" variable, if the class contains a method to write to the console, shouldn't calling the class do that anyway?
OriginalGriff 15-Aug-21 14:55pm    
:laugh:
No - a variable can have any name, but it's a good idea to make it reflect what you want to use it for: calling is "x1234" makes your code a lot less readable to humans than calling it "customerInvoice". When you read the later, you know what the variable is there for and that helps you understand the code.

The variable itself is needed though, so you have something with which to refer to the instance itself.
If you create a new instance, and then don't store it any where, what use is it?
If you go and buy a new car in the real world, and then throw away the keys and jump on a bus instead of driving it off, what use is the car to you? Keep the keys, register it in your name, park it outside your house - and it's available when you need it. That's what a variable does: give you somewhere to "remember" the class instance you h=just created.

The "writing to the console" bit is rather more complicated, because it's part of more advances stuff like "reusability", "the 3-layer model", and "separation of concerns" which I'm sure you'll meet later when your course gets that far.
For the moment, don't worry about it - you have a fair way to go before that becomes important!

Just think of it this way: if your class interacts directly with the user via the Console, how can you use it when you move to Graphics User Interface (or GUI for short) applications that want to interact via buttons, and labels, and textboxes, with nary a text-based Console in sight?
In this case, your homework specifically requires you not to think about that and code the Console directly into your class. (Which is fine, because you won't use the class probably ever again for anything! Such is the nature of homework exercises ... :laugh: )
charlieg 15-Aug-21 15:16pm    
griff - question for you. I cut my teeth on assembly language programming. How would us old farts explain memory allocation? In other words - you can say "create an instance" all you want but if someone does understand what that means at the base level, how do we explain?
OriginalGriff 16-Aug-21 5:03am    
Analogies are about the best thing I can think of: assembly code (I cut my working life teeth on Z80 and Z8000 assemblers) doesn't have the "defined structures" which make code examples really possible without a good understanding of what the reader uses as code. I generally stick to cars because everybody is used to "this car", that car, your car, my car" and the "object nature" of such things.

I mean, if your assembly code is multithreaded (and I've created such in Z80 before) then you can start talking about the threads and their stacks, queues, and suchlike being independent entities and the "object like" nature of the thread + it's stack and it's data, but ... if it's a linear code example, then it's difficult to know where to start! :D
Welcome to OO programming and I know where you are getting lost.

Running with the other posts, lets say we have a car "class". What you have to keep in mind is a class just defines stuff, it does not do anything. And by do anything I mean no memory allocation, etc. When you say:

Car myCar;

the compiler gets very busy - that's C++ by the way. If you are running this program and hit that statement, memory is allocated and all sorts of other things happen. The thing you have to wrap your head around is that defining a class is nice, but it does not do anything - a class defines an object but it is not an object. An object has been allocated in memory, pointers to methods, etc.

Oh, to answer your specific question, no the object name does not need to be the same as the class name. I could easily say:

Car Suzie;
Car Boat;
Car Eve;

It would be bad programming practice, but in this case, the object name is whatever you want it to be.
 
Share this answer
 
v2
The example code you were given to work with is very bad code: there is no accessor (public. or internal) defined on the method in 'WelcomeMessage, and the mismatch in spelling of the class name is confusing.

If I were you, I would find another teacher, or course. Making students stumble is not education ! If you don't mind, will you tell me where this "module" came from ?

This is what the example should have looked like to focus your learning on the essential:
class Program
{
    static void Main(string[] args)
    {
        // create a new instance of Welcome named 'MyWelcome

        // call the method
        MyWelcome.WelcomeMessage();

        // wait for user input
        Console.ReadKey();
    }
}

class Welcome
{
    //complete the class, add WelcomeMessage() method

    public void WelcomeMessage() // note the required accessor 'public
    {
        // your code here
    }
}
 
Share this answer
 
v3
Comments
Eve-lyn 16-Aug-21 8:03am    
Thanks, your example does set things out in a nicer way!

This was the first mini project for an app called SoloLearn.

Ususally it's pretty clear what it's asking for, but this module really threw me!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900