CodeHeadTheo

Code and geek topics from an obbsessed enthusiast.

To string or not to String.

When to use String and string.

 

This question come up a few times now and I find myself going through this exercise to explain. So, I thought it was time to write it up. After all one of the core principles I live by as a developer is, “if you do or know you will be doing something more than once, automate it!”

 

 All of .Nets reference types inherit from System.Object either directly or indirectly, and the String class is no different. The keyword “string” (lower case s) is simply an alias for System.String and thus can be used directly. Let’s take a look here when we typeof both them and see what we get:

 

 

However, this is about understanding when to use ‘string’ and when to use ‘String’, who cares about a lower or upper case S right? Well, anyone trying to read your code will care and you will help them do that task much easier by knowing this little bit. When we declare and instantiate the following:

 

string  _foo = “This ‘string’ is a variable”;

 

We know by the lower case string and the dark color blue (at least in Visual Studio) that this is a variable.

Now, when we do it this way:

 

String  _foo = “This ‘String’ is a variable as well but looks like a the convention we use for methods”;

 

Here we know by the upper case S and the teal-ish color that this is a coding convention we use for Classes and Methods.

 

Now does it work this way? Sure let’s jump to visual studio and take a look:

 

 

 

As you can see in the output, both strings compiled and function just fine but again the second instantiation or capital style looks like a Class or Method convention. Further on down in code I call the String.Equals method and pass our 2 strings _roadGlide and _streetGlide to give you a visual comparative. Also, at the top in the first line of the code you can see where we define the class for ‘Program’ and compare its convention.

 

So in conclusion, when you need to declare or instantiate a string to use as a variable use ‘string’ and when you are calling a method off the String Class us the capital S convention. Anyone reading your code will thank you for it and as well you will thank yourself as you go back to read your code down the road.

 

Now get out the and color the world in CODE!

There is currently a lease on the blob and no lease ID...

A co-worker was having an issue trying to reuse a VHD that he previously used for another VM and was receiving an error. He then decided to delete the VHD and move on with a fresh VHD however was now receiving the following error:

Clicking on details yields the following:

 

 “Error deleting blob '/vhds/YourVHD.vhd”: details, There is currently a lease on the blob and no lease ID was specified in the request.

This is where I entered the story, this is “by design” behavior and the error is expected if, the disk you are attempting to work with is truly attached to an existing VM or if you are attempting to remove a VHD that is still considered leased and/or visible through the portal as a disk or an image in the Virtual Machines management interface.

So, let’s start off by checking to make sure that this VHD is not attached to a VM.

 

  1. In the Azure Management Portal, choose the Virtual Machines interface, and select Disks from the top right pane navigation. Look at the Attached To column and ensure it is blank, if it is not blank, then this is the issue.

 

  1. Now let’s figure out whether it is attached to a VM or not, manually review your instances interface and verify if the disk is attached or not. If it is and If you no longer want the VM and want to reuse the disk, in the  Instances interface on the top right pane navigation choose the VM that it was attached to and select Delete from the “Drawer” (bottom navigation ribbon). You will be presented with two options, Delete the attached disks and Keep the attached disks. Choose delete if you want to be done with the VM and the VHD or keep if you want to try and reuse it.

 

  1. If there is no instance with the disk you are referencing associated, Delete is greyed / not selectable, and you still cannot create a VM from this disk then keep reading. If removal is your goal then you should now be able to use the Storage interface to delete the VHD.

 

Stop here if your goal was to only remove the disk as you’re done. However, if you decide you want to reuse the VHD and chose to keep it then read on.

 

You can first try to delete the disk entry.

*Note* This will NOT delete the blob from storage, it simply removes the disk entry from the VM configuration.

To delete the Disk entry, navigate to Virtual Machines, then choose Disks on the top navigation. Highlight the disk in question and on the drawer choose Delete. You will be presented with two options, Delete the associated VHD and Retain the associated VHD. Make your choice to retain it if you want to reuse it or delete it if your goal is simply to be rid of it.

 

If you have chosen to keep it you will need to remove the lease from the VHD itself if you want to reuse it. There are several tools out there that will help with this, take a look at the Azure Storage Explorers page to see what is available. My choice was Cloud Explorer simply because I am familiar with it.

We are going to make a copy of our VHD here to work around this issue.

  1. Start at the Azure Management Portal, choose the Storage interface, then select the storage account that homes the VHD we are working with. Now on the Drawer (bottom) choose Manage Access Keys at the bottom, this will activate the information windows where you will need to copy your Storage Account Name and your Primary Access Key. Click the copy icon at end of the entry field to copy each one and paste into OneNote or your note keeper of Choice.


 

  1. Again I am using CloudXplorer, if you chose a different tool you will need to setup your account for that tool. In Cloud Explorer go to Accounts on the Home ribbon, choose New, select Azure Blobs Account. Enter the storage account name you made note of previously in the Name field and supply your primary access key for the Secret Key entry. Ensure that the Endpoint drop down is set for General (*.core.windows.net) and then everything else below should be unchecked or blank and click ok.

 


  

  1. Now in Cloud Explorer's  left navigation pane, double click or expand the storage account, select the vhds container and the in the right navigation pane select the VHD in question. Right click the VHD and select Rename and change it a another name and click Ok. You will receive an error stating again, There is currently a lease on the blob and no lease ID was specified in the request. Go ahead and click Ok to continue, then click Refresh on the Home ribbon or press (F5) to refresh. You should now see a copy of the VHD with the name you chose.


  

  1. Almost done, back over on the Azure Management Portal, choose the Virtual Machines interface, select Disks from the top navigation, and then Create from the drawer. Enter your new name for the disk, under VHD URL click the folder and browse through to the location of your VHD copy, make sure to check the selection The VHD contains an operating system. Lastly select the drop-down to specify if it is Windows or Linux and then click the Ok check mark at the bottom.

 

 

On the info drawer you will see Creating Disk YourDiskName, wait for the Successfully created disk YourDiskName

Viola you now have a usable disk! 

Go setup your Virtual Machine with your new disk, the process is a little different to reuse a disk. On the Drawer, click New -> Compute -> Virtual Machine -> From Gallery -> My Disks, and choose the new disk that you just created. From this point forward creation is just like creating a Virtual Machine from scratch.

 

*Note*

I have tested this method of using an Azure Storage Explorer and the rename method for creating a copies of disks without detaching them from the VM and it works well.

 

Happy Azure'ing!