Specifiy Optional Parameters

You'll have to make 2 version of your function (overloading)

One that take N number of parameter... and the other 0 parameter and that will call the first one with default value.

There's no way to specify a Optional parameter as in VB.NET. Optional is language specific to VB and is not in OOP definition (i don't think).
 
Arch4ngel said:
You'll have to make 2 version of your function (overloading)



One that take N number of parameter... and the other 0 parameter and that will call the first one with default value.



There's no way to specify a Optional parameter as in VB.NET. Optional is language specific to VB and is not in OOP definition (i don't think).
and there are ways around this, too. . .

this might seem like alot, but it is actually very useful.



Refer to EventArgs as an Idiom. . . in fact, you could even derive from EventArgs. . .


PHP:
 public class MyArgs

 {
  int _arg1 = null;
  string _arg2 = null;
  
  public MyArgs(int arg1, string arg2)
  {
   _arg1 = arg1;
   _arg2 = arg2;
  }

  public MyArgs(int arg1)
  {
   _arg1 = arg1;
  }

  public MyArgs(string arg2)
  {
   _arg2 = arg2;
  }

  public int Arg1
  {
   get
   {
	return _arg1;
   }
   set
   {
	_arg1 = value;
   }
  }

  public int Arg2
  {
   get
   {
	return _arg2;
   }
   set
   {
	_arg21 = value;
   }
  }
  public static MyArgs Empty
  {
   get
   {
	return  null;
   }
  }

 }
 

 public class MyProcClass
 {
  public void MyProc(MyArgs args)
  {
   if (args == null)
   {
	Console.WriteLine("no args specified");
	return;
   }
   else
   {
	if (args.Arg1 != null)
	 Console.WriteLine("optional arg1 found");
	if (args.Arg2 != null)
	 Console.WriteLine("optional arg2 found");
   }

  }


  public void CallMyProcExample()
  {
   MyProc(MyArgs.Empty);
   MyProc(new MyArgs("Only arg2 defined"));
   MyProc(new MyArgs(1));
   MyProc(new MyArgs(1, "Both Defined"));
  }
 }
 
Last edited:
Back
Top