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

x
?
Solved

not all code paths return a value

Posted on 2005-04-09
3
Medium Priority
?
249 Views
Last Modified: 2008-03-03
Hi,

I recently started to program in C#.
Whem implementing a collectionbase i'm getting this error "not all code paths return a value"

This is the code what causes the error:

            public ClsPicture item(string id)
            {
                  foreach(ClsPicture p in List)
                        if(p.pictureId == id)return p;
            }

and this is the complete class:

      public class ColPicture: CollectionBase
      {

            public int Add(ClsPicture picture)
            {
                  return(List.Add(picture));
            }

            public int IndexOf(clsType type)
            {
                  return( List.IndexOf(type));
            }

            public void Insert(int index, ClsPicture picture)
            {
                  List.Insert(index,picture);
            }

            public void Remove(int index)
            {
                  List.RemoveAt(index);
            }

            public void Remove(ClsPicture picture)
            {
                  int i = 0;
                  foreach(ClsPicture p in List)
                  {
                        if(p.pictureId == picture.pictureId)
                              List.RemoveAt(i);
                        i++;
                  }
            }

            public ClsPicture item(string id)
            {
                  foreach(ClsPicture p in List)
                        if(p.pictureId == id)return p;
            }
      
      }


This works great in Delphi,VB6.0,VB.Net and it probably does in C# but not in this way.
Any ideas!!!

Thanks for the help!

Kurt



0
Comment
Question by:KurtOngenaert
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 525 total points
ID: 13743867
You need to add return operator after foreach loop:

          public ClsPicture item(string id)
          {
               foreach(ClsPicture p in List)
                    if(p.pictureId == id)return p;

               return null;   // id not found
          }
0
 
LVL 5

Expert Comment

by:soumya_parida
ID: 13746560
The compiler expects you to provide a value to return in case the if condition given is not satisfied or in case the List doesn't have any items in it.
There is scenario where the control doesn't go in side the foreach loop or inside the if block.

you can do it this way too.

public ClsPicture item(string id)
          {
               ClsPicture p;
               foreach(p in List)
                    if(p.pictureId == id)break;

               return p;   // id not found
          }
0
 

Author Comment

by:KurtOngenaert
ID: 13746818

I know that the id comes from the list so the value is always found ( or should be).
Either way it worked.

Because I'll tried Alex solution before your comment i'll give the points to him.

Thank's for your comment soumya_parida.


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

571 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