locked
How to declare a local variable of type guid RRS feed

  • Question

  • User269416624 posted

    I must be missing something...

    Is it possible to declare a variable in C# as type Guid, and if so, how would you do that?

    Wednesday, March 25, 2015 5:56 PM

Answers

  • User-434868552 posted

    dolot

    Actually, it's not compiling clean.  I get an error on the Guid on the left side of the equals saying "A get or set accessor expected"

    sorry, but i have to ask:  did you change      Guid.empty    to   Guid.Empty    ?

    if i replace my test method with yours, i get the same error as you:

    [TestClass]
    public class ParsersUnitTests
    {
        [TestMethod]
        public void ParseGUID_Test
        {
            //arrange
            Guid g = Guid.empty;
        }
    }
    Error	1	A get or set accessor expected
    

    When i  change      Guid.empty    to   Guid.Empty  
    the code compiles clean     (oops)
    i still get your error.  (sorry, serves me right for rushing.)

    Your error is that you forgot the parentheses here:   public void ParseGUID_Test()   

    this compiles clean:

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace UnitTestProject1
    {
        [TestClass]
        public class ParsersUnitTests
        {
            [TestMethod]
            public void ParseGUID_Test()   //     ()   is   required !!!
            {
                //arrange
                Guid g = Guid.Empty;
            }
        }
    }

    When one gets an error, look at the surrounding lines of code too.

    i've been programming for almost half a century and i'm still a beginner.    B-( 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 26, 2015 9:52 AM

All replies

  • User2103319870 posted

    dolot

    Is it possible to declare a variable in C# as type Guid,

    Yes its possible. You can use the Guid.Empty as the default value for your variable

    dolot

    and if so, how would you do that?

    You can try with the below code

    //Declare a guid variable
            Guid Yourvariablename = Guid.Empty;

    Above code will assisgn "00000000-0000-0000-0000-000000000000" to the variable

    Wednesday, March 25, 2015 6:31 PM
  • User269416624 posted

    I tried that, but when I do I get a compiler error on the leftmost Guid - it says "A get or set accessor expected".

    Wednesday, March 25, 2015 6:40 PM
  • User-434868552 posted

    @dolot  

    it seems like you want to create a Guid within a class.

    Example:

    public class Dolot
    {
    	public Guid MyGuid { get; set; }
    }
    void Main()
    {
       Dolot dolot = new Dolot();
       Console.WriteLine (dolot.MyGuid);
       dolot.MyGuid = Guid.NewGuid();
       Console.WriteLine (dolot.MyGuid);
       dolot.MyGuid = new Guid();
       Console.WriteLine (dolot.MyGuid);
       dolot.MyGuid = Guid.NewGuid();
       Console.WriteLine (dolot.MyGuid);
    }

    output:

    00000000-0000-0000-0000-000000000000
    96070abe-fb67-46cd-a7ae-a74557af472a
    00000000-0000-0000-0000-000000000000
    f5eea56c-4d7d-43c4-96d0-2a18388af8a4


    Please clarify.

    Note:  showing the pertinent parts of your code can be helpful.

    http://weblogs.asp.net/gerrylowry/clarity-is-important-both-in-question-and-in-answer 

    Wednesday, March 25, 2015 7:32 PM
  • User-369506445 posted

    hi

    please try below code :

     static void Main(string[] args)
            {
    
                Guid guid= Guid.NewGuid();
                Console.WriteLine(guid.ToString());
                
            }

    Thursday, March 26, 2015 3:55 AM
  • User269416624 posted

    Here's the code:

        [TestClass]
        public class ParsersUnitTests
        {
            [TestMethod]
            public void ParseGUID_Test
            {
    
                //arrange
                Guid g = Guid.empty;
    
    
            }
        }

    Thursday, March 26, 2015 8:48 AM
  • User-434868552 posted

    @dolot

    i'm always happy to see programmers writing unit tests and i see from your code that you are using MSTest ...

    i suggest you strongly consider, if your management will allow you, switching to xUnit.net.

    FWIW, the ASP.NET 5 team uses xUnit.net, see http://stephenwalther.com/archive/2015/02/24/top-10-changes-in-asp-net-5-and-mvc-6 

    Meanwhile, back to your question:

    This code compiles clean because "Empty" is case sensitive:

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace UnitTestProject1
    {
        [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                Guid g = Guid.Empty;
            }
        }
    }

    https://msdn.microsoft.com/en-us/library/system.guid.empty(v=vs.110).aspx "Guid.Empty Field"

    public static readonly Guid Empty

    "A read-only instance of the Guid structure whose value is all zeros."

    Note:  if you want a Guid value, then you would use this:

    Guid g = Guid.NewGuid();

    https://msdn.microsoft.com/en-us/library/system.guid.newguid(v=vs.110).aspx 

    edit:

    BTW, when i use "empty" (lower case e), i get a different error message than yours:

    Guid g = Guid.empty;  // should be Guid.Empty !!!!!!!
    Error	1	'System.Guid' does not contain a definition for 'empty'

    FWIW

    end edit.

    Thursday, March 26, 2015 9:13 AM
  • User269416624 posted

    Actually, it's not compiling clean.  I get an error on the Guid on the left side of the equals saying "A get or set accessor expected"

    Thursday, March 26, 2015 9:30 AM
  • User-434868552 posted

    dolot

    Actually, it's not compiling clean.  I get an error on the Guid on the left side of the equals saying "A get or set accessor expected"

    sorry, but i have to ask:  did you change      Guid.empty    to   Guid.Empty    ?

    if i replace my test method with yours, i get the same error as you:

    [TestClass]
    public class ParsersUnitTests
    {
        [TestMethod]
        public void ParseGUID_Test
        {
            //arrange
            Guid g = Guid.empty;
        }
    }
    Error	1	A get or set accessor expected
    

    When i  change      Guid.empty    to   Guid.Empty  
    the code compiles clean     (oops)
    i still get your error.  (sorry, serves me right for rushing.)

    Your error is that you forgot the parentheses here:   public void ParseGUID_Test()   

    this compiles clean:

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace UnitTestProject1
    {
        [TestClass]
        public class ParsersUnitTests
        {
            [TestMethod]
            public void ParseGUID_Test()   //     ()   is   required !!!
            {
                //arrange
                Guid g = Guid.Empty;
            }
        }
    }

    When one gets an error, look at the surrounding lines of code too.

    i've been programming for almost half a century and i'm still a beginner.    B-( 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 26, 2015 9:52 AM
  • User269416624 posted

    Ha!  Indeed, that is it.  In retrospect, I can see how the error message should have pointed me to that. 

    I had been switching back and forth between C# and VB lately, and had noticed that C# doesn't always handle things the same as VB, so I had myself predisposed to think that this Guid issue was just a C# oddity.  I can see now that the oddity was between the keyboard and the chair.

    Thanks much!

    Thursday, March 26, 2015 10:16 AM