C# syntax error, nothing seems wrong - c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace SchoolPasswordLockFolder
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the password: "); // the space character after the semicolon has an error
public string input = Console.ReadLine();
}
}
}
The errors:
Severity Code Description Project File Line
Error CS1513 } expected SchoolPasswordLockFolder c:\Users\CENSOREDSIJGIOFSGJIOFS\documents\visual studio 2015\Projects\App5\SchoolPasswordLockFolder\SchoolPasswordLockFolder\Program.cs 14
(for the one after the semicolon)
and
Severity Code Description Project File Line
Error CS1022 Type or namespace definition, or end-of-file expected SchoolPasswordLockFolder c:\Users\CENSOREDIDONTWANTSTALKERS\documents\visual studio 2015\Projects\App5\SchoolPasswordLockFolder\SchoolPasswordLockFolder\Program.cs 19
(for the last bracket)
I have not programmed in C# for a very long time as I was too busy with web development and lua...

change this:
public string input = Console.ReadLine();
to:
string input = Console.ReadLine();
local variables do not get accessibility modifiers like public.

Related

Visual Studio execution closes when pressing enter

Hello im trying to learn C# step by step. I installed Visual Studio to practice but 20 mins in I cant test my basic code when executing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Write("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("My name is " + name);
}
}
}
It is as basic as this but when I execute and type a name and press enter, the cmd just closes. any help would be appreciated because i am enthusiastic to start out with C#
cmd
The Main method returns after
Console.WriteLine("My name is " + name);
And this effectively terminates the app.
You should put a
Console.Read();
to wait until the next keystroke.
Add another input to close application, so cmd wont close until you press enter again.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Write("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("My name is " + name);
Console.ReadLine();
}
}
}
Try Ctrl + F5
If you don't need to debug, then Ctrl-F5 is the best option
works automatically without any Console.Readline() or ReadKey()
Visual Studio will keep the console window open, until you press a key.

C#, basic error

So I am making a super basic (I'm in my second week of learning C# so please excuse my ignorance) program that takes a string input from a user and outputs the string backwards. I have copied the book to a T in regards to a majority of it but I have noticed spelling errors in some of their code so I don't have a lot of faith in what they are showing. My compiler is giving me an error with WriteLine and ReadLine and I don't understand why as the book says it works. This is the error I am getting;
"WriteLine does not exist in the current context" same with "ReadLine"
My code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
static class funcStrings
{
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
class runProgram
{
class Program
{
static void Main()
{
string name;
WriteLine("Enter your name to be reversed ");
name = ReadLine();
Console.WriteLine(funcStrings.ReverseString(name));
}
}
}
}
Thanks for any guidance here
You just need to add Console. in front:
Console.WriteLine("Enter your name to be reversed ");
name = Console.ReadLine();
You can add such namespace:
using static System.Console;
This brings all the static members from the System.Console class into scope, so that you don't need to prefix them with Console. It is a C# 6 feature, and useful when accessing many members in a static class. See relevant documentation.
The problem is probably that it should be Console.WriteLine and Console.ReadLine.
Currently it is:
static void Main()
{
string name;
WriteLine("Enter your name to be reversed ");
name = ReadLine();
Console.WriteLine(funcStrings.ReverseString(name));
}
You need to add a Console.WriteLine("Enter your name to be reversed"):

the type or namespace component data is missing error in using MathWorks.MATLAB.NET.ComponentData; help me how to solve this error?

i am currently deploying matlab over .net i have included my matlab project dll and mwarray.dll of matlab run-time compiler successfully but when i tried in using component data i have 2 errors one is:
name space missing using MathWorks.MATLAB.NET.ComponentData;
second one is:
cannot convert type void MathWorks.MATLAB.NET.Arrays.Mwcellarray in
line cellout = (MWCellArray)obj.braille();
complete code is as mentioned below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CsharpMatlab;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.ComponentData;
namespace MATCsharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Code For Matlab to C#");
MWCellArray cellout = null;
CsharpMatlab.CellExampleApp obj = new CsharpMatlab.CellExampleApp();
cellout = (MWCellArray)obj.braille();
MWNumericArray item1 = (MWNumericArray)cellout[1];
MWNumericArray item2 = (MWNumericArray)cellout[2];
MWCharArray item3 = (MWCharArray)cellout[3];
Console.WriteLine("item1 is {0}", item1);
Console.WriteLine("item2 is {0}", item2);
Console.WriteLine("item3 is {0}", item3);
Console.ReadLine();
}emphasized text
}
}

How to print out the result of a SWI-Prolog query from C# using Swi-cs-pl library

I'm trying to understand how the Swi-cs-pl library works by doing my own little program, but I cannot printout any result from a query based on the example from SWI-Prolog web.
I have this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SbsSW.SwiPlCs;
namespace HolaMundo
{
class Program
{
static void Main(string[] args)
{
if (!PlEngine.IsInitialized)
{
String[] param = { "-q" }; // suppressing informational and banner messages
PlEngine.Initialize(param);
PlQuery.PlCall("assert(father(hader, nevar))");
PlQuery.PlCall("assert(father(hader, sergio))");
PlQuery.PlCall("assert(brother(nevar, sergio):- father(hader, nevar), father(hader, sergio))");
//How do I write out in a Console the answer for a query like:
// brother(nevar, sergio)
PlEngine.PlCleanup();
}
}
}
}
As I said on my code, I just want to query something basic like: brother(nevar, sergio). and get any answer from Console like true or whatever.
Can anyone help me?

Button click action FIND / REPLACE

I can't seem to get my button to work. This is the first time i've tried to make an application. I just need a simple find / replace. I found some code on the internet and can't seem to get it to work.
http://pastebin.com/9v6TEFMs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Deneuralyzer : Form
{
public Deneuralyzer()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
using System;
using System.IO;
using System.Text.RegularExpressions;
string filePath = #"C:\Program Files (x86)\location\to\application\textfile.txt";
string searchText = "Count,2,";
string replaceText = "Count,200,";
ReplaceInFile(filePath, searchText, replaceText);
static public void ReplaceInFile(string filePath, string searchText, string replaceText)
{
StreamReader reader = new StreamReader(filePath);
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, searchText, replaceText);
StreamWriter writer = new StreamWriter(filePath);
writer.Write(content);
writer.Close();
}
}
}
}
also, does something specific need to be done, so that the application can edit the file? because doing it by hand I must change the file permissions and ownership.
errors out when i run the test
Error 3 Type or namespace definition, or end-of-file expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 59 1 WindowsFormsApplication1
Error 4 Syntax error, '(' expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 27 19 WindowsFormsApplication1
Error 6 Syntax error, '(' expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 28 19 WindowsFormsApplication1
Error 8 Syntax error, '(' expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 29 19 WindowsFormsApplication1
Error 2 Expected class, delegate, enum, interface, or struct C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 54 17 WindowsFormsApplication1
Error 1 } expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 35 70 WindowsFormsApplication1
Error 5 ) expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 27 25 WindowsFormsApplication1
Error 7 ) expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 28 28 WindowsFormsApplication1
Error 9 ) expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 29 49 WindowsFormsApplication1
You should read up on how to structure a class, or perhaps a C# tutorial. Normally you have the following order of elements in a class
// using statements, other components you use in your class
using System;
// namespace name (a group so to speak)
namespace NamespaceName {
// class, this gets nested under a namespace
public class MyClass {
// private variables
private int myVariable;
// constructors
public MyClass() {
// this is where you create the instance, set variables and stuff
myVariable = 314;
}
// methods
public void DoSomething() {
++myVariable;
}
private void anotherMethod() { }
}
}
Now, when the compiler tries to parse your codefile, since it's not structured in this manner, it complains
When you try to build the project the Error list window pops up with the errors you specified. You can double click each of these items and address them. What you can do is after each fix try to compile again, since some errors might be "follow up errors", i.e. errors which are fixed due to the first error being fixed.
In your case you have a method in the button click method. This is not allowed for a C# class, so you need to close the buttonClick method scope (the { } brackets that is) and move the using statements to the top of the cs file.
A tip is to indent your code since well formatted code is easier to read. Visual Studio makes this easy, you can click the Edit menu, choose Advanced and click Format Document (remember the shortcut Ctrl-k Ctrl-d). It can also help spot some errors, such as unmatched brackets.
Edit: Another tip btw is the Right click - Organize usings - Remove and sort option, to clear the cludder in the using statements in the beginning of the file. In many cases you don't need half of the ones which are included by default when Visual Studio creates a file. Later, if you find that you have a unrecognized class, press Ctrl + . ("ctrl dot") and you get the option to include the using statement needed for that class.

Categories

Resources