在另一个方法内部调用方法? [英] Calling Method inside another Method?

查看:115
本文介绍了在另一个方法内部调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有这样的功能.
我在下面的函数中用(//lines)
提到了问题

Hi I have function like this.
I mentioned the problem in below function with(//lines)

public DataTable GetSubMainTypes(int mainid)
    {
        System.Data.DataTable dt = new DataTable();
        if (ConnectionString.Length > 0)
        {
            System.Data.SqlClient.SqlConnection dbConn = null;
            try
            {
                if (!runmethod)
                {
                    string Query = "";
                    if (mainid == 1)
                    {
                        Query = "SELECT dbo.submaintypes.submainid, dbo.SubMainTypes.Name, dbo.SubMainTypes.Type FROM dbo.MainTypes INNER JOIN dbo.SubMainTypes ON dbo.MainTypes.MainID = dbo.SubMainTypes.MainID where dbo.MainTypes.MainID =" + mainid.ToString();
                    }
                    else if (mainid == 2)
                    {
                        Query = "select c.name as 'Name',c.city as 'City',c.state as 'State',c.telephone as 'Telephone',c.organization as 'Organization',c.address1 as 'Address1',c.address2 as 'Address2',c.postalcode as 'PostalCode',cc.name as 'Country',c.telephone as 'Telephone',c.website as 'Website',c.rating as 'Rating',c.created as 'Created',c.createdby as 'CreadtedBy',c.lastmodified as 'LastModified',w.name  as 'WorkFlow' from customers c inner join countries cc on c.countryid=cc.id inner join workflow w  on w.wid=c.wid";
                    }
                    else if (mainid == 3)
                    {
                        Query = "select pt.ID as 'ID',pt.name as 'Name',T.name as 'Type' from ProjectTypes pt inner join Types t on pt.Typeid=t.Typeid";
                    }
                    else if (mainid == 4)
                    {
                        Query = "select p.projid, p.Name as 'Name',t.Name as 'Type' from project p inner join types t on p.typeid=t.typeid";
                    }
                    else if (mainid == 5)
                    {
                        FillGridview("select AID,[Name] as 'Name',DeadLine,ADescription from Assignment where Createdby= " + "'" + Environment.UserName + "'");
//The above function Returns DataSet
//I dont want to execute the below lines if (mainid=5)
                    }
                    dbConn = new System.Data.SqlClient.SqlConnection(ConnectionString);
                    dbConn.Open();
                    System.Data.SqlClient.SqlCommand dbCmd = new System.Data.SqlClient.SqlCommand(Query, dbConn);
                    System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(dbCmd);
                    da.Fill(dt);
                    da.Dispose(); dbCmd.Dispose();
                    return dt;
                }
            }
            catch { return dt; }
            finally
            {
                if (dbConn.State.ToString().Equals("Open"))
                {
                    dbConn.Close();
                }
                dbConn.Dispose();
            }
        }
        else { return dt; }
    }

推荐答案

添加一个表示要运行查询的布尔变量,并使用switch语句代替if/else.

Add a boolean variable indicating that the query is to be run, and use a switch statement instead of if/else.

bool executequery = true;
DataTable dt = null;
string Query = "";
switch (mainid)
{
    case 1 :
        Query = "blah blah";
        break;

    case 2 :
        Query = "blah blah blah";
        break;

    ...

    case 5 :
        FillGridView(...);
        executequery = false;
        break;
    
    default :
        executequery = false;
        break;
}
if (executequery)
{
    dt = new DataTable();
    dbConn = new...
    ...
}



我更改数据表初始化的原因是,如果您不打算执行查询,则分配数据表没有任何意义.如果您的方法返回null,则表示您无所事事.



The reason I changed the datatable initialization is because if you''re not going to perform a query, there''s no point in allocating the datatable. If your method returns null, you know there''s nothing to do.


方法"FillGridview"可能正在填充某些数据集,但未返回任何内容.如果要在此方法之后停止执行,则应将其填充到数据集中.

如果您不想填充它并且不想在此行之后执行任何操作,则只需编写"return dummyDataTable;"

创建没有任何内容的DataTable类型的dummyDatatable对象.


选项2:
使用switch case而不是If并使用 break ;
Method ''FillGridview'' might be filling some dataset but is not returning anything. You should fill this in a dataset if you want to stop execution after this method.

In case you dont want to fill it and dont want to execute anything after this line then just write "return dummyDataTable;"

create dummyDatatable object of type DataTable that has nothing.


OPTION2:
Use switch case instead of If and use break;


这给我一个错误


并非所有代码路径都返回一个值(当我使用Switch时)
具有空Datatable的DataGridview(当我使用If时).

任何帮助.
It gives me an error


Not all code paths returns a value(when i use Switch case)
DataGridview with Empty Datatable(when i use If else).

Any Help..


这篇关于在另一个方法内部调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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