C
C#7mo ago
McCormick

The result won't be shown in console

1. Code (Where result won't be shown in console)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _Data1
{
internal static class Cals
{
public static void Data2(int a = 10, int b = 20)
{
int g = 20;
int v = 40;

int result = (a + b) * g / v;

int h = result;

Console.WriteLine($"The result of h: {h}");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _Data1
{
internal static class Cals
{
public static void Data2(int a = 10, int b = 20)
{
int g = 20;
int v = 40;

int result = (a + b) * g / v;

int h = result;

Console.WriteLine($"The result of h: {h}");
}
}
}
2. Code (Everything normal)
enum WeatherData
{
Morning = 7,
Afternoon = 16,
Evening = 17,
Night = 12
}

public class Program
{
public static void Main(string[] args)
{
PrintWeatherData();

}

public static void PrintWeatherData()
{
int morningValue = (int)WeatherData.Morning;
int afternoonValue = (int)WeatherData.Afternoon;

int result = morningValue + afternoonValue;

Console.WriteLine($"Morning + Afternoon = {result}");
}
}
enum WeatherData
{
Morning = 7,
Afternoon = 16,
Evening = 17,
Night = 12
}

public class Program
{
public static void Main(string[] args)
{
PrintWeatherData();

}

public static void PrintWeatherData()
{
int morningValue = (int)WeatherData.Morning;
int afternoonValue = (int)WeatherData.Afternoon;

int result = morningValue + afternoonValue;

Console.WriteLine($"Morning + Afternoon = {result}");
}
}
41 Replies
Moods
Moods7mo ago
Well You didn’t call the method
McCormick
McCormick7mo ago
Console.WriteLine($"The result of h: {h}"); I did
Moods
Moods7mo ago
No I mean Data2 You didn’t call that
McCormick
McCormick7mo ago
PrintWeatherData? It works, why?
Moods
Moods7mo ago
That is not Data2 Well That works because you called that in Main The program executes everything in Main() starting from the top You did not call that method in Main
McCormick
McCormick7mo ago
hm
Moods
Moods7mo ago
In the Main method, do _Data1.Cals.Data2() Like just put that there
McCormick
McCormick7mo ago
Where we name the method?
Moods
Moods7mo ago
No inside your Main method It’s where the program starts executing
McCormick
McCormick7mo ago
it gives me bunch of errors
Moods
Moods7mo ago
1.) That is not Main and 2.) Make Data2 static
McCormick
McCormick7mo ago
still not shown
Moods
Moods7mo ago
That is not Main Where is your main method???? Like the method actually named Main Remove that from Data2 and put it in Main
McCormick
McCormick7mo ago
Why I should name my method as "Main"? I don't have any method which are named Main
Moods
Moods7mo ago
Yes you do
McCormick
McCormick7mo ago
oh wait you mean from weatherdata?
Moods
Moods7mo ago
Yeah that
McCormick
McCormick7mo ago
public static void Main(string[] args)
{
PrintWeatherData();

}
public static void Main(string[] args)
{
PrintWeatherData();

}
What about it
Moods
Moods7mo ago
Yes !! That is the method
McCormick
McCormick7mo ago
It doesn't conflicts
Moods
Moods7mo ago
Put it there
McCormick
McCormick7mo ago
oh
Moods
Moods7mo ago
What
McCormick
McCormick7mo ago
ohhhh Okay i got it
Moods
Moods7mo ago
Are you following a tutorial out of curiosity?
McCormick
McCormick7mo ago
what tutorial
Moods
Moods7mo ago
No I’m asking you if you’re following a tutorial
McCormick
McCormick7mo ago
No, i wrote that code by myself like i tried to test my skills Like we shouldn't sit and learn learn we should could and take experience etc I still need to put smth to there??
Moods
Moods7mo ago
Ohhh I see I see
Pobiega
Pobiega7mo ago
You need to understand the concept of the main method. It's the entry point to your code, where the computer starts to run it It must exist
Moods
Moods7mo ago
Oh yeah public ig
McCormick
McCormick7mo ago
I know that, but I just don't understand why it's not showing
Moods
Moods7mo ago
Because you didn’t call it In the main method That was why The method was never called
Pobiega
Pobiega7mo ago
A method not called does nothing at all, ever
McCormick
McCormick7mo ago
public _Data1.Cals.Data2();
public _Data1.Cals.Data2();
?
Pobiega
Pobiega7mo ago
If main, or a method called from main, don't call a method.... Then it's not part of the code execution flow This can go to any depth or be conditional etc, but it's still true
Moods
Moods7mo ago
nooope not there in where it was defined okay you should really follow a turorial but idk the tag
McCormick
McCormick7mo ago
it's already public
Moods
Moods7mo ago
it doesnt look public here
McCormick
McCormick7mo ago
oh you mean that one
public static void Data2(int a = 10, int b = 20)
public static void Data2(int a = 10, int b = 20)
? wait it static
Pobiega
Pobiega7mo ago
that seems fine now thats the "method signature" as we call it now you just need to call that method from your main method