A method to see if something exists?

Hi


I have a method FindElementsGuid and I call it like this
Guid gElementGuid = myschema.FindElementsGuid(sElementReference);

But I want to find out if a Guid exists or not.

Would I be better writing a method 'ElementHasAGuid' (which would return a boolean) or can I get that information somehow?

Thanks
Paul

/// <summary>
        /// Find the elements guid with the given fullname using a breadth-first search through the hierarchy. Throws an exception if the element can't be found.
        /// </summary>
        /// <param name="fullname">Fullname to search for</param>
        /// <returns></returns>
        public Guid FindElementsGuid(string fullname)
        {
            foreach (Element oElement in AllElements)
                if (oElement.FullName == fullname)
                    return oElement.Guid;
            throw new ApplicationException("Couldn't find Elements Guid with Fullname '" + fullname.ToString() + "'.");
        }
paulwhelanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

maxy88Commented:

You can just return null in your method above and check it when it comes back:

Guid gElementGuid = myschema.FindElementsGuid(sElementReference);
if(gElementGuid == null)
{
   // Guid does not exist
}
else
{
   // Guid exists
}

....

        public Guid FindElementsGuid(string fullname)
        {
            foreach (Element oElement in AllElements)
                if (oElement.FullName == fullname)
                    return oElement.Guid;
            return null;
        }

Max.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
paulwhelanAuthor Commented:
Does it matter that FindElemensGuid throws and application error if it cant find the guid?

throw new ApplicationException("Couldn't find Elements Guid with Fullname '" + fullname.ToString() + "'.");


0
paulwhelanAuthor Commented:
Actually yeah it throws an application error

Can I do something like

If(application error)
{
//it means it couldnt be found
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

mjmarlowCommented:
If you expect that AllElements will always have the GUID you are searching for, then an exception is reasonable approach.   You should use exceptions to indicate an unexpected situation.

If you expect that occasionally your requested guid will not be found then return null as maxy88 suggests.

Also, if your GUID can be in AllElements - but the GUID can be null - use this:

     public Guid FindElementsGuid(string fullname, out boolean isInCollection)
        {
            foreach (Element oElement in AllElements)
                if (oElement.FullName == fullname)
                 {
                     isInCollection = true;
                     return oElement.Guid;
                 }

            isInCollection = false;
            return null;
        }

      call it:
     bool guidFound;
    Guid g = FindElementsGuid("some name", guidFound);
    if(guidFound) ....

   
0
maxy88Commented:

If you want to keep your exception and throw ti, you could certainly do something like this, but I think it's better to just return null or make a custom exception of your own.

try{
   Guid gElementGuid = myschema.FindElementsGuid(sElementReference);
   // Guid exists
}
catch(ApplicationException e)
{
   // Guid does not exist
}

Max.
0
devsolnsCommented:
Im not a big fan of the

try {
     //do something
}
catch(Exception e)
{

}

around things that you know can occur.

Look to incorporate similar functionality to that of the new 2.0 methods.

1.1 way
try {
     doube d = double.Parse(someVal);
}
catch
{
   //failed must not be double
}


2.0 way
double d;
if(double.TryParse(someVal, out d))
     //it worked.
else
    //it failed.


Besides exceptions are expensive.






0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.