|
I think I know what you need, try the following code. I did not test it so you might need to change it to do exactly as you require -
private IDictionary<int, int> GetResultFromTextFile(IEnumerable<int> src)
{
var filePath = @"C:\BoaSorte\Banco\testeResultado.txt";
var delimiter = new[] { ' ' };
var dict = new Dictionary<int, int>();
foreach (var line in File.ReadLines(filePath).Where(line => !string.IsNullOrEmpty(line)))
{
var values = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries)
.Select(x => int.Parse(x));
var matchCount = values.Count(v => src.Contains(v));
if (matchCount <= 15)
{
if (!dict.ContainsKey(matchCount))
{
dict[matchCount] = 0;
}
dict[matchCount]++;
}
}
return dict;
}
private void button1_Click(object sender, EventArgs e)
{
int outVal;
if (UltimoResultado.Any(Acertos => !int.TryParse(Acertos.Text, out outVal)))
{
MessageBox.Show("Valores Invalidos...");
return;
}
var arr = UltimoResultado.Select(linha => int.Parse(linha.Text));
var result = GetResultFromTextFile(arr).ToList();
for (int i = 0; i < result.Count; i++)
{
dgHits.Rows.Add();
dgHits.Rows[i].Cells["Acertos"].Value = result[i].Value.ToString();
}
}
|
|
|
|
|
Andre Oosthuizen wrote:
if (!dict.ContainsKey(matchCount))
{
dict[matchCount] = 0;
}
dict[matchCount]++; That checks whether the dictionary contains the key; sets the value if it doesn't; gets the value; then sets the value again.
It would be more efficient to use:
dict.TryGetValue(matchCount, out int existing);
dict[matchCount] = existing + 1; If the dictionary doesn't contain the key, the existing variable will be set to 0 .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
André Oosthuizen, thank you very much for your attention, I will try to explain myself better,
I need to show how many hits are in each line, comparing with the numbers generated in the array,
EX: line 1 --- 10 hits,
line2 --- 8 hits,
row 3 --- 10 hits,
line 4 --- 11 hits,
line 5 --- 11 hits,
row 6 --- 7 hits.
the code I have, shows me the total lines with 10 hits,
the total lines with 11 hits,
the total rows with 7 hits, Just exemplifying
*that's not what I need.
what i need each line to have the number of hits at the end of the line itself.
I'll leave an image to try to understand.
Exemple image
modified 10-Aug-23 17:45pm.
|
|
|
|
|
I solved my problem as follows
string[] lines = File.ReadAllLines(@"C:\BoaSorte\Banco\testeResultado.txt");
string[] line, contents, result;
int count;
result = textBox1.Text.Split();
for (int i = 0; i < lines.Length; i++)
{
contents = lines[i].Split(' ');
count = contents.Intersect(result).Count();
line = new string[16];
for (int j = 0; j < 15; j++)
line[j] = contents[j];
line[15] = count.ToString();
dgHits.Rows.Add(line);
}
Thanks for the help of André Oosthuizen and Richard Deeming
|
|
|
|
|
Only a pleasure, glad you found the solution!
|
|
|
|
|
I've been tasked with reading & writing file to/from sharepoint. I've never even seen Sharepoint before.
I have a Windows app that will load files from Sharepoint, run some processing, and write the results back out to Sharepoint. They have made me an admin on it so I can set things up.
I've been Googling, but all I get are snippets with little context.
I'm going to ask some stupid questions...
- How do I setup the location(s) I need?
- Are there actual folders I can read/write from? How do I reference them? Like a drive letter? Or do I need an API?
- Where do I go to get sharted doing this with C#?
Thanks
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
|
|
|
|
|
I did some searching and I trust the following is what you might need going forward -
1) To set up your SharePoint Site, have a look at - MS Support | Create a site in SharePoint[^]
2) SharePoint uses different authentication methods, often involving OAuth tokens or username/password credentials. You'll need to confirm the authentication mechanism that your company uses. Common libraries for handling authentication include Microsoft's MSAL (Microsoft Authentication Library) or ADAL (Active Directory Authentication Library) for OAuth-based authentication. You will find more at - MS Support | With a Mismatch Error | Authentication settings in Central Administration do not match the configuration in web.config[^]
MS Learn | Overview of the Microsoft Authentication Library (MSAL)[^]
MS Azure | Active Directory Authentication Library (ADAL)[^]
3) SharePoint has REST APIs that allow you to interact with it programmatically. Alternatively, there are client libraries provided by Microsoft that can simplify the process of interacting with SharePoint using C#. The Microsoft Graph API is a newer unified API that can also be used to work with SharePoint. See more at - Lists vs. Libraries in SharePoint Online[^]
4) In SharePoint, you have libraries that act like folders. Documents are stored within these libraries.
You can access files using URLs like 'https://your_site.sharepoint.com/sites/your_site_name/your-library-name/your-file-path'.
With APIs, you'll use HTTP requests (GET, PUT, POST, DELETE) to interact with SharePoint. For C#, you can use libraries like HttpClient for making these requests. See more at - MS Support | Work with files in a document library[^]
As a high level example of how you might use 'HttpClient' to read a file from SharePoint using C# -
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string url = "https://<your-domain>.sharepoint.com/sites/<site-name>/<library-name>/<file-path>";
string accessToken = "<your-access-token>";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
else
{
Console.WriteLine("Error: " + response.StatusCode);
}
}
}
}
Another example can be found at - How to read and write a text file from sharepoint[^]
|
|
|
|
|
That's awesome. Thank you
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
|
|
|
|
|
You're welcome. If you think it it helps you out, please give a like, thanks Kevin.
|
|
|
|
|
These changes make me wonder if using an abstract class doesn't already offer these facilities. Would you use them ? Don't these extensions contradict the "classic" model ? Your view ?Quote: 1. Default Implementations: As mentioned earlier, interfaces can now have default implementations of methods. This allows you to add new methods to your interfaces without breaking existing implementations.
2. Static and Instance Members: Interfaces can now contain static methods, instance methods, and operators.
3. Private and Protected Members: Interfaces can now contain private and protected members. This can be useful for providing helper methods used by the default implementations.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
modified 6-Aug-23 10:09am.
|
|
|
|
|
These features may make sense because in a sense, they now support multiple inheritance in .NET.
If interfaces can have methods and instance members, and you can derive from multiple interfaces, then that would be a pretty close approximation of multiple inheritance.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: a pretty close approximation of multiple inheritance. in a way abstract classes do not ?
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
There must be ways to use abstract classes that I never thought of.
I never saw it as a multiple-inheritance mechanism. But I am a simple-minded soul.
|
|
|
|
|
You can't multi-inherit abstract classes, but in all versions you can inherit from a base class and multiple Interfaces.
So adding default methods to Interfaces brings them a lot closer to a class and a fair impersonation of multiple class inheritance - remember that the only real difference between a "traditional" Interface and an abstract class is that the interface contains no code but enforces what you must implement, while the class provides methods and requires implementation.
It's useful, but ... just like var and dynamic it's wide open to abuse, and going to give problems when you inherit "lazy coder" apps.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Sounds like one is "attaching" new behaviour versus simply inheriting it, or promising it. Component-based architecture.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
imho, this "new" Interface is a long way from Fowler and Brooch's classic concept, and "programming to the interface" philosophy.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Perhaps, but I do find it to be a convenient place to put methods that can be implemented entirely in terms of the rest of the interface.
|
|
|
|
|
BillWoodruff wrote: an abstract class
That wouldn't work for struct s, since they can't have a base class (other than ValueType /Object ).
IIRC, the primary use-case for static members was to allow mathematical operations on generic types. And the vast majority of types you're going to want to use that on will be structs.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
i'm afraid i missed how 'structs came up, here.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Generic math - .NET | Microsoft Learn[^]
The majority of types you'd want to perform mathematical operations on are struct s. And you can't have an abstract base class for a struct.
Also, as far as static members go, an abstract base class still wouldn't help; you can't "override" a static member from your base class.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Sometimes using an interface makes more sense than using abstract base classes (the good old "I am" or "I can" discussion). And some of these interfaces where of course defined in nuget packages.
This means to add a method or property, you would have to either rename the interface and support both for a while, or you would force anyone who use your nuget package to reimplement the interface. Doable when users only have only a few nuget packages, and the runtime will guard you against a newer version of a package than you expect.
However with the way a lot of software is structured today it does not work anymore. Projects use a lot of nuget packages, and they have internal dependencies meaning updating one nuget package could lead to a chain reaction of having to update others... and sometimes there just was not a way of getting all of them compatible. Basically DLL hell was replaced... by NuGet hell.
Allowing interfaces to add methods/properties and remain backwards compatible makes it easier to write code that will survive when running with newer version of one or another 4th level dependent nuget package than it was written against.
|
|
|
|
|
I am following the online C# tutorials here: create class and objects.
I cannot get the code to envoke the class to create an object. I have some Python knowledge but am learning C#. I am using VS 2022 community edition and running on Windows 10.
I will post the code below but what happens is the Console.WriteLine("Hello, World!"); executes but not the attempt to envoke the class.
I am guessing the code should work like this:
1. C# executable looks for
static void Main(string[] args)
2. Within this main module is the statement:
Person person = new Person();
This line should create an object person.
3. The following lines should assign values to the person object's attributes
person.Name = "Daniel";
person.Age = 28;
person.Haspet = true;
4. The line :
person.Greeting();
should cause the class to be called:
public void Greeting()
{
Console.WriteLine("Within Greeting method. Name is:" + Name +" age is:"+ Age);
Console.ReadLine();
}
but this code does not execute. What am I am getting wrong?
Code:
Console.WriteLine("Hello, World!");
namespace crltemp
{
public class Person
{
public string Name;
public int Age;
public bool Haspet;
public void Greeting()
{
Console.WriteLine("Within Greeting method. Name is:" + Name + " age is:" + Age);
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "Daniel";
person.Age = 28;
person.Haspet = true;
person.Greeting();
}
}
}
|
|
|
|
|
Quote:
Console.WriteLine("Hello, World!");
namespace crltemp
{
... You've created a project using the new Top-level statements[^] syntax, and forgotten to remove the boilerplate.
As a result, the compiler will generate an entry-point for you which will simply write "Hello, World!" to the console and then exit. The additional Main method in your Program class will never be called.
Remove the code outside of your namespace, and your code should start behaving as expected.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks for your very swift reply to a very basic question. Removing the top-level statement meant the compiled program ran as intended.
|
|
|
|
|
Yet another example of Hutber's Law.
|
|
|
|