在for或foreach循环中处理对象的最佳实践是什么?我们应该在循环外创建一个对象并重新创建它(使用new ...)或者为每个循环迭代创建一个新对象吗?
例:
foreach(var a in collection)
{
SomeClass sc = new SomeClass();
sc.id = a;
sc.Insert();
}
Run Code Online (Sandbox Code Playgroud)
要么
SomeClass sc = null;
foreach(var a in collection)
{
sc = new SomeClass();
sc.id = a;
sc.Insert();
}
Run Code Online (Sandbox Code Playgroud)
哪个更好?
我构建了一个自定义WebControl,它具有以下结构:
<gws:ModalBox ID="ModalBox1" HeaderText="Title" runat="server">
<Contents>
<asp:Label ID="KeywordLabel" AssociatedControlID="KeywordTextBox" runat="server">Keyword: </asp:Label><br />
<asp:TextBox ID="KeywordTextBox" Text="" runat="server" />
</Contents>
<Footer>(controls...)</Footer>
</gws:ModalBox>
Run Code Online (Sandbox Code Playgroud)
该控件包含两个ControlCollection属性,'Contents'和'Footer'.从未尝试使用多个控件集合构建控件,但是像这样(简化)解决了它:
[PersistChildren(false), ParseChildren(true)]
public class ModalBox : WebControl
{
private ControlCollection _contents;
private ControlCollection _footer;
public ModalBox()
: base()
{
this._contents = base.CreateControlCollection();
this._footer = base.CreateControlCollection();
}
[PersistenceMode(PersistenceMode.InnerProperty)]
public ControlCollection Contents { get { return this._contents; } }
[PersistenceMode(PersistenceMode.InnerProperty)]
public ControlCollection Footer { get { return this._footer; } }
protected override void RenderContents(HtmlTextWriter output)
{
// Render content controls.
foreach …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我收到每个数据40.000行.我有500万行要处理(500 MB的MySQL 5.0数据库).
实际上,那些行存储在同一个表中=>缓慢更新,难以备份等.
在这样的应用程序中使用哪种方案以允许数据的长期可访问性而没有太大的表,容易备份,快速读/写的问题?
是postgresql不是更好地mysql为这样的目的是什么?
我们希望实现一个"新闻提要",用户可以看到她的朋友广播的消息,先用最新消息排序.但是Feed应该反映她朋友列表中的变化.(如果她添加了新朋友,那些来自这些消息的消息应该包含在Feed中,如果她删除了朋友,则不应该包含他们的消息.)如果我们使用pubsub-test示例并将收件人列表附加到每封邮件,这意味着很多当用户连接和断开朋友时,操纵邮件收件人列表.
我们首先使用传统的RDBMS思想模拟发布 - 订阅"扇出".它似乎首先起作用,但是,由于IN运算符以它的方式工作,我们很快意识到我们无法继续这条路径.我们发现Brett Slatkin在去年Google I/O上的演示,我们现在已经观看了几次,但我们不清楚如何使用"动态"收件人列表.
我们需要的是在建模时如何"思考"的一些提示.
python google-app-engine social-networking google-cloud-datastore
我是一位有能力的红宝石程序员.昨天我决定最终尝试使用Apple的Cocoa框架.帮我看看ObjC的方式吗?
我想围绕让我的头objc_allocateClassPair和objc_registerClassPair.我的目标是动态生成一些类,然后能够像任何其他类一样使用它们.这是否适用于Obj C?
分配和注册类后A,我在调用时遇到编译错误[[A alloc] init];(它说'A' Undeclared).我只能A使用运行时的objc_getClass方法实例化.有没有办法告诉编译器,A并像我一样传递消息NSString?编译器标志还是什么?
我有10个左右,其他类(B,C,...),都具有相同的超类.我想直接在代码中(消息他们[A classMethod],[B classMethod]......),而无需objc_getClass.我想在这里过于动态还是只是拙劣地实施我的实施?它看起来像这样......
NSString *name = @"test";
Class newClass = objc_allocateClassPair([NSString class], [name UTF8String], 0);
objc_registerClassPair(newClass);
id a = [[objc_getClass("A") alloc] init];
NSLog(@"new class: %@ superclass: %@", a, [a superclass]);
//[[A alloc] init]; blows up.
Run Code Online (Sandbox Code Playgroud) 堆内存是用Java收集的.
堆栈垃圾也收集了吗?
如何回收堆栈内存?
我正在使用.NET SmtpClient发送电子邮件,其中主题可能包含ASCII范围之外的字符.在RFC 2047定义了如何当它含有特殊字符的电子邮件正文应进行编码.以下是电子邮件标题中主题的示例:
Subject: Votre enregistrement numéro 123
在编码为ISO-8859-1之后,这应该成为:
Subject: =?iso-8859-1?Q?Votre=20enregistrement=20num=E9ro=20123?=
所有特殊字符,包括?,=(和其他人)和白色空间,使用编码的=xx转义序列.
但是,当我查看SmtpClient产生的内容时,我发现它不会逃避空白区域,这意味着邮件客户端会收到此标头:
Subject: =?iso-8859-1?Q?Votre enregistrement num=E9ro 123?=
这意味着编码在(我的阅读)RFC 2047中被打破.一些电子邮件客户端对这种不正确的编码(其中大多数,实际上包括Outlook和gmail)非常满意,但是一个(wanadoo.fr)以原始格式显示标题.这不是用户应该看到的:-(
这个问题有没有已知的解决方法?
注意: .NET 4.0实现SmtpClient按预期编码主题,产生此输出,这是正确的:
Subject: =?Windows-1252?Q?Votre_enregistrement_num=E9ro_123?=
我需要在www.euroworker.no/order上设置此更新按钮(您必须将一个项目添加到购物车,使用Kjøp按钮添加和Handlevogn以查看购物车).适用于FF和IE.(尽管IE存在另一个对齐问题)但Chrome或Safari中没有.之前我曾经工作过,但我唯一能想到的就是瞄准野生动物园和Chrome.这可能吗?
这是CSS和HTML(Smarty).
HTML(Smarty的):
{capture assign="cartUpdate"}
<div id="cartUpdate"><!--<input type="submit" class="submit" value="{tn _update}" />-->
<button type="submit" class="submit" id="oppdatersubmit" name="saveFields" title="Oppdater" value=""> </button> </div>
{/capture}
{assign var="cartUpdate" value=$cartUpdate|@str_split:10000}
{php}$GLOBALS['cartUpdate'] = $this->get_template_vars('cartUpdate'); $this->assign_by_ref('GLOBALS', $GLOBALS);{/php}
{form action="controller=order action=update" method="POST" enctype="multipart/form-data" handle=$form id="cartItems"}
CONTENT
{/form}
Run Code Online (Sandbox Code Playgroud)
而CSS:
#oppdatersubmit {
background-image:url(../../upload/oppdater.png);
background-repeat:no-repeat;
background-position:left;
background-color:none;
border:none;
overflow:hidden;
outline:none;
white-space: nowrap;
width:77px;
height:25px;
cursor:pointer;
position:absolute;
}
#cartUpdate {
position:absolute;
width:160px;
height:30px;
left:580px;
bottom:130px;
}
Run Code Online (Sandbox Code Playgroud)
需要为Chrome和Safari更改这些内容.
谢谢.
这是检测用户正在运行的设备的正确方法吗?
NSString *currentModel = [[UIDevice currentDevice] model];
if ([currentModel isEqualToString:@"iPhone"]) {
// The user is running on iPhone so allow Call, Camera, etc.
} else {
// The user is running on a different device (iPod / iPad / iPhone Simulator) disallow Call.
}
Run Code Online (Sandbox Code Playgroud) 我有一个处理一些数据的函数,并找到用最低错误对数据进行分类的阈值.它看起来像这样:
void find_threshold(FeatureVal* fvals, sampledata* data, unsigned int num_samples, double* thresh, double* err, int* pol) {
//code to calculate minThresh, minErr, minPol omitted
printf("minThresh: %f, minErr: %f, minPol: %d\n", minThresh, minErr, minPol);
*thresh = minThresh;
*err = minErr;
*pol = minPol;
}
Run Code Online (Sandbox Code Playgroud)
然后在我的测试文件中我有这个:
void test_find_threshold() {
//code to set up test data omitted
find_threshold(fvals, sdata, 6, &thresh, &err, &pol);
printf("Expected 5 got %f\n", thresh);
assert(eq(thresh, 5.0));
printf("Expected 1 got %d\n", pol);
assert(pol == 1);
printf("Expected 0 got %f\n", err);
assert(eq(err, …Run Code Online (Sandbox Code Playgroud)