Posts by dcastro • 6,808 points
128 posts
-
3
votes1
answer1483
viewsA: Create a mock for any instance of a class
That is not possible. To test the class GeradorDeToken using unit tests, we have to isolate it from its dependencies - and this is done through dependency Injection. This injection can be made…
-
6
votes2
answers141
viewsA: Dynamic declaration in attribute property
I’ve seen this problem solved using a Type (I can’t remember now where, but I’m sure I’ve seen it more than once). public class MicroEntityAttribute : Attribute { public Type Adapter { get; set; } }…
-
7
votes1
answer310
viewsA: Search list elements with partial strings
Uses the extension Enumerable.Where to filter the list by a predicate. In this case, the predicate is: "the string must contain '123'". For this we can use String.Contains Usa Enumerable.Take to get…
-
2
votes1
answer151
viewsA: .Net Reflector compilation errors
Well, that’s three different questions. The first is why you did not import the namespace where the delegate is located Func. using System; The CS1660 error is probably because the method…
-
2
votes2
answers1460
views -
15
votes3
answers5198
views -
3
votes3
answers2971
views -
2
votes1
answer107
viewsA: Messagebox.Show error dialogresult
The bug says to look on line 25. You will definitely find this: using System.Windows.Forms.MessageBoxIcon; That doesn’t work because MessageBoxIcon is not a namespace, is a enum. Then just import…
-
1
votes1
answer475
viewsA: How to add the files in . gitignore in Android Studio?
As you may already know, you need to add these files to . gitignore. But gitignore only serves to ignore files that are not in source control (so that they continue not to be versioned). Whether…
-
5
votes1
answer1543
viewsA: Correct use of async and await in Asp.Net
Yes, when using async I/O, all methods from that point to the controller have to return a Task or Task<T>. It is customary to say "async all the way", which is code for "do not mix async code…
-
1
votes2
answers1158
viewsA: Remove specific data on a stack
By definition, a stack does not allow random access/insertion/removal. If you need to remove a specific item, consider using another data structure, such as List<T>. One more note: the class…
-
1
votes1
answer331
viewsA: Validation of Data Regex
Regex, definitely, is not the right tool to solve this problem. If you want to compare two dates, then use the struct DateTime. I’ll assume the input is two strings, s1 and s2, in a valid format.…
-
3
votes1
answer1756
viewsA: POST and GET methods in C#
It seems that this site requires the presence of the header User-Agent. Then just choose one (I used Curl’s user agent in the example below). The browser, Curl and Postman use their own Useragent.…
-
2
votes3
answers1242
viewsA: LINQ with Where condition using variable
Simple Boolean logic var users = from u in db.Usuario where idade == 0 || (u.Idade == idade && u.Nome == nome)
-
3
votes4
answers754
viewsA: String Array Printing in a Single Messagebox
Simply uses String.Join string.Join(", ", valores); Fiddle: https://dotnetfiddle.net/5nh3wo…
-
5
votes1
answer965
viewsA: How to simplify a foreach by a Linq expression - Lambda
This code cannot be converted directly to LINQ because it has an imperative style and is not functional. In other words, LINQ creates a functional style (without side effects, referential…
-
14
votes2
answers5787
viewsA: Cut string when it reaches space
Usa String.Split to separate the string into a collection of strings separated by a space. And then use FirstOrDefault to obtain the first item of that collection, or null if the collection is…
-
0
votes2
answers831
viewsA: Json with an empty list C#
The error is here: public FreightValue freightValue { get; set; } Like you said yourself, freightValue is a list of objects. So the property also has to be a collection (eg: List<T>) public…
-
2
votes4
answers4241
viewsA: How to get the amount of records in a query in Sqlite?
It is impossible to know how many rows were returned before going through the Reader date, because the command ExecuteReader creates the Reader before of all rows being returned from the server. The…
-
3
votes3
answers458
viewsA: How do I list all the files not added (untracked files) in GIT?
Usa git status Documentation: status
-
5
votes3
answers1482
viewsA: Why does Visual Studio suggest simplifying names? - IDE0001 Name can be simplified
Since string is a alias (pseudonym) of String, there is only one advantage: consistency. The standard is to use the aliases of C# (string, int, long, bool) instead of. NET types (String, Int32,…
-
2
votes1
answer336
viewsA: Response.Redirect() without generating System.Threading.Threadabortexception?
This is a problem specific to Webforms and does not occur in ASP.NET MVC, so I added the correct tag to the question. Translated from here: The default is to call Overload Redirect with the…
-
3
votes1
answer801
viewsA: How to group items in a list with a proper condition?
Use this extension. It goes through the list and: if you find a "I" size question, return it immediately if you find a "M" size question, store it in a buffer until you find a matching pair. When…
-
0
votes1
answer225
viewsA: Error compiling: Error 4 Assembly [...] must contain a strong signature to be marked as a prerequisite
The Assembly you’re trying to compile seems to have a strong assinature, probably to ensure its authenticity. Consequently, all libraries imported into the project must also have a subscription. It…
-
1
votes2
answers918
viewsA: Turn Stream into Byte Array
The problem is this: the MemoryStream starts by allocating a small buffer (for example, a 4 byte array) and, when the buffer fills, the MemoryStream creates a new buffer twice the size, copies the…
-
8
votes2
answers121
views -
3
votes2
answers1247
viewsA: How to implement a function to return the sum of squares in the proposed form?
acumular is a function that traverses a list, from left to right, and that goes calling the function combine passing 2 arguments: The value currently under examination The accumulator of the rest of…
-
4
votes6
answers2762
viewsA: How to walk an Enum?
Taking @ramaral’s advice, a more functional way of expressing code behavior would be: var valores = new Dictionary<char, int> { {'a', 1}, {'b', 2} }; int soma = teste.Select(c =>…
-
3
votes1
answer104
viewsA: How to read, search and change file by bytes?
Try using this library I developed - Sequences -, makes collection handling much easier. It has an API identical to Scala collections. var bytes = new byte[] {0x01, 0x02, 0x03, 0x62, 0x61, 0x72,…
-
3
votes1
answer54
viewsA: Refer to the "parent" of the class
base is used to call a method from one of the derived classes, ignoring methods from the current class (i.e., "non-virtual calls"). To reference the object itself whose code is being executed, use…
-
1
votes2
answers548
views -
4
votes2
answers339
viewsA: Trade foreach for for
Cycles should not be used for with a dictionary, because the dictionary, by nature, is not ordered. The order in which the items are returned is Undefined. So trying to access key-value pairs of the…
-
1
votes1
answer186
viewsA: c#: using threads in a "windows Forms" project
The problem is that the tick of System.Windows.Forms.Timer runs in the thread UI - and the thread UI is waiting for the tick to end. That is, a deadlock occurs. The solution is to make the thread UI…
-
2
votes1
answer350
viewsA: Desktop applications developed in HTML CSS and Javascript
Even if possible, I advise against developing a native Windows application with Web Views only - this leads to a bad user experience. Instead, if the goal is to let users use the application on…
-
3
votes2
answers211
viewsA: Entity Framework 6 Async and method . Wait()
You’re experiencing a deadlock. Wait() and Result can lead to deadlocks depending on the Execution context. Imagining that the method MontarConfiguracaoAsync was implemented as follows: public…
-
8
votes3
answers1704
viewsA: What is the use of Func<T, Tresult>
Imagine that you are writing a method that transforms each element of a list into another element. You want this transformation to be arbitrary, user-defined. This would be implemented like this:…
-
6
votes2
answers8059
viewsA: Run Windows Prompt commands and save the output in a text file
Usa > for redirect the output. ipconfig > file.txt
-
2
votes1
answer142
views -
1
votes2
answers879
viewsA: Create a routine within an Asp net application in c#
It is not advisable to schedule tasks within an ASP.NET application. Appdomain can be recycled at any time, and when it does, all threads that are not connected to a request processing will be…
-
2
votes1
answer2448
viewsA: Changing Routes with Asp.Net MVC
To use the attribute based routing, you need to call the method MapHttpAttributeRoutes (for Web API 2) or MapMvcAttributeRoutes (for MVC): config.routes.MapMvcAttributeRoutes();…
-
0
votes2
answers8188
viewsA: Read specific text inside a . txt in c#
I think the code is self-explanatory using System.IO; using System.Linq; try { var valores = File.ReadLines(filePath) .Select(line => line.Split(new[] {' '},…
-
7
votes3
answers673
viewsA: Functions with Textbox and Buttons type parameters
Complementing the @ramaral response, I think this is a good opportunity to use params. private static void Visible(params Control[] controls) { foreach(var control in controls) control.Visible =…
-
5
votes1
answer3145
views -
1
votes1
answer153
viewsA: GUI Applications - Speeding up! How?
Use of threads directly is advised against. The ideal is to use a high-level abstraction, such as Task or Task<T>. These encapsulate logic that must be executed asynchronously, and propagate…
-
0
votes2
answers330
viewsA: Should I wear rebase?
No. If you rebase master -> new_interface, the commits in master will be re-created in your branch. That is, there will be commits with the same name, content, and different hashes.…
-
3
votes2
answers6238
viewsA: How to know if the file is being used by another process before trying to read
Good I/O practice dictates that you should not check whether the file is in use before opening it, because such checking would be useless. For example, given the following pseudo-code:…
-
0
votes1
answer66
viewsA: How to register dependencies with Windsor that are in different layers of the application?
To Composition root - the place where dependencies are registered - is not part of any of the application layers. Hence the name, root. Therefore, Composition root has (and should) reference all…
-
1
votes1
answer67
viewsA: Remove namespace or full names
Visualstudio’s re-factoring functionality is very limited, but with Resharper you can do so: Refactor -> Remove unused References or simply Cleanup code (Ctrl+E, Ctrl+C)…
-
4
votes1
answer361
viewsA: Back debug line
In Visual Studio, in debug mode, a yellow arrow appears on the left side of the code. You can drag that arrow up to run code again.
-
4
votes1
answer3305
viewsA: Get path from desktop
Use the Enum Environment.SpecialFolder.Desktop string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string filePath = Path.Combine(desktop, "File.txt");…