I'm making Class that will read json data from files, but for some unknown reason, I keeep geting errors. I really hope it's not only stupid mistake on my side.
My Code: - Not working
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Blackjack
{
class LeaderBoard
{
LeaderBoard(string fileName)
{
public string filename = fileName;
if (!File.Exists(fileName))
{
File.Create(fileName);
}
private string contents = System.IO.File.ReadAllLines(fileName);
}
}
}
My Code - Working
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Blackjack
{
class LeaderBoard
{
LeaderBoard(string fileName)
{
if (!File.Exists(fileName))
{
File.Create(fileName);
}
}
}
}
Output
Build started...
1>------ Build started: Project: Blackjack, Configuration: Debug Any CPU ------
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(13,10,13,10): error CS1513: } expected
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(15,13,15,15): error CS1519: Invalid token 'if' in class, record, struct, or interface member declaration
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(15,17,15,18): error CS1031: Type expected
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(15,17,15,18): error CS8124: Tuple must contain at least two elements.
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(15,17,15,18): error CS1026: ) expected
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(15,17,15,18): error CS1519: Invalid token '!' in class, record, struct, or interface member declaration
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(15,29,15,30): error CS1519: Invalid token '(' in class, record, struct, or interface member declaration
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(15,38,15,39): error CS8124: Tuple must contain at least two elements.
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(15,39,15,40): error CS1519: Invalid token ')' in class, record, struct, or interface member declaration
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(17,28,17,29): error CS1519: Invalid token '(' in class, record, struct, or interface member declaration
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(17,37,17,38): error CS8124: Tuple must contain at least two elements.
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(17,38,17,39): error CS1519: Invalid token ';' in class, record, struct, or interface member declaration
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(21,5,21,6): error CS1022: Type or namespace definition, or end-of-file expected
1>C:\Users\ASUS\RiderProjects\Blackjack\Blackjack\Leaderboard.cs(22,1,22,2): error CS1022: Type or namespace definition, or end-of-file expected
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I really don't know what is wrong... where is that missing semicolon?
CodePudding user response:
You cannot use access modifiers inside a class member, they can only be applied to the class members themselves:
class LeaderBoard
{
public string filename;
private string[] contents;
LeaderBoard(string fileName)
{
filename = fileName;
if (!File.Exists(fileName))
{
File.Create(fileName);
}
contents = System.IO.File.ReadAllLines(fileName);
}
}
CodePudding user response:
Problem is that you are using public and private in the constructer of your class. Also you should define the contents as a string array.
namespace Blackjack
{
class LeaderBoard
{
LeaderBoard(string fileName)
{
string filename = fileName;
if (!File.Exists(fileName))
{
File.Create(fileName);
}
string[] contents = System.IO.File.ReadAllLines(fileName);
}
}
}
CodePudding user response:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Blackjack
{
class LeaderBoard
{
public string LeaderBoard(string fileName)
{
string fileContent;
if (!File.Exists(fileName))
{
File.Create(fileName);
}
if (new FileInfo(fileName).Length == 0) {
return;
}
using (var sr = new StreamReader(fileName)) {
fileContent = sr.ReadToEnd();
}
return fileContent;
}
}
}
Try this :)