Skip to main content

Posts

Showing posts with the label C#

C# Specify generic type parameter once and use it throughout class

Let's say I have a class ClassWhichDoesThings which makes various calls to methods such as DoSomething<TheTypeIWantToSpecifyOnce>(); DoAnotherThing<TheTypeIWantToSpecifyOnce>(); AndAnother<TheTypeIWantToSpecifyOnce>(); throughout the class. Is it possible to specify the generic type in one place (like a variable but not determined at runtime) without anything outside of the class having to also pass a generic type (avoiding ClassWhichDoesThings<T> ) such that the method calls become something like: Type WriteTypeOnce = typeof(TheTypeIWantToSpecifyOnce); DoSomething<WriteTypeOnce>(); DoAnotherThing<WriteTypeOnce>(); AndAnother<WriteTypeOnce>(); The objective here being that if I want to change the Type, I don't have to do a find and replace on 20 different method calls for example. Essentially I want a generic class which specifies its own generic type privately. Edit: In other words, I'm trying to better organise code w...

C# Regex replace all occurrences of 3 numbers together with a different one from a list

I have a list of numbers that have in the left the new number and in the right the old number I want to change, each separated by a Tab character: 000 256 007 002 056 078 And I have filenames with the following composition: aaaa_bbb_01_cccc_000_a aaaa_bbb_01_cccc_000_b aaaa_bbb_01_cccc_000_c aaaa_bbb_01_cccc_007_a aaaa_bbb_01_cccc_056_a I want to change the 3 digits that are together into it's corresponding new number, but the fact that some of the numbers in the filenames repeat have me a little stuck since I'm just trying to learn regex. The output I want would look like this (keeping the same order as above): aaaa_bbb_01_cccc_256_a aaaa_bbb_01_cccc_256_b aaaa_bbb_01_cccc_256_c aaaa_bbb_01_cccc_002_a aaaa_bbb_01_cccc_078_a How can I change these filenames using regex? If it's not possible using regex what other alternative could I use? by Claudia Provoste in StackOverflow on July 05, 2022 . Answer Extra...

Why am I getting HttpPostedFile instead of HttpPostedFileBase when iterating HttpFileCollection?

I'm iterating over an HttpFileCollection and trying to get a List<HttpPostedFileBase> as the result. public List<HttpPostedFileBase> GetFiles() { HttpFileCollection files = HttpContext.Current.Request.Files; List<HttpPostedFileBase> result = new List<HttpPostedFileBase>(); foreach (string fileName in files) { HttpPostedFileBase castedFile = files[fileName]; //This is HttpPostedFile and not HttpPostedFileBase result.Add(castedFile); } return result; } How can I get a List<HttpPostedFileBase> out of an HttpFileCollection? by Greg in StackOverflow on July 02, 2022 . Answer HttpPostedFile does not derive from HttpPostedFileBase. If you really want to return List<HttpPostedFileBase> instead of List<HttpPostedFile>, then wrap each HttpPostedFile object within an HttpPostedFileWrapper object: HttpPostedFileBase castedFile = new HttpPostedFileW...

Variance and Average on a 2D array in C#

I was reading this article and I trying to follow their code example but I think I am missing a library. They have this : First, let's create a 2D matrix with some random data. We'll use the System.Random class to generate pseudo-random numbers: var rand = new Random(); var matrix = new double[5, 5]; for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < matrix.GetLength(1); j++) { matrix = rand.NextDouble() * 100; } } Now that we have our data, we can calculate the mean and standard deviation : double mean = matrix.Average(); double stdDev = Math.Sqrt(matrix.Variance()); but when I tried that in C# does, I get this compile time error : Severity Code Description Project File Line Suppression State Error CS1061 'double[ , ]' does not contain a definition for 'Variance' and no accessible extension method 'Variance' accepting a first argument of type 'double[ , ]' could be f...

How to return both comparator IEnumerables (true and false) using LINQ?

Define the following variables: List<int> numbers = new List { 0, 1, 2, 3, 4, 5 }; Func<int, bool> comparator = (int t) => { return t < 3; } var (listWhereTrue, listWhereFalse) = /* looking for this snippet */; /// expected output: /// listWhereTrue = { 0, 1, 2 } /// listWhereFalse = { 3, 4, 5} Is there a LINQ combination that I can use to return two IEnumerable, in which one passes the comparator and the other doesn't? by dreamstep in StackOverflow on July 02, 2022 . Answer You can use ToLookup : List<int> numbers = new List<int> { 0, 1, 2, 3, 4, 5 }; Func<int, bool> comparator = t => t < 3; var lookup = numbers.ToLookup(i => comparator(i)); var listWhereTrue = lookup[true].ToList(); var listWhereFalse = lookup[false].ToList(); Or MoreLINQ 's Partition (though it returns IEnumerable<T> , not a List<T> ): var (listWhereTrue, listWhereFalse) = numbers.Partition(co...

Visual Studio - Files Does Not Showing on the Right

I just accidentally closed the tab showing the files in the project when i doing my blog project. How can i bring? Thanks. enter image description here by XcellentEEE in StackOverflow on June 05, 2022 . Answer You can open up solution explorer here in VS 2022 by WyattBradley on June 05, 2022 . Other helpful answers Either - View > Solution Explorer or CTRL + ALT + L You can also "pin" the solution explorer menu using the icon that looks like a pin on the top right hand corner of the solution explorer menu by bsod_ on June 05, 2022 .

How to write a word from a string. c#

I've started recently to learn c# and I have a problem. The problem gives me a sentence and a number and my program has to return that number's word. Here is what I've made: using System; string inputData = Console.ReadLine(); string text = inputData; inputData=Console.ReadLine(); int x = Convert.ToInt32(inputData); string currentWord = String.Empty; int wordCount = 1; for (int i = 0; i < text.Length; ++i) { if (text[i] == ' ') { wordCount++; currentWord = String.Empty; while (text[i] == ' ') i++; } if (text[i] != ' ') { currentWord += text[i]; } if (wordCount == x) Console.WriteLine(currentWord); } Console.Read(); For the sentence " I have two pens" and the number 2, the program returns h ha hav have. What do I do wrong? by Skike in StackOverflow on June 05, 2022 . Answer I would change it a bit. The problem why it's is giving multiple values,...

Can I integrate React into a Knockout JavaScript app?

I've been building an application in my spare time, its backend is C# and I use Knockout js for the front end, to be honest its an app I use out side of my main job for learning purposes etc, I may launch it one day not really sure. I've now a new job where they use React, so my question is can I now integrate React into my app so I can begin learning this library? I'd aim to keep each component separate but does anyone think this a bad idea mixing libraries like this? I haven't learnt React yet hence I want to start integrating it into my side project though I don't really want to start rewriting all the Knockout JavaScript again also. by Martin Cooke in StackOverflow on 2022-06-05 . Answer A React app can be initialized on any DOM node you want. Pick a page you want to put React on, create a DOM node (a simple div ) and create a Element on that node. <!DOCTYPE html> <html> <head> <meta...

How to change x:Name property inside xaml code using c# script?

Problem: Using code need to add the XAML x:Name property to my ShellContentItem, that I'm creating inside AppShell. Screenshots: by Matsenko1 in StackOverflow on 2022-06-04 . Answer When adding an element in c#, you don't "create an x:Name". Instead, you simplify define a property, and set that property to the element. private ShellContent myItem; // Inside your method myItem = new ShellContent(); ... Now you access myItem like any other property. If you wish to create and access a collection of items, you do that like any other kind of object in c#: private List<ShellContent> myItems; // In method. myItems = new List<ShellContent>(); for (...) { var item = new ShellContent(); myItems.Add(item); } Now access one like any other list: myItems[...] . by ToolmakerSteve on 2022-06-04 .

Disable certificate verification on Ubuntu

I have one very old legacy project, its web API, and really I need to call it (it hosts on Windows Server 2012). This API require .p12 premade client certificates include in request to it, and i have one. It works only with https and it have strange certificate. If i debug my .net 6 project (calls with RestSharp ) on Windows 10 - it's OK, but on Ubuntu 22.04 LTS I have issues. Adding TLSv1.0 or TLSv1.1 or TLSv1.2 support in /etc/ssl/openssl.cnf don't works. Curl -k or --insecure don't works. root@nginx:/home/xxx# curl -vvv https://192.168.201.111:44301/api/ * Trying 192.168.201.111:44301... * Connected to 192.168.201.111 (192.168.201.111) port 44301 (#0) * ALPN, offering h2 * ALPN, offering http/1.1 * CAfile: /etc/ssl/certs/ca-certificates.crt * CApath: /etc/ssl/certs * TLSv1.0 (OUT), TLS header, Certificate Status (22): * TLSv1.3 (OUT), TLS handshake, Client hello (1): * TLSv1.0 (IN), TLS header, Certificate Status (22): * TLSv1.3 (IN), TLS handshake, ...

How can I access a variable from another script in Unity?

I want to be able to use a variable from one script in another script. My goal is to allow me to reference the particleScale variable and use it to affect the size of the object connected to the second script. I also ant to later reference other variables from other scripts. There also will be several instances of each object. This is my first script; public class Particle : MonoBehaviour { public float particleSize; public Transform particle; void Start() { particle.localScale *= particleSize; } } This is my second; public class Magnetic : MonoBehaviour { public Transform magnetic; void Start() { magnetic.localscale *= Particle.particleSize; } } Help! by Kyle Bryson in StackOverflow on 2022-06-05 . Answer Try this: public class Particle : MonoBehaviour { public static Particle instance; public float particleSize; public Transform particle; void Awak...