使用未分配的局部变量'ext' - 这里有什么问题? [英] use of unassigned local variable 'ext' - what is wrong here?

查看:78
本文介绍了使用未分配的局部变量'ext' - 这里有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里编写了代码,但在if语句中根据数据库中文件的文件扩展名选择了response.content类型,'ext'声明的字符串给了我一个错误。我该如何解决这个问题?



I have code here written but in the if statements to select the response.content type based on the file extension of the file in the database, the 'ext' declared string is giving me an error. How do I fix this?

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "view":
                        string filename = e.CommandArgument.ToString();
                        string path = Server.MapPath("~/BusinessProfilesContent/" + Session["BusinessName"] + "/" + filename);
                        String ext;
                        SqlConnection conn;
                        SqlCommand comm;
                        String connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
                        conn = new SqlConnection(connectionString);
                        comm = new SqlCommand("SELECT Incident.File_Extension FROM Incident	JOIN User_Acc ON user_acc.User_id=incident.user_id JOIN business ON incident.business_id=business.Business_id JOIN incident_type ON incident_type.incident_type_id = incident.incident_type_id WHERE Business.Business_Name = @BusinessName", conn);
                        comm.Parameters.Add("@BusinessName", System.Data.SqlDbType.VarChar).Value = Session["BusinessName"];
                        conn.Open();
                        SqlDataReader reader = comm.ExecuteReader();
                        while (reader.Read())
                        {
                          ext = reader["File_Extension"].ToString();                            
                        }
                        byte[] bts = System.IO.File.ReadAllBytes(path);
                        Response.Clear();
                        Response.ClearHeaders();
                        if ( ext == ".mov")
                        {
                            Response.ContentType = "video/quicktime";
                        }
                        else if (ext == ".jpg")
                        {
                            Response.ContentType = "image";
                        }
                        Response.WriteFile(path);
                        Response.Flush();
                        Response.End();
                        break;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);

            }
        }

推荐答案

解决方案1将有助于正式编译应用程序,但它会让它有缺陷。您需要解决问题的根本原因。想一想为什么不允许使用未分配的变量。虚拟值将允许您的代码跳过 if ... else if else 代码语句,它们不会为<$​​ c $ c>分配任何内容Response.ContentType 。语法规则的目的是避免这种情况。如果您没有分配 string.Empty ,但是将 else 添加到中会更好,为没有扩展匹配的情况分配内容。



另外,你需要拿ext.ToLower() ,以不区分大小写的方式进行比较。



-SA
The Solution 1 will help to formally compile the application, but it will leave it defective. You need to address the root cause of the problem. Think why it is not allowed to work with unassigned variables. A dummy value will allow your code to skip both if ... else if statements of your code, which will not assign anything to Response.ContentType. The purpose of the syntax rule was to avoid this situation. It would be better if you did not assign string.Empty but added else to your if, to assign something for the cases when none of the "extensions" match.

Also, you need to take ext.ToLower(), to compare in case-insensitive way.

—SA


只需指定一个虚拟值当你声明它时, ext 变量。

这样:

Simply assign a dummy value to ext variable when you declare it.
This way:
String ext = String.Empty;





希望这会有所帮助。





如果我是你,我会改变比较字符串的方式;特别是这些行:



Hope this helps.


If I were you, I would change the way I compare strings; specifically these lines:

if ( ext == ".mov")




else if (ext == ".jpg")



我会翻译成:


which I would translate to:

if (ext.Equals(".mov", StringComparison.InvariantCultureIgnoreCase))




else if (ext.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase))



分别。

[/编辑]


repectively.
[/Edit]


这篇关于使用未分配的局部变量'ext' - 这里有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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