Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am getting this error:
"The variable 'a' is assigned but its value is never available"
namespace test
{
public class Program
{
static void Main(string[] args)
{
test();
Console.WriteLine(a);
Console.ReadLine();
}
public static void test()
{
int a = 11;
}
}
}
You are talking about "methods" not classes.
You can read more here https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods
But in the mean time, try this:
namespace test
{
public class Program
{
static void Main(string[] args)
{
int a = test();
Console.WriteLine(a);
Console.ReadLine();
}
public static int test()
{
int a = 11;
return a;
}
}
}
class Program
{
static void Main(string[] args)
{
int a = test();
Console.WriteLine(a);
Console.ReadLine();
}
public static int test()
{
int a = 11;
return a;
}
}
You should probably get this error from your code. "The name 'a' does not exist in the current context"
As you do not get the variable "a" out from your method and try to access it.
Try the above and see if it helps.
This is a class and a method, what you have there. NOT 2 classes.
What do you want to do exactly?
The a variable does not exist where you want to print it.
Something like this?
static void Main(string[] args)
{
test();
Console.ReadLine();
}
public static void test()
{
int a = 11;
Console.WriteLine(a);
}
Related
This question already has answers here:
"does not contain a static 'main' method suitable for an entry point"
(9 answers)
Closed 8 days ago.
why i can't run my program it's says
CS5001: exe does not contain a static 'Main' method suitable for an entry point
This is my Code in C#:
using System;
class Human
{
private int _distance = 0;
private int _wordsSpoken = 0;
public void Walk()
{
Console.WriteLine("Walking");
_distance++;
}
public void Speak()
{
string[] phrases = {
"How are you?",
"It is a lovely day. Isn't it?",
"It's a pleasure to meet you.",
"Hello.",
"A pleasant morning to you."
};
Random rand = new Random();
int index = rand.Next(phrases.Length);
Console.WriteLine(phrases[index]);
_wordsSpoken++;
}
public void Hop()
{
Console.WriteLine("Hop");
_distance += 2;
}
public int Distance
{
get { return _distance; }
}
public int WordsSpoken
{
get { return _wordsSpoken; }
}
}
You need to include this in your code:
static void Main(string[] args)
{
// call your first method or whatever you need
}
This method is the starting point for your program and it is required. You would usually put this at the top of your code.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to write a simple program in C# to display the sum and the average of the numbers 1 to 100. the output should look like...
the sum is 5050
the average is 50.5
I cant seem to get it to work properly though, the return value only returns the sum not the wording before it, I have tried other ways for it to display the message and sum and ave but they arent working. I am trying to do this using the model view view controller method, but cant understand where I am going wrong and how to get it to display the above result. my code is below.
class SumAndAverage
{
public float sum = 0;
public float ave = 0;
public float SumAndAve()
{
for (int i = 1; i <= 100; i++)
{
sum = sum + i;
ave = sum / i;
}
return sum + ave;
}
}
}
class SumAndAverageController
{
IView view;
SumAndAverage sumAndAverage;
public SumAndAverageController(IView theView, SumAndAverage theSumAndAverage){
view = theView;
sumAndAverage = theSumAndAverage;
}
public void Go()
{
view.Start();
//mAndAverage.SetNumber(view.GetString("Please enter a number"));
view.Show(sumAndAverage.SumAndAve());
//view.Show(sumAndAverage.Result());
view.Stop();
}
}
class ConsoleView : IView
{
public void Start()
{
Console.Clear();
}
public void Stop()
{
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
public void Show<T>(T message)
{
Console.WriteLine(message);
}
}
interface IView
{
void Start();
void Stop();
void Show<T>(T message);
}
class Program
{
static void Main(string[] args)
{
new SumAndAverageController(new ConsoleView(), new SumAndAverage()).Go();
}
}
using System;
using System.Linq;
namespace SumAndAverage
{
class Program
{
static void Main(string[] args)
{
var data = Enumerable.Range(1, 100);
Console.WriteLine("the sum is " + data.Sum());
Console.WriteLine("the average is " + data.Average());
}
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I know there is some posts on that but I didn't find the answer I'm looking for, and its simple.
I know calling a method static or not (instanciation (OOP)).
Example, using static:
using System;
namespace Projet_Test
{
class surfacesCalcul
{
public static int calc_air(int a, int b){
int result = a * b;
return result;
}
}
/*
* ##########################
* ##########################
*/
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine(surfacesCalcul.calc_air(4,5));
}
}
}
You call the class(surfacesCalcul).Method and you can access the Method.
--
Normal, using OOP:
using System;
namespace Projet_Test
{
class surfacesCalcul
{
public int calc_air(int a, int b){
int result = a * b;
return result;
}
}
/*
* ##########################
* ##########################
*/
class MainClass
{
public static void Main (string[] args)
{
surfacesCalcul calculMeThis = new surfacesCalcul();
Console.WriteLine(calculMeThis.calc_air(4,5));
}
}
}
You create an instantiation and then you can access the methods in the class.
--
[My Question]
Which one is the best, I'm just wondering why using static method more than from an instance or the inverse. Is there a real reason or both are good. Is there a best time to use one more than the other?
I'll argue both sides and you can choose.
Consider your first example with a static function:
namespace Projet_Test
{
class surfacesCalcul
{
public static int calc_air(int a, int b){
int result = a * b;
return result;
}
}
/*
* ##########################
* ##########################
*/
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine(surfacesCalcul.calc_air(4,5));
}
}
}
This is fine if you never care about overriding or any kind of inheritance.
Now consider this example:
class surfacesCalcul
{
public virtual int calc_air(int a, int b){
int result = a * b;
return result;
}
}
class surfacesCalcul2 : surfacesCalcul {
public override int calc_air(int a, int b)
{
var g = base.calc_air(a, b);
return g*2;
}
}
/*
* ##########################
* ##########################
*/
class MainClass
{
public static void Main (string[] args)
{
var calculMeThis = new surfacesCalcul2();
Console.WriteLine(calculMeThis.calc_air(4,5));
}
}
You're not changing the "correctness" of the program at all just allowing polymorphism to do its thing.
This example it really to trivial for it to really matter. I would prefer the route with the class since static methods can't have instance variables and you can't do any kind of dependency injection, which makes unit testing so much easier. The object instantiation also allows for different instances to contain different state information where static can't contain any instance specific data.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to develop a class library in C# whose methods I would call in a hierarchical way, like this:
using MyProduct.Common;
protected void Page_Load(object sender, EventArgs e)
{
Utils myUtils = new Utils();
int res = myUtils.Numbers.Add(1, 2);
string text = myUtils.Text.ToUpper("test"); //***
}
Is it possible to do, using Namespaces, classes, interfaces, whatever?
UPDATE:
I have my library like this, but it doesn't work, as I can't "reference a type through an expression" (line with *** above)
namespace MyProduct.Common
{
public class Utils
{
public static class Text
{
public static string ToUpper(string s)
{
return s.ToUpper();
}
}
}
}
UPDATE 2:
Ok, it seems I need to clarify...
I have this MyProduct.Common.dll:
namespace MyProduct.Common
{
public static class Utils
{
public static class Text
{
public static void ToUpper(string s)
{
return s.ToUpper();
}
}
public static class Number
{
public static void Add(int a, int b)
{
return (a + b);
}
}
}
}
and I have my project:
using MAD.Comum;
...
protected void Page_Load(object sender, EventArgs e)
{
string x = Utils.Text.ToUpper("aa"); //this works
string res = Utils.Number.Add(1, 2); //this works
}
and now 2 questions:
- is it possible to separate classes Number and Text in 2 files?
- is this a good design?
Of course, you can do something like this:
namespace MyProduct.Common.Utils
{
public static class Numbers
{
public static int Add(int n, int m)
{
return n + m;
}
}
public static class Text
{
public static string ToUpper(string str)
{
return str.ToUpper();
}
}
}
And you use it like this:
MyProduct.Common.Utils.Numbers.Add(1, 2);
Hope this helps.
Cheers
This is a Console Program
Scenario:
I have a many function in my c# code and is set to be Public and Private. And I have static int Main(string args[]) of course. In int Main i calling some function Public and Private and thats interupting the function when i debug that. It says if main was static so all function must be static, yet my code in function Public and Private must be have "that" function. If i change static int Main with my public int main, debug cant be done because int Main must be static.
First im saying i really sorry for giving a non-understable question
If that's so confusing understanding my scenario, see my Example Code Here
namespace Example
{
class Program
{
public class ClassInPublic
{
public int Num;
public string Sentence;
}
ClassInPublic DeclaringInstance = new ClassInPublic();
private void Foo()
{
DeclaringInstance.Num = 35;
DeclaringInstance.Sentence = "Hello World";
Console.Write("This Private Function! And Assign Variable Value From ClassInPublic Variable");
}
private void Foo2()
{
Console.Write("This Single Foo didnt have any Instance Class");
}
static int Main(string args[])
{
Foo();
}
}
}
That's my case
And with help from others i already build this
namespace Example
{
class Program
{
public static class ClassInPublic
{
public int Num;
public string Sentence;
}
ClassInPublic DeclaringInstance = new ClassInPublic();
private static void Foo()
{
DeclaringInstance.Num = 35;
DeclaringInstance.Sentence = "Hello World";
Console.Write("This Private Function! And Assign Variable Value From ClassInPublic Variable");
}
private static void Foo2()
{
Console.Write("This Single Foo didnt have any Instance Class");
}
public static int Main(string args[])
{
Foo();
}
}
}
Error sign is lower right now, the rest is the ClassInPublic
It says i cannot declare
cannot declare instance members in static class
May someone assist me to overcome this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Example
{
class Program
{
public void foo()
{
Console.WriteLine("This is foo in Class Program");
}
static void fooStatic()
{
Console.WriteLine("This is Static foo");
}
static void Main(string[] args)
{
Program fooInstance = new Program();
fooStatic();
fooInstance.foo();
}
}
}
Here is a small example. Static functions are one per class. When you make public or private function they exist for each instance.
Edit:
Here is what I think you are trying to do:
namespace Example
{
class Program
{
public class ClassInPublic
{
static public int Num;
static public string Sentence;
}
ClassInPublic DeclaringInstance = new ClassInPublic();
private static void Foo()
{
ClassInPublic.Num = 35;
ClassInPublic.Sentence = "Hello World";
Console.WriteLine("This Private Function! And Assign Variable Value From ClassInPublic Variable");
}
private static void Foo2()
{
Console.Write("This Single Foo didnt have any Instance Class\n");
}
public static int Main(string[] args)
{
Foo();
return 0;
}
}
}