如何在输入参数中传递加号?(How do I pass a plus sign in input parameter?)
如何在我的输入参数中传递带有加号的电子邮件地址?
用户名值是“johnsmith+1@gmail.com”
http://domain.com/page1.aspx?username=johnsmith+1@gmail.com
? 它似乎不工作?
How do I pass an email address with a plus sign in my input parameter?
Username Value is "johnsmith+1@gmail.com"
http://domain.com/page1.aspx?username=johnsmith+1@gmail.com
? It does not seem to work?
原文:https://stackoverflow.com/questions/3628182
最满意答案
你正在与编译器作斗争; 你应该继续使用重载。
“我宁愿从普通类型'容器'中检测出类型。”
您的选择是变体或无类型指针。 您将不得不解压缩“Value”参数。 使用无类型指针,您将不得不完成所有工作; 使用变体,您将不得不完成大部分工作。 很乱。
“它们几乎完全相同的重复代码,除了微小的变化,取决于Value是Variant还是TObject。”
如果确实如此,那么你仍然应该继续使用重载,但是添加一个内部的“SetProp”方法,该方法采用“规范化”的数据来完成实际的工作。 您的“重复”代码是属性值的设置。 但是,您仍然需要编写特定的代码来破解传入的“Value”参数,无论您有一个接受“容器”类型的方法,还是多个重载方法,这些方法采用您想要接受的各种类型。 在one-method-container类型中,您将拥有一个(复杂的)if-then-else块,它会破坏Value。 在重载方法类型中没有if-testing; 你只需破解每个方法接受的类型的值。 主要优点是您的对象更好地记录:您可以看到“值”可接受的类型,更好的是,编译器可以帮助您,因为它“知道”可接受的类型。 使用单方法方法,编译器将无法帮助您强制执行“值”类型; 你正在做所有的工作。
此外,使用重载方法,我不会有一个接受变量(虽然下面的例子)。 对每个字符串,整数,双精度等都有一个单独的重载。
type TNormalizedPropValue = record // .... end; procedure TProps.internalSetProp(Value : TNormalizedPropValue); begin // // Set the property value from the "Normalized" pieces and parts. // end; procedure TProps.SetProp(Value : TObject); var NormalizedObjectPropValue : TNormalizedPropValue; begin // Copy the pieces and parts from "Value" into NormalizedObjectPropValue // internalSetProp(NormalizedObjectPropValue); end; procedure TProps.SetProp(Value : variant); var NormalizedVariantPropValue : TNormalizedPropValue; begin // Crack "Value" variant and copy the pieces and parts into NormalizedVariantPropValue // internalSetProp(NormalizedVariantPropValue); end;
You are fighting the compiler; You should continue to use overloads.
"I would rather detect the type from a common type 'container'."
Your choices are variant or untyped pointer. You are going to have to unpack the "Value" parameter. With an untyped pointer you will have to do all the work; with a variant you will have to do most of the work. Very messy.
"They do pretty much the same repeating code except from minor variations depending on whether the Value is Variant or TObject."
If that is really true then you should still continue to use overloads but add an internal "SetProp" method that takes "normalized" data that does the actual work. Your "repeating" code is the setting of the property values. But you still have specific code to write to crack the incoming "Value" parameter whether you have one method that accepts a "container" type or multiple overloaded methods that take the various types you want to accept. In the one-method-container type you will have a (complex) if-then-else block that cracks the Value. In the overloaded-methods type there is no if-testing; you just crack the Value for the type that each method accepts. The major advantage is that your object is better documented: you can see what types are acceptable for "Value" and, better still, the compiler helps you because it "knows" what types are acceptable. With your one-method approach the compiler will not be able to help you enforce the type of "Value"; you are doing all the work.
Also, using the overloaded methods, I wouldn't have one that accepts variant (although the example below does). Have an separate overload for each of string, integer, double, etc.
type TNormalizedPropValue = record // .... end; procedure TProps.internalSetProp(Value : TNormalizedPropValue); begin // // Set the property value from the "Normalized" pieces and parts. // end; procedure TProps.SetProp(Value : TObject); var NormalizedObjectPropValue : TNormalizedPropValue; begin // Copy the pieces and parts from "Value" into NormalizedObjectPropValue // internalSetProp(NormalizedObjectPropValue); end; procedure TProps.SetProp(Value : variant); var NormalizedVariantPropValue : TNormalizedPropValue; begin // Crack "Value" variant and copy the pieces and parts into NormalizedVariantPropValue // internalSetProp(NormalizedVariantPropValue); end;
相关问答
更多-
Func
或谓词 [2023-08-11]?(Func or Predicate ?) 在LINQ中, Func用于Where这样的事情,以便使索引和元素一致的其他重载是一致的: IEnumerable Where(IEnumerable source, Func predicate) IEnumerable Where(IEnumerable source, Func predicate) 就个人而言,我认为Predicate这个名称更具描述性,所以我会在没有上述一致性问题的情况下使用它。 请注意,只有 ... -
你必须在参数后面加一个:_* ,告诉编译器这不是一个参数,而是整个参数序列: action(closeables :_*) 在评论中编辑有关您的第二个问题:针对您的特定问题,不使用可变参数可能是更好的选择,但是直接将结果Seq与部分函数结合使用: def usingva[T <: Closeable, R](closeables: T*)(action: PartialFunction[Seq[T], R]): Unit = { try { action(closeab ...
-
将Variant(Integer)参数转换为Variant(Long)(Converting a Variant (Integer) parameter to Variant (Long))[2022-09-23]
我认为你应该像这样定义你的函数: Function Foo(ByVal vNum As Variant) As Long 如果未指定ByVal,则参数将传递给ByRef,并且您对参数所做的任何操作都是在“原始”变量上进行的,因此是整数。 I think you should just define your function like this: Function Foo(ByVal vNum As Variant) As Long If you don't specify ByVal, your pa ... -
在VBA中传递Variant参数ByRef时编译错误(Compilation-error while passing Variant argument ByRef in VBA)[2021-11-06]
用ByVal装饰参数 Public Sub SomeMethod(ByVal argument As someclass) 然后你可以传递一个变体; SomeMethod var 这是因为ByVal argument是作为Variant[someclass]的引用的本地副本接收的,这意味着VB可以自由地执行类型转换(转换Variant[someclass]->someclass )。 它作为ByRef传递时不起作用,因为对argument任何更改也会影响当前过程之外的调用对象引用变量。 如果传递As O ... -
好吧,正如Igor Tandetnik建议的那样,我做了类似的事情: VARIANT * pVARIANT_IN_EXTENSION = new VARIANT() ; VariantInit(pVARIANT_IN_EXTENSION); IDispatch * piDisp = pVARIANT_IN->pdispVal ; ::DISPPARAMS invokeparams = {0} ; HRESULT hRes = piDisp->Invoke(0, IID_NULL, 0, DISPATCH_P ...
-
如何将Variant或TObject传递给相同的方法参数?(How can I pass either Variant or TObject to the same method argument?)[2023-08-12]
你正在与编译器作斗争; 你应该继续使用重载。 “我宁愿从普通类型'容器'中检测出类型。” 您的选择是变体或无类型指针。 您将不得不解压缩“Value”参数。 使用无类型指针,您将不得不完成所有工作; 使用变体,您将不得不完成大部分工作。 很乱。 “它们几乎完全相同的重复代码,除了微小的变化,取决于Value是Variant还是TObject。” 如果确实如此,那么你仍然应该继续使用重载,但是添加一个内部的“SetProp”方法,该方法采用“规范化”的数据来完成实际的工作。 您的“重复”代码是属性值的设置。 ... -
传递方法作为参数(Pass method as argument)[2023-09-27]
您需要@selector编译器指令从方法名称中提取select,就像您在创建计时器时所做的那样: [self TimeViewWithtimeInterval:.6 selector:@selector(targetOpenView)]; 并将您的参数定义为SEL类型: -(void) TimeViewWithtimeInterval:(float)interval selector:(SEL)openViewMethod { ... } 然后,当将参数传递给NSTimer方法时,您可以不使用 ... -
Variant不能容纳对象,它只能包含诸如整数和字符串之类的基本类型。 我会建议改变你的阵列是你想要的类型而不是变体。 如果您不确定所需的对象类型,请创建一个TObject数组或您的数组将保存的对象的最低可能基类。 A Variant cannot hold objects, it can only contain primitive types such as integer and string. I would suggest changing your array to be of the type ...
-
欢迎您将您想要的任何参数传递给您想要的任何功能。 如果你需要在另一个函数中使用传递的值,你需要将它保存在某个地方,以便后面的函数仍然可以访问它。 使用您的示例,您似乎已为showkeyboard函数提供了Sender参数。 我假设您正在传递对触发键盘显示的TEdit控件的引用。 存储在vkeyboard的Tvkeyboard对象稍后需要使用该值,因此请将该值的副本提供给Tvkeyboard对象。 声明一个TEdit字段: type Tvkeyboard = class(...) ... pu ...
-
尝试插入虚拟类型作为variant的第一个参数。 正如您发现的注释所解释的那样,只有变体中的第一个类型用于变体自己的默认构造函数。 您可以为此使用空结构类型( struct NoFunction {}; )。 也就是说,您可能已经想到了将boost :: functions用作变体中的类型的想法......它们至少是默认构造的。 我不确定你从这种方法中得到的另一个错误是由什么引起的,但只是想让你知道如果你不能使用我提到的虚拟类型的解决方法,你可以更多地追求这个角度。 Try inserting a dumm ...