My Development Snippets this Week

My Development Snippets this Week

Slow Start

The week started slow for me as I was super busy during the weekend. I felt tired on Monday. I didn't take a sick leave though as working from home, I could wake up later than usual and planned regular breaks during the day. In this way, I could still focus on my highlight of the day.

That's one of the perks of working from home.

How to code faster?

Break things faster.

Change the codes you want to.

Then use find references in your IDE and update or refactor as needed.

Find References

Run your program.

Do small commits so that you can easily stash or discard your changes.

Rinse and repeat.

How do you return a dynamic object in C#?

If you have a JavaScript background and working on C# but you don't want to create classes for every object, you can use dynamic classes and objects.

https://stackoverflow.com/questions/12709333/how-to-create-own-dynamic-type-or-dynamic-object-in-c

If you want your method to return such an object, add dynamic as return type.

More details:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/walkthrough-creating-and-using-dynamic-objects

Does C# has a built in capitalize method for a string?

I have the impression that C# does not come loaded with a Capitalize method. You need to create your own utility method or use CultureInfo namespace.

The ToTitleCase() method converts everything to uppercase.

https://docs.microsoft.com/en-us/dotnet/api/system.globalization.textinfo.totitlecase?view=net-5.0

How to get an enum value from a valid string in C#?

One thing I always seem to struggle with strong types languages is the use of enums. The syntax looks a bit different depending on programming language. Here is an example I've just used to parse a string into an enum:

(EnumType) Enum.Parse(typeof(EnumType), "Value");

How to wait for iframe to load when using Selenium?

Even with the use of both Explicit and Fluent Waits, I find Selenium a bit flaky or inconsistent at best when dealing with asynchronous changes or an iframe in a web application.

The codes below is the best way I've found this week to wait for an iframe to load before doing anything.

WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.TagName("iframe")));