C# - 错误"不是所有code路径返回一个值"与数组作为out参数 [英] C# - Error "not all code paths return a value" with an array as out parameter

查看:99
本文介绍了C# - 错误"不是所有code路径返回一个值"与数组作为out参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有以下code:


  

公众诠释GetSeatInfoString(DisplayOptions选择,出来的String [] strSeatInfoStrings)

  {
        strSeatInfoStrings = NULL;
        诠释计数= GetNumOfSeats(选择);        如果((计数< = 0))
            返回0;        strSeatInfoStrings =新的字符串[统计]        INT I = 0;        对于(INT指数= 0;&指数LT = m_totNumOfSeats - 1;指数++)
        {
            如果(string.IsNullOrEmpty(m_nameList [指数]))
                strSeatInfoStrings [I +] =


  
  

m_nameList [指数]的ToString();
              }

 }


这code产生的,...... GetSeatInfoString.DisplayOptions错误,出来的String [])':不是所有的code路径返回一个值基本上,我期待在上面做。方法是循环的阵列以及包含一个字符串数组中的任何值,我想这些然后加入到新的数组,strSeatInfoStrings进而,可以从一个单独的类,然后在显示新的数组的内容被称为列表框。

如何纠正这种任何建议?

在此先感谢


解决方案

您可以添加回 strSeatInfoStrings.Length

 公众诠释GetSeatInfoString(DisplayOptions选择,出来的String [] strSeatInfoStrings)    {
        strSeatInfoStrings = NULL;
        诠释计数= GetNumOfSeats(选择);        如果((计数< = 0))
            返回0;        strSeatInfoStrings =新的字符串[统计]        INT I = 0;        对于(INT指数= 0;&指数LT = m_totNumOfSeats - 1;指数++)
        {
            如果(string.IsNullOrEmpty(m_nameList [指数]))
                strSeatInfoStrings [I +] =
m_nameList [指数]的ToString(); }    返回strSeatInfoStrings.Length;    }

I currently have the below code:

public int GetSeatInfoString(DisplayOptions choice, out string[] strSeatInfoStrings)

    {
        strSeatInfoStrings = null;
        int count = GetNumOfSeats(choice);

        if ((count <= 0))
            return 0;

        strSeatInfoStrings = new string[count];

        int i = 0;

        for (int index = 0; index <= m_totNumOfSeats - 1; index++)
        {
            if (string.IsNullOrEmpty(m_nameList[index]))
                strSeatInfoStrings[i++] =

m_nameList[index].ToString(); }

    }

This code produces an error of, "...GetSeatInfoString.DisplayOptions, out string[])': not all code paths return a value. Basically, what I am looking to do in the above method is to cycle through an array and for any values in the array that contain a string, I want these then adding to the new array, strSeatInfoStrings which in turn, can be called from a separate class and the new array content then displayed in a listbox.

Any suggestions on how to rectify this?

Thanks in advance

解决方案

You can add return strSeatInfoStrings.Length at the end

public int GetSeatInfoString(DisplayOptions choice, out string[] strSeatInfoStrings)

    {
        strSeatInfoStrings = null;
        int count = GetNumOfSeats(choice);

        if ((count <= 0))
            return 0;

        strSeatInfoStrings = new string[count];

        int i = 0;

        for (int index = 0; index <= m_totNumOfSeats - 1; index++)
        {
            if (string.IsNullOrEmpty(m_nameList[index]))
                strSeatInfoStrings[i++] =
m_nameList[index].ToString(); }

    return strSeatInfoStrings.Length;

    }

这篇关于C# - 错误&QUOT;不是所有code路径返回一个值&QUOT;与数组作为out参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆