My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Showing posts with label net. Show all posts
Showing posts with label net. Show all posts

Friday, June 29, 2018

Null conditional operator : Question mark (?) before Variable

One of the reader recently asked about the new kind of operator that we use in C# 6.0. He noticed that in the VS - 2015, there is a use of new operator as following. And, he wanted about what it is:
Eg: public class A {
   string method1 { get; set; }
}
..
var a = new A();
var foo = "anwar";
if(a?.method1 != foo) {
   //somecode
}

So, let me explain this brief concept!

It's the null conditional operator. It basically means to evaluate the first operand. If that is null, stop, with a result of null. If not, evaluate the second operand as a member of the first operand.

In the above example, the point is that if a is null, then a?.method1() will evaluate to null rather than throwing an exception.  It will then compare that null reference with foo, using string class overload, and find that they are not equal and execution will go into the body of the if statement.

In addition, it can be very useful when flattening a hierarchy and/or mapping objects. iEg, Instead of:

if (Model.Model2 == null
  || Model.Model2.Model3 == null
  || Model.Model2.Model3.Model4 == null
  || Model.Model2.Model3.Model4.Name == null)
{
  mapped.Name = "default"
}
else
{
  mapped.Name = Model.Model2.Model3.Model4.Name;
}
It can be written like (same logic as above)

mapped.Name = Model.Model2?.Model3?.Model4?.Name ?? "default";

So, with this I end. Go, grab a cuppa coffee. I will still do Cheers :)

Tuesday, February 28, 2017

CD with mapped mounted drive on network : CHDIR command


It has happened with me that I was able to succesfully mount a network drive and can freely access it using Windows file-system Explorer. But my script was not able to access it. Digging a step further, i noticed that I could not even CD to that drive on command prompt. Hence i finally wrote this blog post for anyone running into the similar issue.

Question : How do I change to a mapped network drive at the command line?

Issue:
Say, you have a networked drive mapped to "U:\"
Now you want to go to that drive from a command line.
However, when you try you get an error suggesting
C:>u: The system cannot find the drive specified.


Solution:
There are two aspects to the solution:
1. The Syntax to use CD command on Windows may be the cause. Remember that for changing directories you should use cd /d U: rather than just U:
2. You should first start using the map drive on command prompt. Command used to do that is as follows:
net use u:

See attached screenshot showing the error as well as what happened after solution.




C:\Users\mfaiz\Downloads>net use p:
Local name        p:
Remote name       \\sxa.xxx.xxx.com\ap-xxx
Resource type     Disk
The command completed successfully.