Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code is now throwing me errors... I am really new to Visual Studio and C# the errors I keep getting are under the = invalid token '=' in class struct, or interface member declaration
"|" gives me the error type expected Tuple must contain at least two elements ) expected
Type expected
Tuple must contain at lease two elements. ) expected
invalid token @"C:\Users\Documents\test_shared_folder\EDITest3.txt" in class, struct, or interface member declaration.

what do these errors mean??

What I have tried:

using System.IO;

namespace text
{
	internal class Replace
	{
		string text = File.ReadAllText(@"C:\Users\OneDrive\Desktop\EDI_Examples_855_Orders\ftpedi.edi855_o_1397.EDI.16701");
		text = text.Replace("|","*");
File.WriteAllText(@"C:\Users\Documents\test_shared_folder\EDITest3.txt", text);
	}
}
Posted
Updated 20-Aug-18 9:22am

1 solution

You need to spend a bit more time learning the syntax of C#, specifically about methods:

Methods (C# Programming Guide) | Microsoft Docs[^]

At the class level, you are really only allowed to declare things. There is a bit of a cheat here, because you can provide initializations for the things you declare. So, to some extent, you can also do things (in a limited fashion).

Generally, if you want to do things they should be inside of a method. So, for example, the following would allow you to compile your code. Though, you'd need to add an invocation of the DoSomeStuff method elsewhere.

using System.IO;

namespace text
{
  internal class Replace
  {
    string text = File.ReadAllText(@"C:\Users\OneDrive\Desktop\EDI_Examples_855_Orders\ftpedi.edi855_o_1397.EDI.16701");

    internal void DoSomeStuff()
    {
      text = text.Replace("|", "*");
      File.WriteAllText(@"C:\Users\Documents\test_shared_folder\EDITest3.txt", text);
    }
  }
}


This leaves the obvious chicken-and-the-egg question. If I need to do things in a method, and invoke the method, how does it all get started. That depends on the type of application. For console applications, this usually occurs in the Main method. For other applications, its usually in a method that is handling some external event and is called by the .NET framework when the event occurs.
 
Share this answer
 
v3
Comments
Member 13935962 22-Aug-18 10:52am    
So if you could dumb this down a little for me... the only experience I have with Visual Studio and C# are the tutorials and classes I have taken online... I have seen the main method but I am not sure where to put it... when I paste in the code above I get the error

Error CS5001 Program does not contain a static 'Main' method suitable for an entry point ConsoleApp23 c:\users\source\repos\ConsoleApp23\ConsoleApp23\CSC 1 Active

In this project I have a folder called text in that folder is the replace.cs

and I also have the Program.cs that was created when I started the project.

The code i pasted above is on the replace.cs page...

So I am really confused as where to build things

thank you
Eric Lynch 22-Aug-18 16:24pm    
You might want to start over. When you first create a console application project, it contains a Program.cs file, which contains a Program class and a static Main method. The Main method is automatically called, by the operating system, when you execute the program. By the error you mention, it sounds like you have somehow deleted (or illegally altered) the Program.cs file.

Traditionally, when folks start programming, they start by writing a "Hello World" application. I'd make sure you fully understand that before moving on.

See: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/hello-world-your-first-program

Member 13935962 23-Aug-18 15:30pm    
so I have started over as you suggested and I am not getting errors but the code isn't adding a new file in my shared folders. I have the Program.cs file with the following code:
using System;
using System.IO;

namespace ConsoleApp23
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

and then i have a text folder in that folder is a Replace.cs file with the following code:
using System.IO;

namespace text
{
internal class Replace
{
string text = File.ReadAllText(@"C:\Users\OneDrive\Desktop\EDI_Examples_855_Orders\ftpedi.EDI01-00722615.355");

internal void DoSomeStuff()
{
text = text.Replace("*", "+");
File.WriteAllText(@"C:\Users\Documents\test_shared_folder\EDITest3.txt", text);
}
}
Eric Lynch 23-Aug-18 16:38pm    
You seem to want a short cut to learning C#. None exist. You have to read a lot more and learn to understand. At a minimum, you need to understand the large building blocks. Things like: What are classes? What are methods? How are methods used?

In this case, you have a class Replace and a method DoSomeStuff that are simply never used. The syntax is fine, so they'll compile fine and you'll get no errors.

So, ask yourself some questions. Why does my program output "Hello World!"? Who creates an instance of the class Program? Who invokes the Main method in the Program class? The answers are simple. When you create a console application, by default, the operating system will create an instance of the Program class and invoke the method named Main. This is all explained in the "Hello World" link I shared.

Now ask yourself the same questions about the Replace class and the DoSomeStuff method. Who creates an instance of the class Replace? Who invokes the DoSomeStuff method in the Replace class? The answer are as follows. Nothing creates the Replace class...you include no code to do that. Nothing invokes the DoSomeStuff method...you include no code to do that.

As discussed, console applications have some default behaviors for the Program class and its Main method. That's it...nothing else. They don't have default behaviors for other classes or other methods. Those classes and methods sit there and do nothing unless you provide code to actually use them.

This all goes back to basics. A class is basically a description of something: a car, a building, a piece of belly lint, or something else. By itself, its only that: a description. Until you actually create a car, or a building, or (yuck) belly lint, you have a description that sits there and does nothing. When you create a class, its called instantation. In C#, you create a class instance with the "new" keyword.

For example, within a method (like Main), you could add the following:

Replace replacer = new Replace();

A shorter syntax is as follows:

var replacer = new Replace();

Once you've created a instance of the Replace class, you are free to refer to any of its accessible constants, fields, properties, or methods. For example, if you wanted to invoke its DoSomeStuff method, the syntax would be:

replacer.DoSomeStuff();

Please go back and read a lot more about C# syntax. There are many excellent books on the topic. Also, the following can provide a good starting point, with links to many relevant topics.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/

We all start out somewhere and a lot of people on this site, myself included, are willing to give you some help, when you get stuck. It seems like you have some good intuition for syntax. My recommendation would be to read a bit more and then play with a some of the code samples that are available online, until you REALLY understand how they're put together and work. There are a bunch of them out there.

Trying to learn the basics from Q&A isn't very effective.

Hope this helps. Best of luck.
Member 13935962 29-Aug-18 14:10pm    
thank you for your comments! I have been trying to wrap my head around C# and visual studio... Thank you for the links to help me learn more.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900