C# 代码为';t从函数返回字符串
我有以下问题:我想检查gameName是否包含函数中可用的预定义字符串,但不知何故他没有将它们传输回来。我真的不知道为什么,因为它们是在函数中声明的,应该传输回主要部分。顺便说一下,OpenFileDialog1的输入是一个名为:hl1-sp-the-infinite-shift.7z的文件C# 代码为';t从函数返回字符串,c#,function,C#,Function,我有以下问题:我想检查gameName是否包含函数中可用的预定义字符串,但不知何故他没有将它们传输回来。我真的不知道为什么,因为它们是在函数中声明的,应该传输回主要部分。顺便说一下,OpenFileDialog1的输入是一个名为:hl1-sp-the-infinite-shift.7z的文件 using System; using System.Collections.Generic; using System.ComponentModel; using Syst
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace MapTap2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string CheckGame(string sourceName, string gamename)
{
string hl1 = "hl1";
string hl2 = "hl2";
string hl2ep2 = "hl2-ep2";
string hl2ep1 = "hl2-ep1";
string bs = "bs";
string prtl = "prtl";
string prtl2 = "prtl";
string op = "op";
bool gamebool;
gamebool = sourceName.Contains(hl1);
if (gamebool == true)
{
gamename = "hl1";
return gamename;
}
gamebool = sourceName.Contains(hl2);
if (gamebool == true)
{
gamename = "hl2";
return gamename;
}
gamebool = sourceName.Contains(hl2ep2);
if (gamebool == true)
{
gamename = "hl2-ep2";
return gamename;
}
gamebool = sourceName.Contains(hl2ep1);
if (gamebool == true)
{
gamename = "hl2-ep1";
return gamename;
}
gamebool = sourceName.Contains(bs);
if (gamebool == true)
{
gamename = "bs";
return gamename;
}
gamebool = sourceName.Contains(prtl);
if (gamebool == true)
{
gamename = "prtl";
return gamename;
}
gamebool = sourceName.Contains(prtl2);
if (gamebool == true)
{
gamename = "prtl2";
return gamename;
}
gamebool = sourceName.Contains(op);
if (gamebool == true)
{
gamename = "op";
return gamename;
}
else
{
gamename = "You sure that this file is from PP?";
return gamename;
}
}
private void Form1_Load(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = @"C:\Users\";
openFileDialog1.Filter = "7zip archives|*.7z|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
}
private void cmdCompress_Click(object sender, EventArgs e)
{
string sourceName = "";
string targetFolderName = "testy";
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null )
{
sourceName = openFileDialog1.FileName;
lblconsole.Text = sourceName;
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "7za.exe";
p.Arguments = " e " + sourceName + " -o" + targetFolderName;
Process x = Process.Start(p);
x.WaitForExit();
//Check the Game Version of the archive (line 20)
string gamename = "If you see this in Debug you failed.\n Y U NO MAKE WORKING CODE?";
CheckGame(sourceName, gamename);
lblconsole.Text = gamename;
}
}
}
}
}调用函数时,实际上并没有将返回值设置为任何值。试着做
string gamename = "If you see this in Debug you failed.\n Y U NO MAKE WORKING CODE?";
gamename = CheckGame(sourceName, gamename);
lblconsole.Text = gamename;
作为补充说明,这里有很多优化。其中一个正在更改if
s和return
s的外观:
if (sourceName.Contains(hl1))
return = hl1;
快乐编码这是演示问题所需的最少代码量吗?如果没有,请编辑您的问题。在旁注上。。。了解如何使用switch语句!每当你有长if-else语句时,这是一个很好的指示器,这是一个很好的迹象,表明你应该使用switch@詹维德补充说。如果这解决了您的问题,请随意标记为已接受的答案。谢谢,欢迎光临。下一次的提示是,只需发布与手头问题相关的代码,这样其他人就不必过多筛选:)