Problem with class constructor inside the class library [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to create a class library with the class I shown. but I'm getting errors.
I thought that the constructor is called when I'm creating the object, why it already requires arguments?
I'm using Visual Studio 2019 and .NET Framework 4.8
If I add a comment on 15 line, the two first errors disappear
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSVDataViewer
{
class CSV
{
public List<string> FirstColumn { get; set; }
public List<string> SecondColumn { get; set; }
// Line 15​
public CSV(string path, char separator)
{
using (var reader = new StreamReader(path))
{
List<string> listA = new List<string>();
List<string> listB = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(separator);
​
listA.Add(values[0]);
listB.Add(values[1]);
}
}
}
}
}
CS1056 Unexpected character '' at line 15
CS1519 Invalid token '' in class, struct or interface member declaration at line 15
CS1056 Unexpected character '' at line 26

Related

c# - Insert predefined value inside an array inside a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
using System;
using System.IO;
using System.Security.Cryptography;
namespace Rextester{
public class Program{
public static void Main(string[] args){
String[,] name = {{"Juan", "Pérez"}, {"Fred", "Nurk"}, {"Marko", "Marković"}, {"Si", "Polan"}};
int place = 3;
String _name = (!String.IsNullOrEmpty(name[place,0])) ? $"Name: {name[{place},0]} {name[{place},1]}" : "Error";
Console.WriteLine(_name);
}
}
}
I try to use the integer place inside the String $"Name: {name[{place},0]} {name[{place},1]}" as identifier for the array name but the compiler give me these errors back:
Compilation error (line 9, col 74): Identifier expected
Compilation error (line 9, col 80): Syntax error, ',' expected
Compilation error (line 9, col 92): Identifier expected
Compilation error (line 9, col 98): Syntax error, ',' expected
Why the compiler would not compile these line? The identifier is given, so I don't understand the problem.
You already are inside curly brackets in the string, you don't need more curly brackets for each variable.
Not $"Name: {name[{place},0]} {name[{place},1]}"
But $"Name: {name[place,0]} {name[place,1]}"

Can't print Stack<T> C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi I have a question about my code. Everything should work without a problem, but when I try to print on the console, the stack gives me the following
input:
4
Output:
System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.
Stack1 [System.UInt64]
my question is if i need to add a new library because even with int it gives me the same.
using System;
using System.Collections.Generic;
namespace ConsoleApp29
{
class Program
{
static void Main(string[] args)
{
Stack<ulong> nm = new Stack<ulong>();
ulong p = ulong.Parse(Console.ReadLine());
for(ulong i = 0; i < p; i++)
{
nm.Push(i);
}
foreach(int i in nm)
{
Console.Write(nm);
}
}
}
}
nm, which you are printing, is the entire Stack object; you want to be printing i, the current element of nm.

EndsWith() not working properly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to add the corresponding index of all the strings in the tres4 array, if they match the end of the input string. Yet, my list gets filled with all the indexes 1-12, as opposed to only those, matching the end of my input string. Only 1 should be added to my List in this case.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Encoding
{
class Program
{
static void Main(string[] args)
{
string[] tres4 = {
"CHU",
"TEL",
"OFT",
"IVA",
"EMY",
"VNB",
"POQ",
"ERI",
"CAD",
"K-A",
"IIA",
"YLO",
"PLA"
};
string message = "CHUTEL";
List<int> digits = new List<int>();
for (int i = 0; i < tres4.Length; i++)
{
if (message.EndsWith(tres4[i]));
{
digits.Add(i);
}
}
Console.WriteLine(String.Join(", ", digits));
}
}
}
You have an extra semicolon:
if (message.EndsWith(tres4[i]));
See the semicolon at the end? Remove it and it'll work.
Next time you should give a debugger a try, it would show you the problem right away.
Remove ; from if condition
if (message.EndsWith(tres4[i]))

"A namespace cannot directly contain members such as fields or methods" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to use this code for NET.reflector using Reflexil. I am trying to replace code with this:
if(Input.GetKeyDown(KeyCode.Keypad5)) {
int i = 0;
Character localPlayer = PlayerClient.GetLocalPlayer().controllable.GetComponent<Character>();
foreach (UnityEngine.Object obj2 in UnityEngine.Object.FindObjectsOfType(typeof(LootableObject)))
{
if (obj2 != null)
{
i++;
LootableObject loot = (LootableObject) obj2;
Debug.Log("Loot "+i+": "+loot.transform.position.ToString());
CCMotor ccmotor = localPlayer.ccmotor;
if(ccmotor != null && tpPos1 != Vector3.zero) {
ccmotor.Teleport(loot.transform.position);
Notice.Popup("", "Teleported to "+loot.name, 1.5f);
}
break;
}
}
}
But it gives me an error when I try to compile:
Line: 1 Column: 1 Error Number: CS0116 Error Message: "A namespace does not directly contain members such as fields or methods"
This is Unity code I think. I am not that experienced. Could anyone fix this for me? Or tell me what to do? Thanks!
The snippet you're showing doesn't seem to be directly responsible for the error.
This is how you can CAUSE the error:
namespace MyNameSpace
{
int i; <-- THIS NEEDS TO BE INSIDE THE CLASS
class MyClass
{
...
}
}
If you don't immediately see what is "outside" the class, this may be due to misplaced or extra closing bracket(s) }.

Calculating unique words in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a page called post summary.
Under this page, I want to count the total number of words and total number of unique words.
I managed to count the total number of words in the post successfully.
However, I do not know how I can count the unique words.
Eg: "I enjoyed school today very much."
Expected output:
Total word count: 6
Unique word count: 5
Here is my current 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;
namespace empTRUST
{
public partial class PostSummary : Form
{
string target_fbid;
string fbStatus;
public PostSummary(string target_fbid, string fbStatus)
{
InitializeComponent();
this.target_fbid = target_fbid;
this.fbStatus = fbStatus;
}
private void PostSummary_Load(object sender, EventArgs e)
{
label_totalwordcount.Text = fbStatus.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).Length.ToString();
}
}
}
I don't understand your example since there are no repeating words in "I enjoyed school today very much". However, this is a naive approach which might work for you:
var allWords = text.Split();
int count = allWords.Length; // 6
int unqiueCount = allWords.Distinct().Count(); // 6
It is naive because punctuation characters modify the result. So you might want to replace them in the first step:
var allWords = text.ToUpperInvariant().Replace(".", "").Replace(",","").Split(); // ...
Also, the case modifies the result, so you could compare case-insensitively if that is desired.
Can use something like this:
"I enjoyed school school today very much.".Split(' ').Distinct()
This one returns 6, even if there is "school" word that appears 2 times.
EDIT
if you need some custom comparison logic (say case insensitive) you may use Distinct overload where you can specify custom equality comparer.
The first tought is:
public int GetUniqueWordsCount(string input)
{
return input.Split(' ').GroupBy(s => s).Count();
}
If you would like a case insensitive solution you could add .ToLower() or .ToUpper() conversion to your group key selector. Also you may implement your own IEqualityComparer if you want some custom comparsion logic.

Categories

Resources