Hidden .NET Gems – Path.Combine

0

Hi,

I’ve learned about the magic of the method Combine from Path a few months ago. Usually, even in production code, we tend to use string concatenation through either directly using ‘+’ or StringBuilder. Thing is, you do not want to directly use string concatenation to build your path. Try to think about using your code in an other OS, what then ? There’s more logic to think about to build your path. That’s not something that you want. Less code is better code for you, because there’s less to maintain ūüôā

Path.Combine does a lot of good ! For whatever OS your code might run on, it will build the correct path by using Path.PathSeparator. While building your path, it’s going to check whether or not the first path already has a separator at the end so it won’t duplicate it. While combining the paths, it’s going to check whether or not every paths, starting from the second one you provide, is a root path or not. It’s really easy to use to ! Being that it can accept an infinity of paths, you have no excuse to not use this jewel of .NET!

Path.Combine(firstPath, secondPath, ..., nPath)

 

Kevin

Advertisements

The Rebirth of the Phoenix

0

HEY !

This is Kevin here. This has been way too long since the last time I wrote something on the blog.
Usual excuse, I’ve been way TOO BUSY! But that’s going to change because I miss doing this on a regular basis.
I have so much content, fun content (don’t worry about it, there’s plenty for everyone). For now, there no tech discussion, but they¬†are on their way !

Please stay tune ūüôā

Kevin

Being a junior developer

0

Hi,

I stumbled on a really awesome article:¬†What it really means to be a ‚Äújunior‚ÄĚ developer. Jonathan Barronville had some great insights on what it truly means to be a junior developer. For us, freshly out of college with our diplomas, after four years of hard work, we feel like we know everything. That is so wrong because we don’t nearly know enough ! ¬†Of course, we saw a lot of concepts and we had tons of practice, but some¬†never had some real world experience.

Knowing a lot of concepts can help you at the beginning, but what really helps is to have real world experience. By having real world experience I mean to have projects when you have set backs at every turn or for some reasons, the project seems to fail and your team have a lot of work to in a little amount of time to deliver a fully functional project to your client(s). See, after writing that article, Scott Hanselman received Jonathan for an online interview. In this interview, he talked about something that we don’t truly value in our domain: failure. And that’s quite right. Who would go in an interview saying that I¬†tried to take on 4 projects by myself¬†right after college to get some experience, but I¬†failed to deliver what was ask ¬†on me on 2 of those projects and one that succeed had some serious memory leaks and I had to re-factor my code. Thing is, failure shapes us. By failing, we learn quite a lot of what should be done and what should not be.

As for myself, I’m still a student who has two more years to do before I can get my degree. Being a student and soon, a junior developer, means something else for me. It’s a world that moves really quickly, and I think (not sure) that it moves way quicker than it did in the past. So, I’m not trying to master everything because that’s impossible. There are things that interest me such as web and mobile applications. Being a student, I have all the time I need to get to know stuff.

I do have some major interest in C# because I’ve been trying a few things such as Java or Javascript. But having a lot of time gives me the freedom to try out new technologies and try to get know them. Knowing the options I can have to create a service or a software can truly help shape the architecture and the design I might take to program my application.

Even though I might know what senior developers or architects are talking about, there’s still a world of difference between “knowing” a technology and really¬†it, inside and out. My definition of a junior developer would be a new programmer who’s still trying out new technologies. Some can’t wait to be seniors or architects. It might be fun to get those titles, but the real deal, it’s the journey that we took to get at this level and that’s what I’m really into !

Kevin

Programming basics : manage program flow final part

0

Welcome reader,

This will be the final part in the programming basics mini series in how to manage the program flow of your software application. We’re going to see how to manage the flow of your program when we’re using a switch. It is a structure that is mostly use when we have predefined conditions.When this happen, knowing exactly those condition, we’ll have the correct operation to perform if it is met. For example, when someone is doing an exam, we could have three states

  1. He didn’t know what to do, so he had a 0
  2. The knowledge on the topic was not mastered he managed to get 11 out of 25
  3. The student mastered the topic and had 25 out of 25

If conditions like these we found in our application, it would be better to use a switch instead of the if-else if – else structure because we’ll have less to write. We’ll see below the structure of a switch statement :


/*

Everything in this code will not work

it is used to demonstrate how a switch statement is written

*/

switch (aVariable)
{
 case /*ValueOfAVariable*/:
        /*Operation*/;
 break; //tells the computer to stop the operation in this case
 default: //Represent what to do if no condition is met
        /*Operation*/;
}

In a swich statement, a case represents the value of the variable we’re evaluating in the switch. If a case is met, then all the operations for this case will be perform until the machine find the line with a break statement. Moreover, the cases can be link altogether. Like it was said before, until the computer find the statement “break;”, the switch will continue to operate. Below, we’ll see an example of such a switch:

switch(value)
{
   case 1:
   case 2:
   case 3:
   case 4:
     //do some stuff
     break;
  case 5:
  case 6:
    //do other stuff
    break;
  default:
      //do some default stuff
      break;
}

Also, we have the ternary operator, which embeds the way the if-else structure work. We’ll¬†be able to find the structure of the ternary operator below for when you want to assign a value to a variable:

  • dataType variable = ( aCondition) ? (firstValue) : (secondValue) ;

The ternary operator evaluates the given condition. If it is true, firstValue will be assign to variable. If the condition was to be false in our program, then secondValue would be assigned to variable. We’ll try out a code sample to see how it could be useful:


int minValue=15;

int midValue=30;

int maxValue=45;

int aMaxValue=(midValue>maxValue) ? midValue : maxValue;

All of this covers how to manage the program flow in your software application. If for the moment this does not look useful, in the next posts,  it will get more clear as we get deeper into programming as our knowledge of the basics gets better.

Kevin

Programming basics: manage program flow part two

0

Welcome reader,

We’ll pick up where we left with how to manage the program flow of a software application. We saw how to use the if-else if-else structure. To evaluate a condition, we need comparaison operators. Those are use evaluate a boolean expression, which will either result a true or false. Below, you’ll be able to find them.

Operator

Description

==

Equality

!=

Inequality

>

Greater than

<

Less than

>=

Greater or equal

<=

Less or equal

&&

Called Logical AND operator. If both the operands are true, then condition becomes true.

||

Called the logical OR operator. If any the operands is true, then the condition become true.

!

Called the logical NOT operator. It uses the reverse logical state of a an operand. Which means that if an operand was initially at true, the NOT operator makes it false during the evaluation

Thanks to these operators, ¬†there is a lot that can be achieved to evaluation an expression. We’ll use them in samples to try to understand how they work. You can use more than just one to evaluate a given condition in your program flow.

Here, we can see how to evaluate operand(values on each side of the operator) with the greater than and less than.
float bankAccount=2500;
if(bankAccount&gt;3000){
  Console.WriteLine("You have quite a big account");
}else if(bankAccount<1000 && bankAccount>2000){
Console.WriteLine("You have more than 1000 $ and less than 2000 $ in your account");
}else{
    Console.WriteLine("We can't evaluate the value in your account, here's how much you have now : {0} $",bankAccount);
}
This sample will show how to use the NOT opeator.
bool aBoolValue=true;
if(!aBoolValue){
    Console.WriteLine("That's not right ! Is it ?"); //You should see this as false
}
if(aBoolValue){
  Console.WriteLine("That is true! If you have guess that the ouput, good job!");
}
Finally, we’re going to see how to use the equality operator. Beware, because this is a general known issue for beginners; the “=” is use to assign values and the “==” is used to evaluate is two values are equals. Because if you use the “=” operator, it will be a true expression. Why? Because you are assigning a value to a variable; doing so you’re asking the computer, for example, is it true that this variable as the value of 8?
int ageOfPerson=18;
if(ageOfPerson == 18)&lt;/div&gt;
{
   Console.WriteLine(&quot;You are consider as an adult in Canada&quot;);
}
//next example is a bad example. Please be sure not to reproduce code likethis one
if(ageOfPerson=21)
{
    Console.WriteLine("You can vote in any country that you want, or I think ?");
}
We will learn how to use switches and the ternary operator in the last part of how to manage the program flow.
Kevin

Programming basics: manage program flow part one

0

Welcome reader,

Our journey into programming began with the use of variables, how to name and how to store data in them. Programming goes a bit further than this. When you tell your machine to perform a given operation, sometimes, you want to check a state or a condition before your console application performs the work that needs to be done. In your daily life, these events surely happens often such as “if I get 80 or more on this test, I’ll go to X party” or else if “I get a bonus in the end of year, I’m going to go to the Bahamas”. I think you can see a pattern? Basically, we do the same thing in a software application such as if this events does happen, perform this task. In short, we evaluate conditions and, depending of what happened and what we are looking for, we’ll either perform a task or we coule just do nothing. ¬†In programming, we use if-else if-else, the ternary operator or switches(more on them in a later post). We’ll code below an example in which will create a variable storing an integer value. We’ll evaluate conditions and then print the value of the variable if one of these conditions are met.


public static void Main(){

int ageOfClient=30;

//the operators which were use to create the condition will be explained in the next post

//using the if- else if - else structure

if(ageOfClient<18){

Console.WriteLine("The client is a child");

}

else{

Console.WriteLine("Your client is an adult");

}
}

// the ternary operator will be mention when we'll talk about strings.

Kevin

Programming basics: My first console application

0

Welcome reader,

We haven’t create an application since the start of the programming basics. It is sort of a ritual that the first program that a programmer writes will print of the screen the following : “Hello World!”. So, we are actually going to write this as our first console application.

Open your IDE and select Console application. Copy paste the follow code below and hit the run button in your IDE :

public static void Main()
   {
      System.Console.WriteLine(&quot;Hello, World!&quot;);
   }

There, you have the most basic application you can create. You are actually using a method called “WriteLine” and putting information inside the parenthesis. This information that was passed as a parameter (they will be discussed at length during the method post) was then written on your screen.

There you go ! It wasn’t so bad, you can now call yourself a “developer” or a “programmer”. As we’ll progress, you’ll see that programming isn’t so hard as long as you put your time in it and really want to learn. Being passionate about what you’re trying to achieve will help you to master a lot of subjects. Don’t beat yourself down if there was ever a subject that eluded you even if it was the 100th that you tried to do it. Take some time off and come back, make sure you understand the core fundamentals before moving foward !

Kevin

Programming basics: naming convention, indent and white spaces

0

Welcome reader,

We’ll follow where we left off when we’ve talked about variables. When you are behind you screen, there is a lot more going on then just writing lines in the IDE to make you computer do something. ¬†There are a set of rules that you should always stick to make your code clearer. Before, we talked about commenting your code. ¬†Comments are meant to explain the purposes of your program. As an example, ¬†when you’re creating variables, placing comments can help explain what you’re trying to do with those variables. When you’re programming, your instructions will be read by the IDE. Comments will be ignore by the IDE.

There are different ways to comment code. You can place comment on one line. You can also you what I call paragraph commenting. These will come handy when we’ll describe the behavior of methods. You’ll find some commenting examples.


//This is a single line comment

/*

This a paragraph comment. While I'm inside the /**/

all that will be written will be ignored by the IDE

*/

int myIntVariable; //Explain the usefulness of your variable with this comment 

More than just comments, there are other ways to make sure that other programmers who might read your code will appreciate reading it. As you could have seen, when I’m writting my variables, I follow a set of rules which I strongly suggest that you should also follow. This is called the “camelCase” convention. When you’re creating something like a variable or a method (more on methods later), these are the rules that you’ll have to follow.

  • The name of your variable cannot start with a number such as “123MyVariable”
  • The name of the variable should always start with a lower case letter such as “myVariable”
  • When you’re trying to write a composed name, you can either do this : “thisIsTheNameOfTheVariable” or use the underscore to separate either word of the variable’s name like this : ¬†“this_Is_The_Name_Of_The_Variable”
  • Never forget to give a meaningful name to your variable

Finally, even following those set of rules is not quite enough. A good programmer should always indent his code a have a moderate use of white spaces. Well, I’ve just blurred some words that won’t really mean something to you so I’ll explain what I’m trying to tell you. Indenting code is a convention where you place tabs in the blocks of code. As for white spaces, there just meant to make code easier to read. It could all be close together, but that wouldn’t do any good if one must spend some time, trying to decrypt what you’ve wrote.

You’ll find below an example :

/*
The following block of code won't work
It will serve as an example to make sure
that you understand indentation
*/
//indentation
int variable_1=10;
{
   int variable_2=13; //See? There was a tab after the curly brace
}

//white spaces(none)
int variable_3=15;
double variable_4=20.0;
decimal variable_5=0.05m;
short variable_6=56;

//white spaces
/*
Putting white before and after operators(more on them later)
is helpful. Your code does not have to be too cramped
More than just that, separate your variables makes it also easier to read

What I suggest is if you have a set of variables that have a relation
altogether, let them be in a group but after create an empty line and then
resume your coding activity
*/
int variable_7 = 123;

double variable_8 = 234.98;

decimal variable_9 = 0.45m;

First post

1

Hi everyone,

This is my first post in wordpress. I’ve created this website in order to help developers to dive into the great world of .NET! Thing is, it is so vast that it can be bothersome to try to find exactly what you want to achieve with .NET.

About everything can be achieved with .NET: you can create desktop application, web applications and services, mobile applications and so on. This blog will focus on helping you to get a better understanding of .NET.

At first, I will show you the core fundamentals of programming with C# and VB.NET. In order to achieve projects,it is my belief that people should have a good foundation of the basics before they can try to get into the different current hypes like voice enabled mobile apps or create a web-based video game.

I’d like to introduce myself. My name is Kevin Avignon, I’m a Java and C# developer. I started my path in the tech world about two years ago. I started programming with C# but I’m currently doing my IT engineering bachelor with Java.¬†I’ve been in love with the .NET world since I first wrote my C# console application. I’ll do my best to make sure that in time, you’ll love it as much I do.

I have topics that I’ll want to mention in this blog because either I consider them really important or because I have an interest in them. ¬†If ever there was a topic that was not on this website and you’d like me to talk about it, please make sure to let me know by commenting on my posts or posting your question in the Ask Kevin tab.

Let’s dive in into the world of .NET together and ¬†I hope you’ll enjoy the ride !

Kevin