Patterns for simulating optional "out" parameters in C#? - c#

I'm translating an API from C to C#, and one of the functions allocates a number of related objects, some of which are optional. The C version accepts several pointer parameters which are used to return integer handles to the objects, and the caller can pass NULL for some of the pointers to avoid allocating those objects:
void initialize(int *mainObjPtr, int *subObjPtr, int *anotherSubObjPtr);
initialize(&mainObj, &subObj, NULL);
For the C# version, the obvious translation would use out parameters instead of pointers:
public static void Initialize(out int mainObj, out int subObj,
out int anotherSubObj);
... but this leaves no way to indicate which objects are unwanted. Are there any well-known examples of C# APIs doing something similar that I could imitate? If not, any suggestions?

Well, you shouldn't be using int for the objects anyway - they should be references, i.e. out SomeMainType mainObj, out SomSubType subObj. That done, you can then use overloads, but this would be ungainly.
A better approach would be to return something with the 3 objects - a custom type, or in .NET 4.0 maybe a tuple.
Something like:
class InitializeResult {
public SomeMainType MainObject {get;set;}
public SomeSubType SubObject {get;set;}
...
}
public static InitializeResult Initialize() {...}
Re-reading it, it looks like the caller is also passing data in (even if only null / not-null), so out was never the right option. Maybe a flags enum?
[Flags]
public enum InitializeOptions {
None = 0, Main = 1, Sub = 2, Foo = 4, Bar = 8, ..
}
and call:
var result = Initialize(InitializeOptions.Main | InitializeOptions.Sub);
var main = result.MainObject;
var sub = result.SubObject;

The closest translation would be using ref and IntPtr
public static void Initialize(ref IntPtr mainObj, ref IntPtr subObj,
ref IntPtr anotherSubObj)
and specifying IntPtr.Zero for unwanted values.
But for me the question arises why you want to resemble the API that close unless you are trying to figure out a P/Invoke signature. Assuming mainObj has accessible references to both sub object something like
public static MainObjectType Initialize(bool initSubObj, bool initAnotherSubObj)
appears to be a much cleaner solution to me. In .NET 4 you can even make the boolean arguments optional or simulate this with overloads in pre .NET 4. If there are no accessible references to the sub objects you could return a simple container type holding the references.

You can provide overloads of the method that don't take out parameters, and call the overload that does :
public static void Initialize()
{
int mainObj;
int subObj;
int anotherSubObj;
Initialize(out mainObj, out subObj, out anotherSubObj);
// discard values of out parameters...
}
public static void Initialize(out int mainObj, out int subObj, out int anotherSubObj)
{
// Whatever...
}
But as suggested by Marc, you should probably consider using a more object oriented approach...

Related

How can set optional parameter in method [duplicate]

Note: This question was asked at a time when C# did not yet support optional parameters (i.e. before C# 4).
We're building a web API that's programmatically generated from a C# class. The class has method GetFooBar(int a, int b) and the API has a method GetFooBar taking query params like &a=foo &b=bar.
The classes needs to support optional parameters, which isn't supported in C# the language. What's the best approach?
Surprised no one mentioned C# 4.0 optional parameters that work like this:
public void SomeMethod(int a, int b = 0)
{
//some code
}
Edit: I know that at the time the question was asked, C# 4.0 didn't exist. But this question still ranks #1 in Google for "C# optional arguments" so I thought - this answer worth being here. Sorry.
Another option is to use the params keyword
public void DoSomething(params object[] theObjects)
{
foreach(object o in theObjects)
{
// Something with the Objects…
}
}
Called like...
DoSomething(this, that, theOther);
In C#, I would normally use multiple forms of the method:
void GetFooBar(int a) { int defaultBValue; GetFooBar(a, defaultBValue); }
void GetFooBar(int a, int b)
{
// whatever here
}
UPDATE: This mentioned above WAS the way that I did default values with C# 2.0. The projects I'm working on now are using C# 4.0 which now directly supports optional parameters. Here is an example I just used in my own code:
public EDIDocument ApplyEDIEnvelop(EDIVanInfo sender,
EDIVanInfo receiver,
EDIDocumentInfo info,
EDIDocumentType type
= new EDIDocumentType(EDIDocTypes.X12_814),
bool Production = false)
{
// My code is here
}
From this site:
https://www.tek-tips.com/viewthread.cfm?qid=1500861
C# does allow the use of the [Optional] attribute (from VB, though not functional in C#). So you can have a method like this:
using System.Runtime.InteropServices;
public void Foo(int a, int b, [Optional] int c)
{
...
}
In our API wrapper, we detect optional parameters (ParameterInfo p.IsOptional) and set a default value. The goal is to mark parameters as optional without resorting to kludges like having "optional" in the parameter name.
You could use method overloading...
GetFooBar()
GetFooBar(int a)
GetFooBar(int a, int b)
It depends on the method signatures, the example I gave is missing the "int b" only method because it would have the same signature as the "int a" method.
You could use Nullable types...
GetFooBar(int? a, int? b)
You could then check, using a.HasValue, to see if a parameter has been set.
Another option would be to use a 'params' parameter.
GetFooBar(params object[] args)
If you wanted to go with named parameters would would need to create a type to handle them, although I think there is already something like this for web apps.
You can use optional parameters in C# 4.0 without any worries.
If we have a method like:
int MyMetod(int param1, int param2, int param3=10, int param4=20){....}
when you call the method, you can skip parameters like this:
int variab = MyMethod(param3:50; param1:10);
C# 4.0 implements a feature called "named parameters", you can actually pass parameters by their names, and of course you can pass parameters in whatever order you want :)
An easy way which allows you to omit any parameters in any position, is taking advantage of nullable types as follows:
public void PrintValues(int? a = null, int? b = null, float? c = null, string s = "")
{
if(a.HasValue)
Console.Write(a);
else
Console.Write("-");
if(b.HasValue)
Console.Write(b);
else
Console.Write("-");
if(c.HasValue)
Console.Write(c);
else
Console.Write("-");
if(string.IsNullOrEmpty(s)) // Different check for strings
Console.Write(s);
else
Console.Write("-");
}
Strings are already nullable types so they don't need the ?.
Once you have this method, the following calls are all valid:
PrintValues (1, 2, 2.2f);
PrintValues (1, c: 1.2f);
PrintValues(b:100);
PrintValues (c: 1.2f, s: "hello");
PrintValues();
When you define a method that way you have the freedom to set just the parameters you want by naming them. See the following link for more information on named and optional parameters:
Named and Optional Arguments (C# Programming Guide) # MSDN
Hello Optional World
If you want the runtime to supply a default parameter value, you have to use reflection to make the call. Not as nice as the other suggestions for this question, but compatible with VB.NET.
using System;
using System.Runtime.InteropServices;
using System.Reflection;
namespace ConsoleApplication1
{
public class Class1
{
public static void SayHelloTo([Optional, DefaultParameterValue("world")] string whom)
{
Console.WriteLine("Hello " + whom);
}
[STAThread]
public static void Main(string[] args)
{
MethodInfo mi = typeof(Class1).GetMethod("sayHelloTo");
mi.Invoke(null, new Object[] { Missing.Value });
}
}
}
I agree with stephenbayer. But since it is a webservice, it is easier for end-user to use just one form of the webmethod, than using multiple versions of the same method. I think in this situation Nullable Types are perfect for optional parameters.
public void Foo(int a, int b, int? c)
{
if(c.HasValue)
{
// do something with a,b and c
}
else
{
// do something with a and b only
}
}
optional parameters are for methods. if you need optional arguments for a class and you are:
using c# 4.0: use optional arguments in the constructor of the class, a solution i prefer, since it's closer to what is done with methods, so easier to remember. here's an example:
class myClass
{
public myClass(int myInt = 1, string myString =
"wow, this is cool: i can have a default string")
{
// do something here if needed
}
}
using c# versions previous to c#4.0: you should use constructor chaining (using the :this keyword), where simpler constructors lead to a "master constructor".
example:
class myClass
{
public myClass()
{
// this is the default constructor
}
public myClass(int myInt)
: this(myInt, "whatever")
{
// do something here if needed
}
public myClass(string myString)
: this(0, myString)
{
// do something here if needed
}
public myClass(int myInt, string myString)
{
// do something here if needed - this is the master constructor
}
}
The typical way this is handled in C# as stephen mentioned is to overload the method. By creating multiple versions of the method with different parameters you effectively create optional parameters. In the forms with fewer parameters you would typically call the form of the method with all of the parameters setting your default values in the call to that method.
Using overloads or using C# 4.0 or above
private void GetVal(string sName, int sRoll)
{
if (sRoll > 0)
{
// do some work
}
}
private void GetVal(string sName)
{
GetVal("testing", 0);
}
You can overload your method. One method contains one parameter GetFooBar(int a) and the other contain both parameters, GetFooBar(int a, int b)
You can use default.
public void OptionalParameters(int requerid, int optinal = default){}
For a larger number of optional parameters, a single parameter of Dictionary<string,Object> could be used with the ContainsKey method. I like this approach because it allows me to pass a List<T> or a T individually without having to create a whole other method (nice if parameters are to be used as filters, for example).
Example (new Dictionary<string,Object>() would be passed if no optional parameters are desired):
public bool Method(string ParamA, Dictionary<string,Object> AddlParams) {
if(ParamA == "Alpha" && (AddlParams.ContainsKey("foo") || AddlParams.ContainsKey("bar"))) {
return true;
} else {
return false;
}
}
Instead of default parameters, why not just construct a dictionary class from the querystring passed .. an implementation that is almost identical to the way asp.net forms work with querystrings.
i.e. Request.QueryString["a"]
This will decouple the leaf class from the factory / boilerplate code.
You also might want to check out Web Services with ASP.NET. Web services are a web api generated automatically via attributes on C# classes.
A little late to the party, but I was looking for the answer to this question and ultimately figured out yet another way to do this. Declare the data types for the optional args of your web method to be type XmlNode. If the optional arg is omitted this will be set to null, and if it's present you can get is string value by calling arg.Value, i.e.,
[WebMethod]
public string Foo(string arg1, XmlNode optarg2)
{
string arg2 = "";
if (optarg2 != null)
{
arg2 = optarg2.Value;
}
... etc
}
What's also decent about this approach is the .NET generated home page for the ws still shows the argument list (though you do lose the handy text entry boxes for testing).
I have a web service to write that takes 7 parameters. Each is an optional query attribute to a sql statement wrapped by this web service. So two workarounds to non-optional params come to mind... both pretty poor:
method1(param1, param2, param 3, param 4, param 5, param 6, param7)
method1(param1, param2, param3, param 4, param5, param 6)
method 1(param1, param2, param3, param4, param5, param7)... start to see the picture. This way lies madness. Way too many combinations.
Now for a simpler way that looks awkward but should work:
method1(param1, bool useParam1, param2, bool useParam2, etc...)
That's one method call, values for all parameters are required, and it will handle each case inside it. It's also clear how to use it from the interface.
It's a hack, but it will work.
I had to do this in a VB.Net 2.0 Web Service. I ended up specifying the parameters as strings, then converting them to whatever I needed. An optional parameter was specified with an empty string. Not the cleanest solution, but it worked. Just be careful that you catch all the exceptions that can occur.
For just in case if someone wants to pass a callback (or delegate) as an optional parameter, can do it this way.
Optional Callback parameter:
public static bool IsOnlyOneElement(this IList lst, Action callbackOnTrue = (Action)((null)), Action callbackOnFalse = (Action)((null)))
{
var isOnlyOne = lst.Count == 1;
if (isOnlyOne && callbackOnTrue != null) callbackOnTrue();
if (!isOnlyOne && callbackOnFalse != null) callbackOnFalse();
return isOnlyOne;
}
optional parameters are nothing but default parameters!
i suggest you give both of them default parameters.
GetFooBar(int a=0, int b=0) if you don't have any overloaded method, will result in a=0, b=0 if you don't pass any values,if you pass 1 value, will result in, passed value for a, 0 and if you pass 2 values 1st will be assigned to a and second to b.
hope that answers your question.
In the case when default values aren't available the way to add an optional parameter is to use .NET OptionalAttribute class - https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.optionalattribute?view=netframework-4.8
Example of the code is below:
namespace OptionalParameterWithOptionalAttribute
{
class Program
{
static void Main(string[] args)
{
//Calling the helper method Hello only with required parameters
Hello("Vardenis", "Pavardenis");
//Calling the helper method Hello with required and optional parameters
Hello("Vardenis", "Pavardenis", "Palanga");
}
public static void Hello(string firstName, string secondName,
[System.Runtime.InteropServices.OptionalAttribute] string fromCity)
{
string result = firstName + " " + secondName;
if (fromCity != null)
{
result += " from " + fromCity;
}
Console.WriteLine("Hello " + result);
}
}
}
You can try this too
Type 1
public void YourMethod(int a=0, int b = 0)
{
//some code
}
Type 2
public void YourMethod(int? a, int? b)
{
//some code
}

Cannot use Classes on this ref ClassName (byRef) method Feature C# 7.2

i was testin the new and shiny C# 7.1/7.2/7.3 features and when i was trying out the this ref Class, it was not working, meanwhile this ref int worked, do you guys have any idea on how to make it work with classes?
Example of the code:
public static bool Works(this ref int i)
{
return i == 0;
}
public static bool DontWorks(this ref Test i)
{
return i.A == 0;
}
public class Test
{
public int A { get; set; }
}
I am sorry that the title was a little bit bad, but i didnt know on how to make it better, feel free to give me suggestions or edit if you like.
Thank you for your time, and have a nice day.
Ref extension methods are only allowed on types known to be structs. This is intentional. The reasons behind this can be found in the feature proposal document.
Besides, regular class types are reference types. Passing a reference type as a parameter does not copy the object, unlike with value type parameters.

How to make C# COM class support parameterized properties from VB6

I've researched this question quite a bit, and while I've found a lot about C# and parameterized properties (using an indexer is the only way), I haven't found an actual answer to my question.
First, what I'm trying to do:
I have an existing COM DLL written in VB6 and I'm trying to create a C# DLL that uses a similar interface. I say similar because the VB6 DLL is only used with late binding, so it doesn't have to have the same GUIDs for the calls (that is, it doesn't have to be "binary compatible"). This VB6 COM DLL uses parameterized properties in a few places, which I know aren't supported by C#.
When using a VB6 COM DLL with parameterized properties, the reference in C# will access them as methods in the form "get_PropName" and "set_PropName". However, I'm going in the opposite direction: I'm not trying to access the VB6 DLL in C#, I'm trying to make a C# COM DLL compatible with a VB6 DLL.
So, the question is: How do I make getter and setter methods in a C# COM DLL that appear as a single parameterized property when used by VB6?
For example, say the VB6 property is defined as follows:
Public Property Get MyProperty(Param1 As String, Param2 as String) As String
End Property
Public Property Let MyProperty(Param1 As String, Param2 As String, NewValue As String)
End Property
The equivalent in C# would be something like this:
public string get_MyProperty(string Param1, string Param2)
{
}
public void set_MyProperty(string Param1, string Param2, ref string NewValue)
{
}
So, how would I make those C# methods look like (and function like) a single parameterized property when used by VB6?
I tried creating two methods, one called "set_PropName" and the other "get_PropName", hoping it would figure out that they're supposed to be a single parameterized property when used by VB6, but that didn't work; they appeared as two different method calls from VB6.
I thought maybe some attributes needed to be applied to them in C# so that they'd be seen as a single parameterized property in COM and VB6, but I couldn't find any that seemed appropriate.
I also tried overloading the methods, removing "get_" and "set_", hoping it would see them as a single property, but that didn't work either. That one generated this error in VB6: "Property let procedure not defined and property get procedure did not return an object".
I'm almost positive that there should be a way of doing this, but I just can't seem to find it. Does anyone know how to do this?
Update:
I took Ben's advice and added an accessor class to see if this could solve my problem. However, now I'm running into another issue...
First, here's the COM interface I'm using:
[ComVisible(true),
Guid("94EC4909-5C60-4DF8-99AD-FEBC9208CE76"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ISystem
{
object get_RefInfo(string PropertyName, int index = 0, int subindex = 0);
void set_RefInfo(string PropertyName, int index = 0, int subindex = 0, object theValue);
RefInfoAccessor RefInfo { get; }
}
Here's the accessor class:
public class RefInfoAccessor
{
readonly ISystem mySys;
public RefInfoAccessor(ISystem sys)
{
this.mySys = sys;
}
public object this[string PropertyName, int index = 0, int subindex = 0]
{
get
{
return mySys.get_RefInfo(PropertyName, index, subindex);
}
set
{
mySys.set_RefInfo(PropertyName, index, subindex, value);
}
}
}
Here's the implementation:
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid(MySystem.ClassId)]
[ProgId("MyApp.System")]
public class MySystem : ISystem
{
internal const string ClassId = "60A84737-8E96-4DF3-A052-7CEB855EBEC8";
public MySystem()
{
_RefInfo = new RefInfoAccessor(this);
}
public object get_RefInfo(string PropertyName, int index = 0, int subindex = 0)
{
// External code does the actual work
return "Test";
}
public void set_RefInfo(string PropertyName, int index = 0, int subindex = 0, object theValue)
{
// External code does the actual work
}
private RefInfoAccessor _RefInfo;
public RefInfoAccessor RefInfo
{
get
{
return _RefInfo;
}
}
}
Here's what I'm doing to test this in VB6, but I get an error:
Set sys = CreateObject("MyApp.System")
' The following statement gets this error:
' "Wrong number of arguments or invalid property assignment"
s = sys.RefInfo("MyTestProperty", 0, 0)
However, this works:
Set sys = CreateObject("MyApp.System")
Set obj = sys.RefInfo
s = obj("MyTestProperty", 0, 0)
It appears that it's trying to use the parameters on the property itself and getting an error because the property has no parameters. If I reference the RefInfo property in its own object variable, then it applies the indexer properties correctly.
Any ideas on how to arrange this so that it knows to apply the parameters to the accessor's indexer, rather than attempting to apply it to the property?
Also, how do I do a +1? This is my first question on StackOverflow :-)
Update #2:
Just to see how it would work, I also tried the default value approach. Here's how the accessor looks now:
public class RefInfoAccessor
{
readonly ISystem mySys;
private int _index;
private int _subindex;
private string _propertyName;
public RefInfoAccessor(ISystem sys, string propertyName, int index, int subindex)
{
this.mySys = sys;
this._index = index;
this._subindex = subindex;
this._propertyName = propertyName;
}
[DispId(0)]
public object Value
{
get
{
return mySys.get_RefInfo(_propertyName, _index, _subindex);
}
set
{
mySys.set_RefInfo(_propertyName, _index, _subindex, value);
}
}
}
This works great for a "get". However, when I try setting the value, .NET flips out with the following error:
Managed Debugging Assistant 'FatalExecutionEngineError' has detected a
problem in 'blahblah.exe'.
Additional information: The runtime has encountered a fatal error. The
address of the error was at 0x734a60f4, on thread 0x1694. The error
code is 0xc0000005. This error may be a bug in the CLR or in the
unsafe or non-verifiable portions of user code. Common sources of this
bug include user marshaling errors for COM-interop or PInvoke, which
may corrupt the stack.
I'm assuming the problem is that .NET tried setting the value to the method, rather to the default property of the returned object, or something similar. If I add ".Value" to the set line, it works fine.
Update #3: Success!
I finally got this working. There's a few things to look for, however.
First, the default value of the accessor must return a scaler, not an object, like so:
public class RefInfoAccessor
{
readonly ISystem mySys;
private int _index;
private int _subindex;
private string _propertyName;
public RefInfoAccessor(ISystem sys, string propertyName, int index, int subindex)
{
this.mySys = sys;
this._index = index;
this._subindex = subindex;
this._propertyName = propertyName;
}
[DispId(0)]
public string Value // <== Can't be "object"
{
get
{
return mySys.get_RefInfo(_propertyName, _index, _subindex).ToString();
}
set
{
mySys.set_RefInfo(_propertyName, _index, _subindex, value);
}
}
}
Second, when using the accessor, you need to make the return type an object:
public object RefInfo(string PropertyName, int index = 0, int subindex = 0)
{
return new RefInfoAccessor(this,PropertyName,index,subindex);
}
This will make C# happy, since the default value is a COM thing (dispid 0) and not a C# thing, so C# expects a RefInfoAccessor to be returned, not a string. Since RefInfoAccessor can be coerced into an object, no compiler error.
When used in VB6, the following will now all work:
s = sys.RefInfo("MyProperty", 0, 0)
Debug.Print s
sys.RefInfo("MyProperty", 0, 0) = "Test" ' This now works!
s = sys.RefInfo("MyProperty", 0)
Debug.Print s
Many thanks to Ben for his help on this!
C# can do indexed properties, but these must be implemented using a helper class which has an indexer. This method will work with early-bound VB but not with late-bound VB:
using System;
class MyClass {
protected string get_MyProperty(string Param1, string Param2)
{
return "foo: " + Param1 + "; bar: " + Param2;
}
protected void set_MyProperty(string Param1, string Param2, string NewValue)
{
// nop
}
// Helper class
public class MyPropertyAccessor {
readonly MyClass myclass;
internal MyPropertyAccessor(MyClass m){
myclass = m;
}
public string this [string param1, string param2]{
get {
return myclass.get_MyProperty(param1, param2);
}
set {
myclass.set_MyProperty(param1, param2, value);
}
}
}
public readonly MyPropertyAccessor MyProperty;
public MyClass(){
MyProperty = new MyPropertyAccessor(this);
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var mc = new MyClass();
Console.WriteLine(mc.MyProperty["a", "b"]);
}
}
There is a tutorial here:
https://msdn.microsoft.com/en-us/library/aa288464(v=vs.71).aspx
Late-bound VB Workaround
This is a workaround which takes advantage of two facts about VB. One is that in the array index operator is the same as the function call operator - round brackets (parens). The other is that VB will allow us to omit the name of the default property.
Read-only Properties
If the property is get-only, you don't need to bother with this. Just use a function, and this will behave the same as array access for late-bound code.
Read-Write properties
Using the two facts above, we can see that these are equivalent in VB
// VB Syntax: PropName could either be an indexed property or a function
varName = obj.PropName(index1).Value
obj.PropName(index1).Value = varName
// But if Value is the default property of obj.PropName(index1)
// this is equivalent:
varName = obj.PropName(index1)
obj.PropName(index1) = varName
This means that instead of doing the this:
//Property => Object with Indexer
// C# syntax
obj.PropName[index1];
We can do this:
// C# syntax
obj.PropName(index1).Value
So here is the example code, with a single parameter.
class HasIndexedProperty {
protected string get_PropertyName(int index1){
// replace with your own implementation
return string.Format("PropertyName: {0}", index1);
}
protected void set_PropertyName(int index1, string v){
// this is an example - put your implementation here
}
// This line provides the indexed property name as a function.
public string PropertyName(int index1){
return new HasIndexedProperty_PropertyName(this, index1);
}
public class HasIndexedProperty_PropertyName{
protected HasIndexedProperty _owner;
protected int _index1;
internal HasIndexedProperty_PropertyName(
HasIndexedProperty owner, int index1){
_owner = owner; _index1 = index1;
}
// This line makes the property Value the default
[DispId(0)]
public string Value{
get {
return _owner.get_PropertyName(_index1);
}
set {
_owner.set_PropertyName(_index1, value);
}
}
}
}
Limitations
The limitation is that to work, this depends on the call being made in a context where the result is coerced to a non-object type. For example
varName = obj.PropName(99)
Since the Set keyword was not used, VB knows that it must get the default property for use here.
Again, when passing to a function which takes for example a string, this will work. Internally VariantChangeType will be called to convert the object to the correct type, which if coercing to a non-object will access the default property.
The problem may occur when passing directly as a parameter to a function which takes a Variant as an argument. In this case the accessor object will be passed. As soon as the object is used in a non-object context (e.g. an assignment or conversion to string) the default property will be fetched. However this will be the value at the time it is converted, not the time it was originally accessed. This may or may not be an issue.
This issue can be worked around however by having the accessor object cache the value it returns to ensure it is the value as at the time the accessor was created.
This feature you seek is usually called "indexed properties". The flavor that VB6 uses is the flavor supported by COM interfaces.
This IDL fragment is similar to what VB6 would generate, and shows what's going on under the hood:
interface ISomething : IDispatch {
[id(0x68030001), propget]
HRESULT IndexedProp(
[in, out] BSTR* a, // Index 1
[in, out] BSTR* b, // Index 2
[out, retval] BSTR* );
[id(0x68030001), propput]
HRESULT IndexedProp(
[in, out] BSTR* a, // Index 1
[in, out] BSTR* b, // Index 2
[in, out] BSTR* );
[id(0x68030000), propget]
HRESULT PlainProp(
[out, retval] BSTR* );
[id(0x68030000), propput]
HRESULT PlainProp(
[in, out] BSTR* );
};
IndexedProp is a String property that takes two String parameters as indices. Contrast with PlainProp, which is of course a non-indexed conventional property.
Unfortunately, C# has very limited support for COM-style indexed properties.
C# 4.0 supports consuming COM objects (written elsewhere) that implement a COM interface with indexed properties. This was added to improve interoperability with COM Automation servers like Excel. However, it doesn't support declaring such an interface, or creating an object that implements such a COM interface even if legally declared elsewhere.
Ben's answer tells you how to create indexed properties in C# - or at least something that results in an equivalent syntax in C# code. If you just want the syntax flavor while writing C# code, that works great. But of course it's not a COM-style indexed property.
This is a limitation of the C# language, not the .NET platform. VB.NET does support COM indexed properties, because they had the mandate to replace VB6 and therefore needed to go the extra mile.
If you really want COM indexed properties, you could consider writing the COM version of your object in VB.NET, and have that object forward calls to your C# implementation. It sounds like a lot of work to me. Or port all your code to VB.NET. It really depends on how badly do you want it.
References
C# Team Blog: FAQ on new features in C# 4.0:
But this feature is available only for COM interop; you cannot create your own indexed properties in C# 4.0.
Why C# doesn't implement indexed properties?
Eric Lippert answers
COM-style Indexed Properties in VB.NET: Property with parameter

Change value inside an (void) extension method

So I have this mock extension method which change a value to another value:
public static void ChangeValue(this int value, int valueToChange)
{
value = valueToChange;
}
When I try using it:
int asd = 8;
asd.ChangeValue(10);
Debug.Log(asd);
It returns 8 instead of 10.
While the value did change inside the ChangeValue method, it didn't change the value of "asd". What do I need to add to the method, to make it update "asd"?
You can't do that without using either a return value, or a ref parameter. The latter doesn't work alongside this (extension methods), so your best bet is a return value (rather than void).
The old answer is not valid anymore since newer C# versions support this ref. For further details refer to this answer.
Old Answer:
int is a struct so it's a value-type. this means that they are passed by value not by reference. Classes are reference-types and they act differently they are passed by reference.
Your option is to create static method like this:
public static void ChangeValue(ref int value, int valueToChange)
{
value = valueToChange;
}
and use it:
int a = 10;
ChangeValue(ref a, 15);
Old question, but on newer versions of C# it looks like you can now do this for value types by using the this and ref keywords together. This will set value to be the same as valueToChange, even outside of this extension method.
public static void ChangeValue(this ref int value, int valueToChange)
{
value = valueToChange;
}
I believe this change was made to the compiler on version 15.1, which I think corresponds to C# 7 (or one of its sub versions). I did not immediately find a formal announcement of this feature.
According to this answer: https://stackoverflow.com/a/1259307/1945651, there is not a way to do this in C#. Primitive types like int are immutable, and cannot be modified without an out or ref modifier, but the syntax won't allow out or ref here.
I think your best case is to have the extension method return the modified value instead of trying to modify the original.
Apparently this is possible in VB.NET and if you absolutely needed it, you could define your extension method in a VB.NET assembly, but it is probably not a very good practice in the first place.
I know it's too late, but just for the record, I recently really wanted to do this, I mean...
someVariable.ChangeValue(10);
...apparently looks way neat than the following (which is also perfectly fine)
ChangeValue(ref someVariable, 10);
And I managed to achieve something similar by doing:
public class MyClass
{
public int ID { get; set; }
public int Name { get; set; }
}
public static void UpdateStuff(this MyClass target, int id, string name)
{
target.ID = id;
target.Name = name;
}
static void Main(string[] args)
{
var someObj = new MyClass();
someObj.UpdateStuff(301, "RandomUser002");
}
Note that if the argument passed is of reference type, it needs to be instantiated first (but not inside the extension method). Otherwise, Leri's solution should work.
Because int is value type, so it copied by value when you pass it inside a function.
To see the changes outside of the function rewrite it like:
public static int ChangeValue(this int value, int valueToChange)
{
//DO SOMETHING ;
return _value_; //RETURN COMPUTED VALUE
}
It would be possible to do using ref keyowrd, but it can not be applied on parameter with this, so in your case, just return resulting value.

How can you use optional parameters in C#?

Note: This question was asked at a time when C# did not yet support optional parameters (i.e. before C# 4).
We're building a web API that's programmatically generated from a C# class. The class has method GetFooBar(int a, int b) and the API has a method GetFooBar taking query params like &a=foo &b=bar.
The classes needs to support optional parameters, which isn't supported in C# the language. What's the best approach?
Surprised no one mentioned C# 4.0 optional parameters that work like this:
public void SomeMethod(int a, int b = 0)
{
//some code
}
Edit: I know that at the time the question was asked, C# 4.0 didn't exist. But this question still ranks #1 in Google for "C# optional arguments" so I thought - this answer worth being here. Sorry.
Another option is to use the params keyword
public void DoSomething(params object[] theObjects)
{
foreach(object o in theObjects)
{
// Something with the Objects…
}
}
Called like...
DoSomething(this, that, theOther);
In C#, I would normally use multiple forms of the method:
void GetFooBar(int a) { int defaultBValue; GetFooBar(a, defaultBValue); }
void GetFooBar(int a, int b)
{
// whatever here
}
UPDATE: This mentioned above WAS the way that I did default values with C# 2.0. The projects I'm working on now are using C# 4.0 which now directly supports optional parameters. Here is an example I just used in my own code:
public EDIDocument ApplyEDIEnvelop(EDIVanInfo sender,
EDIVanInfo receiver,
EDIDocumentInfo info,
EDIDocumentType type
= new EDIDocumentType(EDIDocTypes.X12_814),
bool Production = false)
{
// My code is here
}
From this site:
https://www.tek-tips.com/viewthread.cfm?qid=1500861
C# does allow the use of the [Optional] attribute (from VB, though not functional in C#). So you can have a method like this:
using System.Runtime.InteropServices;
public void Foo(int a, int b, [Optional] int c)
{
...
}
In our API wrapper, we detect optional parameters (ParameterInfo p.IsOptional) and set a default value. The goal is to mark parameters as optional without resorting to kludges like having "optional" in the parameter name.
You could use method overloading...
GetFooBar()
GetFooBar(int a)
GetFooBar(int a, int b)
It depends on the method signatures, the example I gave is missing the "int b" only method because it would have the same signature as the "int a" method.
You could use Nullable types...
GetFooBar(int? a, int? b)
You could then check, using a.HasValue, to see if a parameter has been set.
Another option would be to use a 'params' parameter.
GetFooBar(params object[] args)
If you wanted to go with named parameters would would need to create a type to handle them, although I think there is already something like this for web apps.
You can use optional parameters in C# 4.0 without any worries.
If we have a method like:
int MyMetod(int param1, int param2, int param3=10, int param4=20){....}
when you call the method, you can skip parameters like this:
int variab = MyMethod(param3:50; param1:10);
C# 4.0 implements a feature called "named parameters", you can actually pass parameters by their names, and of course you can pass parameters in whatever order you want :)
An easy way which allows you to omit any parameters in any position, is taking advantage of nullable types as follows:
public void PrintValues(int? a = null, int? b = null, float? c = null, string s = "")
{
if(a.HasValue)
Console.Write(a);
else
Console.Write("-");
if(b.HasValue)
Console.Write(b);
else
Console.Write("-");
if(c.HasValue)
Console.Write(c);
else
Console.Write("-");
if(string.IsNullOrEmpty(s)) // Different check for strings
Console.Write(s);
else
Console.Write("-");
}
Strings are already nullable types so they don't need the ?.
Once you have this method, the following calls are all valid:
PrintValues (1, 2, 2.2f);
PrintValues (1, c: 1.2f);
PrintValues(b:100);
PrintValues (c: 1.2f, s: "hello");
PrintValues();
When you define a method that way you have the freedom to set just the parameters you want by naming them. See the following link for more information on named and optional parameters:
Named and Optional Arguments (C# Programming Guide) # MSDN
Hello Optional World
If you want the runtime to supply a default parameter value, you have to use reflection to make the call. Not as nice as the other suggestions for this question, but compatible with VB.NET.
using System;
using System.Runtime.InteropServices;
using System.Reflection;
namespace ConsoleApplication1
{
public class Class1
{
public static void SayHelloTo([Optional, DefaultParameterValue("world")] string whom)
{
Console.WriteLine("Hello " + whom);
}
[STAThread]
public static void Main(string[] args)
{
MethodInfo mi = typeof(Class1).GetMethod("sayHelloTo");
mi.Invoke(null, new Object[] { Missing.Value });
}
}
}
I agree with stephenbayer. But since it is a webservice, it is easier for end-user to use just one form of the webmethod, than using multiple versions of the same method. I think in this situation Nullable Types are perfect for optional parameters.
public void Foo(int a, int b, int? c)
{
if(c.HasValue)
{
// do something with a,b and c
}
else
{
// do something with a and b only
}
}
optional parameters are for methods. if you need optional arguments for a class and you are:
using c# 4.0: use optional arguments in the constructor of the class, a solution i prefer, since it's closer to what is done with methods, so easier to remember. here's an example:
class myClass
{
public myClass(int myInt = 1, string myString =
"wow, this is cool: i can have a default string")
{
// do something here if needed
}
}
using c# versions previous to c#4.0: you should use constructor chaining (using the :this keyword), where simpler constructors lead to a "master constructor".
example:
class myClass
{
public myClass()
{
// this is the default constructor
}
public myClass(int myInt)
: this(myInt, "whatever")
{
// do something here if needed
}
public myClass(string myString)
: this(0, myString)
{
// do something here if needed
}
public myClass(int myInt, string myString)
{
// do something here if needed - this is the master constructor
}
}
The typical way this is handled in C# as stephen mentioned is to overload the method. By creating multiple versions of the method with different parameters you effectively create optional parameters. In the forms with fewer parameters you would typically call the form of the method with all of the parameters setting your default values in the call to that method.
Using overloads or using C# 4.0 or above
private void GetVal(string sName, int sRoll)
{
if (sRoll > 0)
{
// do some work
}
}
private void GetVal(string sName)
{
GetVal("testing", 0);
}
You can overload your method. One method contains one parameter GetFooBar(int a) and the other contain both parameters, GetFooBar(int a, int b)
You can use default.
public void OptionalParameters(int requerid, int optinal = default){}
For a larger number of optional parameters, a single parameter of Dictionary<string,Object> could be used with the ContainsKey method. I like this approach because it allows me to pass a List<T> or a T individually without having to create a whole other method (nice if parameters are to be used as filters, for example).
Example (new Dictionary<string,Object>() would be passed if no optional parameters are desired):
public bool Method(string ParamA, Dictionary<string,Object> AddlParams) {
if(ParamA == "Alpha" && (AddlParams.ContainsKey("foo") || AddlParams.ContainsKey("bar"))) {
return true;
} else {
return false;
}
}
Instead of default parameters, why not just construct a dictionary class from the querystring passed .. an implementation that is almost identical to the way asp.net forms work with querystrings.
i.e. Request.QueryString["a"]
This will decouple the leaf class from the factory / boilerplate code.
You also might want to check out Web Services with ASP.NET. Web services are a web api generated automatically via attributes on C# classes.
A little late to the party, but I was looking for the answer to this question and ultimately figured out yet another way to do this. Declare the data types for the optional args of your web method to be type XmlNode. If the optional arg is omitted this will be set to null, and if it's present you can get is string value by calling arg.Value, i.e.,
[WebMethod]
public string Foo(string arg1, XmlNode optarg2)
{
string arg2 = "";
if (optarg2 != null)
{
arg2 = optarg2.Value;
}
... etc
}
What's also decent about this approach is the .NET generated home page for the ws still shows the argument list (though you do lose the handy text entry boxes for testing).
I have a web service to write that takes 7 parameters. Each is an optional query attribute to a sql statement wrapped by this web service. So two workarounds to non-optional params come to mind... both pretty poor:
method1(param1, param2, param 3, param 4, param 5, param 6, param7)
method1(param1, param2, param3, param 4, param5, param 6)
method 1(param1, param2, param3, param4, param5, param7)... start to see the picture. This way lies madness. Way too many combinations.
Now for a simpler way that looks awkward but should work:
method1(param1, bool useParam1, param2, bool useParam2, etc...)
That's one method call, values for all parameters are required, and it will handle each case inside it. It's also clear how to use it from the interface.
It's a hack, but it will work.
I had to do this in a VB.Net 2.0 Web Service. I ended up specifying the parameters as strings, then converting them to whatever I needed. An optional parameter was specified with an empty string. Not the cleanest solution, but it worked. Just be careful that you catch all the exceptions that can occur.
For just in case if someone wants to pass a callback (or delegate) as an optional parameter, can do it this way.
Optional Callback parameter:
public static bool IsOnlyOneElement(this IList lst, Action callbackOnTrue = (Action)((null)), Action callbackOnFalse = (Action)((null)))
{
var isOnlyOne = lst.Count == 1;
if (isOnlyOne && callbackOnTrue != null) callbackOnTrue();
if (!isOnlyOne && callbackOnFalse != null) callbackOnFalse();
return isOnlyOne;
}
optional parameters are nothing but default parameters!
i suggest you give both of them default parameters.
GetFooBar(int a=0, int b=0) if you don't have any overloaded method, will result in a=0, b=0 if you don't pass any values,if you pass 1 value, will result in, passed value for a, 0 and if you pass 2 values 1st will be assigned to a and second to b.
hope that answers your question.
In the case when default values aren't available the way to add an optional parameter is to use .NET OptionalAttribute class - https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.optionalattribute?view=netframework-4.8
Example of the code is below:
namespace OptionalParameterWithOptionalAttribute
{
class Program
{
static void Main(string[] args)
{
//Calling the helper method Hello only with required parameters
Hello("Vardenis", "Pavardenis");
//Calling the helper method Hello with required and optional parameters
Hello("Vardenis", "Pavardenis", "Palanga");
}
public static void Hello(string firstName, string secondName,
[System.Runtime.InteropServices.OptionalAttribute] string fromCity)
{
string result = firstName + " " + secondName;
if (fromCity != null)
{
result += " from " + fromCity;
}
Console.WriteLine("Hello " + result);
}
}
}
You can try this too
Type 1
public void YourMethod(int a=0, int b = 0)
{
//some code
}
Type 2
public void YourMethod(int? a, int? b)
{
//some code
}

Categories