我还是新手,所以我会尽力解释我的问题.英语不是我的第一语言,所以如果我错误地使用某些术语,我会道歉.
我有一个100行代码,每按一次按钮就会执行一次.我的问题是,我有20个按钮,它们都包含相同的代码(它们在从不同来源获取信息的方式上略有不同).有没有办法做到这一点,而不是复制相同的代码太多次.
基本上我的代码是这样的:
private void button1_Click(object sender, EventArgs e)
{
//file data source url
sourceUrl = ("www.myurl.com")
//Grab data
code
code
code
//Store data
code
code
code
//Write data
code
code
code
}
Run Code Online (Sandbox Code Playgroud)
除"sourceUrl"部分外,每个按钮都具有相同的代码.如果我想添加更多按钮,我必须复制>粘贴整个代码,我的应用程序开始变得巨大.有没有办法通过只让代码一次缩小代码,然后每次按下按钮时调用一个动作或方法.因此,不是多次使用100行代码,而是每个按钮都有一个行代码,顶部有一个100行代码,它们将成为该行代码的源代码.
提前致谢
我试图只抓住这个字符串中的一个:
Serving Size: 1 cup, halves, Calories: 48.6, Fat: 0.5g, Carbs: 11.7g, Protein: 1g
Run Code Online (Sandbox Code Playgroud)
还有很多人喜欢它.虽然每个字符串都有不同的变量.例如,上面的字符串可能如下所示:
Serving Size: 100 oz, Calories: 48.6, Fat: 0.5g, Carbs: 11.7g, Protein: 1g
Run Code Online (Sandbox Code Playgroud)
等等...
所以,我现在正试图获得字符串中的"oz"或"cup"部分.
我试过的正则表达式看起来像这样:
(?<=Serving Size:\s\d*\s)
Run Code Online (Sandbox Code Playgroud)
虽然在rubular中它一直说"无效".
虽然如果我这样做
(?<=Serving Size:)\s\d*\s
Run Code Online (Sandbox Code Playgroud)
它将完美匹配数字...我正在尝试排除数字并匹配度量类型.
我怎样才能做到这一点?
我有实体框架的问题.特别是我不能做插入或更新操作.错误信息:
An error occurred while updating the entries. See the inner exception for details.System.Data.SqlClient.SqlException (0x80131904): The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) …Run Code Online (Sandbox Code Playgroud) 你如何在一个空格之前插入N个逗号到这个字符串,但不是在一个句号或另一个逗号之后?使用ruby或javascript.
我有以下练习:定义一个名为circle的类,它接受point类型的对象,并计算它们与圆心的距离.如果该点在圆圈之外,则应发送异常通知.
这是我的代码:
class Point
{
protected int x,y;
public Point(int x,int y)
{
this.x = x;
this.y = y;
}
}
class Circle : Point
{
public Circle(Point p,int radius):base(3,5)
{
}
}
class Program
{
static void Main(string[] args)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道在圆圈课上我要做什么,我怎么知道圆点是否在圆圈内?感谢大家.
我有:
string s = "A=1,B=2,C=3";
Run Code Online (Sandbox Code Playgroud)
我需要使用LINQ在哈希表中输出.
Hastable应填写为
["A"] | 1
["B"] | 2
["C"] | 3
Run Code Online (Sandbox Code Playgroud) 我要做的是在box1中输入随机单词,单击一个按钮,然后在box2中打印所有以"D"开头的单词.因此,如果我输入"Carrots Doors Apples Desks Dogs Carpet"之类的东西,然后点击"Doors Desks Dogs"按钮将打印在box2中.
string s = box1.Text;
int i = s.IndexOf("D");
string e = s.Substring(i);
box2.Text = (e);
Run Code Online (Sandbox Code Playgroud)
当我使用这个^^
它会打印出"门苹果书桌狗地毯",而不仅仅是D'.
注意:这些单词是一个例子,我可以在box1中输入任何内容.
有帮助吗?
我试图从一堆文件名中删除空格(目录中的pdf).我尝试了以下内容.输入和输出目录都是folderbrowserdialog box
DirectoryInfo di = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
foreach (var file in di.GetFiles())
{
try
{
File.Copy(file.FullName, outputDir + @"\" + file.Replace(" ", "_"));
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的课程:
public class RawBomItem
{
private string material;
private string item;
private string component;
private string quantity;
private string b;
private string spt;
...
}
Run Code Online (Sandbox Code Playgroud)
每个数据库都有一个属性.
然后我有一个List包含这个类的实例
private List<RawBomItem> rawBom;
Run Code Online (Sandbox Code Playgroud)
该列表包含超过70000个项目.
此时我想在此List上运行一个复杂的LINQ查询.
List<string> endProducts = new List<string>(
rawBom.Where(x1 => new List<string>(rawBom.Select(x2 => x2.Component)
.Distinct())
.Contains(x1.Material) && (x1.B != "F"))
.Select(x3 => x3.Material));
Run Code Online (Sandbox Code Playgroud)
查询似乎遇到了无限循环.(我等了好几分钟然后把它关了)
我会把它变成DB工作,我只是对可能出现的问题感兴趣.
我是RoR的新手.我在Ruby 1.9.3中编程,Rails 3.2在我的模型中:
class SharedInfo < ActiveRecord::Base
attr_accessible :shared_info_type_id, :severity_id, :source_info_id, :created_date
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器中:
@sharedinfo = SharedInfo.new(params[:shareinfo])
@sharedInfo.longitude = params[:longitude][:value]
@sharedInfo.latitude = params[:latitude][:value]
@sharedInfo.save
Run Code Online (Sandbox Code Playgroud)
当它执行它.浏览器引导错误:"未定义方法`longitude ='为nil:NilClass"有没有人帮我解决这个问题?非常感谢你!