Solved

Easy C# question about Guids

Posted on 2013-11-07
5
382 Views
Last Modified: 2013-11-08
I am taking SharePoint dev courses about SP workflows and the assumption is that I have already mastered C#, which i have not, I only have a decent understanding of the principles. So the course keeps on pulling a rabbit out of a hat, so to speak, without explanation. So I might have many more questions like this ion the future, but hopefully they will all be easy points for everyone.

I understand what default(Guid) does after some research, and the course code used this in another exercise
public Guid varname = default(Guid);

But now in this exercise, when it is declaring a field to hold a Guid value it is:
public Guid workflowId = default(System.Guid);

At first I thought it needed System.Guid instead of just Guid because I have no using statement at the top for System.Guid, so in this case, it has to be explicit. So when I went to add using System.Guid, intellisense does not know what I am talking about. I have read that Guid is in the System namespace, so I thought that maybe I need to add a reference to System.Guid, but under .net, I cannot find any such reference.

So what's going on?

Also, inside of an event handler, it calls this
taskID = Guid.NewGuid();

I understand that the Guid has a static method called NewGuid, which does not create an empty guid like in the case of the field variable declaration above, but instead generates an actual Guid value. But I don't understand how this method could just seemingly be called out of nowhere right after a Guid value type. Does it have something to do with the fact that taskID has already been declared as a Guid field/variable?
0
Comment
Question by:BobHavertyComh
  • 2
  • 2
5 Comments
 
LVL 39

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
Comment Utility
You would need System.Guid if you didn't have a "using System;"  statement.  If you have the using statement, you should just be able to refer to Guid as you normally would.  The using refers to the namespace (eg: system) . . . and then you get access to all classes in that namespace by default.


Just a note on one of the most useful tricks I've ever learned.

Type this into your page (remove references to system.Collection if it's there):

        Thread x;

Click back on the word "Thread"

Hit ctrl + period

This will automatically ask you whether you want to add the using statement or change the list to be the fully qualified name . . . very useful if you're trying to find out where something is coming from.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
Admittedly, I have no SP development experience. Based on what I read above:

At first I thought it needed System.Guid instead of just Guid because I have no using statement at the top for System.Guid, so in this case, it has to be explicit.
That is correct.

I have read that Guid is in the System namespace, so I thought that maybe I need to add a reference to System.Guid, but under .net, I cannot find any such reference.
Try mscorlib.dll.

But I don't understand how this method could just seemingly be called out of nowhere right after a Guid value type.
As long as you declared the variable somewhere before you attempt to use it (e.g. assign to it), then you can use it. So...

Does it have something to do with the fact that taskID has already been declared as a Guid field/variable?
...yes.
0
 
LVL 9

Author Closing Comment

by:BobHavertyComh
Comment Utility
Thanks guys. It looks like it doesn't even need default(System.Guid) and default(Guid) seems to build fine, so maybe it's just a redundancy in the course's code as it's not really the best course in the world, but it's cheap. what they did was not wrong, it just wasn't necessary. As far as a using statement, apparently "using System;" itself takes care of it after what I have just read, even though it is in the mscorlib.dll. But when i go to add mscorlib.dll to the references, it tells me that this will be added at compile time, so it does not add it.

As far as Guid.NewGuid, the leading Guid threw me off, but I suppose that this is the same thing as saying String.Empty, so i can just look at it that way, where if you call a static method of a class, you have to refer to the class itself first.

Thank you much. I may have many more questions following, and hopefully they are all easy for anyone like this one was. Microsoft does technically have these explanations, but they aren't very explicit and while I can often figure the explanation out myself, I sometimes cannot and need some confirmation.
0
 
LVL 9

Author Comment

by:BobHavertyComh
Comment Utility
Correct on one thing. The code was System.Guid because one instance of this was automatically populated by default by VA, and the other was generated by the auto field builder when one is binding something to a field, so i guess that any auto builders try to be as safe as possible and make no assumptions, so it is System.Guid rather than just Guid Either way, apparently it is the same thing.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
As far as Guid.NewGuid, the leading Guid threw me off, but I suppose that this is the same thing as saying String.Empty, so i can just look at it that way, where if you call a static method of a class, you have to refer to the class itself first.
Correct.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now