Error with Enum.GetNames() - c#

When I try and compile this code,
I get this error.
Error 1 The type or namespace name 'transmission' could not be found (are you missing a using directive or an assembly reference?)
How will I be able to rectify this error?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public enum transmission
{
Manual,
NonSynchronous,
Automatic,
SemiAutomatic,
Continuously,
Infinitely,
Electric,
Hydrostatic,
Hydrodynamic,
}
}
public class Car
{
static void Main(string[] args)
{
string[] transmissions = Enum.GetNames(typeof(transmission));
foreach(String transmission in transmissions) {
Console.WriteLine(transmission);
}
}
}

You are outside the scope of ConsoleApplication1 namespace. Either include Car class to that namespace or call typeof(ConsoleApplication1.transmission).

Related

Begginer problem with class in another file [duplicate]

This question already has an answer here:
'Class.function' doesn't exist in current context while using a self made NuGet Package
(1 answer)
Closed 5 months ago.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
}
}
}
And another class in another file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rock_Paper_Scissors
{
internal class ComputerGenerateSign
{
public string computer;
Console.ReadKey()
}
}
Why Visual Studio code have a problem ?>"Error IDE1007 The name 'Console.ReadKey' does not exist in the current context"
Use 'Console.ReadKey();' in the body of a method, That way it will recognize. You didn't write a method inside the 'ComputerGenerateSign' class like the class 'program' has the main method. hope this helps.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rock_Paper_Scissors
{
public class ComputerGenerateSign
{
public void method1()
{
public string computer;
Console.ReadKey();
}
}
}
You have made function call in class which was not allowed. In class you can define properties, fields and functions.
You need to call that function inside a function body.
Main.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Rock_Paper_Scissors;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
ComputerGenerateSign obj=new ComputerGenerateSign();
obj.WaitForUserInput();
}
}
}
And another class in another file
using System;
using System.Text;
namespace Rock_Paper_Scissors
{
public class ComputerGenerateSign
{
public string computer;
public void WaitForUserInput()
{
Console.ReadKey();
}
}
}

Error CS0017 "Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point." problem

I opened a new project right now, and I opened a new class there with a "Main" inside it.
I already read whole over the internet about it and I already got that it happens because I have more than one "Main" method, but I read that when you choose inside the - Properties --> Startup object - your "Main" that you want to open it should be fixed.
The problem is that it doesn't show them at all.
What am I suppose to do?
That's the Error:
CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
My classes:
First -
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeginnerProject
{
internal class Program
{
static void Main(string[] args)
{
}
}
}
Second -
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeginnerProject
{
internal class FakeCoin
{
static void Main(string[] args)
{
Console.WriteLine("Hi");
}
}
}
Third -
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeginnerProject
{
internal class WhyLikeThat
{
static void Main(string[] args)
{
}
}
}
Properties:
https://i.stack.imgur.com/5wkYV.png
As described in the official documention, you just need to specify your entry point in your .csproj by using the <StartupObject> or the <MainEntryPoint> tag.
In your case you should be writting something like <StartupObject>BeginnerProject.FakeCoin</StartupObject> if you want the second class to be your entry point.

missing using directive assembly reference error in c# vscode

When I try to compile a simple application I made in VSCode with 2 .cs files one with a class, and the other with the main method, I get an error: cannot find type or namespace "MyClass". I do not understand why this happens, as I have put both the main method and class declaration in the same namespace.
MyClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes_test.src
{
public class MyClass
{
public void testFunc() { Console.WriteLine("Hello World!"); }
}
}```
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes_test
{
class Program
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.testFunc();
Console.WriteLine("Hello World!");
}
}
}
I want the console to print Hello World!, but instead I get this error.
classes_test.src
and
classes_test
are not the same Namespace.
In order to set your class visible to the main method, you have to use the following using statement.
using classes_test.src;
This is a quite common mistake, especially for those who are c#-newbie.
Including a namespace, DOES NOT include the subnamespaces

Why i'm getting Inconsistent accessibility: field type .... is less accessible than field ? And how to fix it?

Error 1 Inconsistent accessibility: field type 'Youtube_Manager.Ffmpeg' is less accessible than field 'Youtube_Manager.ScreenShot.fmpeg'
In a class top i added:
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
namespace Youtube_Manager
{
public class ScreenShot
{
#region Global Variables
public static Ffmpeg fmpeg;
Then i'm calling to use fmpeg in another class in a timer click event:
ScreenShot.fmpeg.Close();
And i'm getting in the class ScreenShot on the fmpeg the error:
Error 1 Inconsistent accessibility: field type 'Youtube_Manager.Ffmpeg' is less accessible than field 'Youtube_Manager.ScreenShot.fmpeg'
And this is the top of the class Ffmpeg:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using DannyGeneral;
namespace Youtube_Manager
{
class Ffmpeg
{
NamedPipeServerStream p;
String pipename = "mytestpipe";
System.Diagnostics.Process process;
string ffmpegFileName = "ffmpeg.exe";
string workingDirectory;
public Ffmpeg()
{
Even if i change the variable fmpeg from static to public i'm getting on it the same error.
Top-level C# classes are internal by default. Internal has a lower visibility than public. Use public class Ffmpeg to make the class public.

How to stop ReSharper from removing unused Using statements when moving/updating Namespace declarations?

When using ReSharper to move/update namespace declarations, is there a way to stop ReSharper from removing unused Using statements?
In other words, if I have a class such as:
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
namespace Foo.Bar
{
class MyClass
{
List<string> Names { get; set; }
}
}
And I want to move it into the Foo.Bar.Utilities namespace using ReSharper, Resharper will remove all the unused Using statements and leave me with:
using System.Collections.Generic;
namespace Foo.Bar.Utilities
{
class MyClass
{
List<string> Names { get; set; }
}
}
However, I do not want ReSharper to touch my Using statements while moving the namespace declaration. I'd prefer to have the result as:
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
namespace Foo.Bar.Utilities
{
class MyClass
{
List<string> Names { get; set; }
}
}
I don't think you can do this unequivocally.
However, there is an option to specify namespaces that should not be removed (Resharper Options -> C# -> Namespace Imports), but you need to know which ones you don't want it to remove.

Categories

Resources