Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

A method to see if something exists?

Posted on 2006-04-07
6
Medium Priority
?
196 Views
Last Modified: 2010-04-16
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() + "'.");
        }
0
Comment
Question by:paulwhelan
6 Comments
 
LVL 6

Accepted Solution

by:
maxy88 earned 120 total points
ID: 16400502

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
 

Author Comment

by:paulwhelan
ID: 16400617
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
 

Author Comment

by:paulwhelan
ID: 16400649
Actually yeah it throws an application error

Can I do something like

If(application error)
{
//it means it couldnt be found
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:mjmarlow
ID: 16401110
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
 
LVL 6

Expert Comment

by:maxy88
ID: 16401656

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
 
LVL 13

Expert Comment

by:devsolns
ID: 16402814
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

578 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