Begginer problem with class in another file [duplicate] - c#

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();
}
}
}

Related

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.

Where to store information about point in console application?

I need to create a struct storing four points. I wanted to create a Point variable, but it looks I can't use System.Drawing in console application. What should I use?
you should add a reference to System.Drawing.dll and then add using System.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Point point = new Point();
}
}
}

What this error mean ? Inconsistent accessibility: field type 'DannyGeneral.OptionsFile' is less accessible than field 'AnimationEditor.Form1

Inconsistent accessibility: field type 'DannyGeneral.OptionsFile' is less accessible than field 'AnimationEditor.Form1.setting_file'
In Form1 i did:
public OptionsFile setting_file;
The error is on the setting_file part.
This is the beginning of the Options_File code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Configuration;
namespace DannyGeneral
{
class OptionsFile
{
string path_exe;
string temp_settings_file;
string temp_settings_dir;
string Options_File;
StreamWriter sw;
StreamReader sr;
public OptionsFile(string settings)
{
if (!File.Exists(settings))
{
if (!Directory.Exists(Path.GetDirectoryName(settings)))
{
Directory.CreateDirectory(Path.GetDirectoryName(settings));
}
File.Create(settings).Close();
}
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
Options_File = settings;
}
And In Form1 the top:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DannyGeneral;
using unfreez_wrapper;
namespace AnimationEditor
{
public partial class Form1 : Form
{
private static string settings_dir;
private static string settings_file;
public OptionsFile setting_file;
You need to have the type OptionsFile itself be public if you want to use it as a public property.
Restrictions on Using Accessibility Levels (C# Reference)
Either mark the class OptionsFile as public or mark the field setting_File as non-public (internal or private).
i had the same problem but i get it now for the people who don't get it it should be
namespace DannyGeneral
{
public class OptionsFile
{
string path_exe;
string temp_settings_file;
string temp_settings_dir;
string Options_File;
StreamWriter sw;
StreamReader sr;

Categories

Resources