C
C#12mo 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
Moods12mo ago
Well You didn’t call the method
McCormick
McCormickOP12mo ago
Console.WriteLine($"The result of h: {h}"); I did
Moods
Moods12mo ago
No I mean Data2 You didn’t call that
McCormick
McCormickOP12mo ago
PrintWeatherData? It works, why?
Moods
Moods12mo 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
McCormickOP12mo ago
hm
Moods
Moods12mo ago
In the Main method, do _Data1.Cals.Data2() Like just put that there
McCormick
McCormickOP12mo ago
Where we name the method?
Moods
Moods12mo ago
No inside your Main method It’s where the program starts executing
McCormick
McCormickOP12mo ago
it gives me bunch of errors
Moods
Moods12mo ago
1.) That is not Main and 2.) Make Data2 static
McCormick
McCormickOP12mo ago
still not shown
Moods
Moods12mo 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
McCormickOP12mo ago
Why I should name my method as "Main"? I don't have any method which are named Main
Moods
Moods12mo ago
Yes you do
McCormick
McCormickOP12mo ago
oh wait you mean from weatherdata?
Moods
Moods12mo ago
Yeah that
McCormick
McCormickOP12mo ago
public static void Main(string[] args)
{
PrintWeatherData();

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

}
What about it
Moods
Moods12mo ago
Yes !! That is the method
McCormick
McCormickOP12mo ago
It doesn't conflicts
Moods
Moods12mo ago
Put it there
McCormick
McCormickOP12mo ago
oh
Moods
Moods12mo ago
What
McCormick
McCormickOP12mo ago
ohhhh Okay i got it
Moods
Moods12mo ago
Are you following a tutorial out of curiosity?
McCormick
McCormickOP12mo ago
what tutorial
Moods
Moods12mo ago
No I’m asking you if you’re following a tutorial
McCormick
McCormickOP12mo 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
Moods12mo ago
Ohhh I see I see
Pobiega
Pobiega12mo 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
Moods12mo ago
Oh yeah public ig
McCormick
McCormickOP12mo ago
I know that, but I just don't understand why it's not showing
Moods
Moods12mo ago
Because you didn’t call it In the main method That was why The method was never called
Pobiega
Pobiega12mo ago
A method not called does nothing at all, ever
McCormick
McCormickOP12mo ago
public _Data1.Cals.Data2();
public _Data1.Cals.Data2();
?
Pobiega
Pobiega12mo 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
Moods12mo ago
nooope not there in where it was defined okay you should really follow a turorial but idk the tag
McCormick
McCormickOP12mo ago
it's already public
Moods
Moods12mo ago
it doesnt look public here
McCormick
McCormickOP12mo 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
Pobiega12mo 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
Want results from more Discord servers?
Add your server