7 Replies - 2092 Views - Last Post: 30 August 2008 - 12:09 PM Rate Topic: -----

#1 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Optional Args

Posted 30 August 2008 - 08:04 AM

I am trying to make a gradient background for my form. I was wondering how do you make some of the args optional.

I know that in VB.NET you can do this
	Private Sub someSub(Optional ByVal TrueOrFalse As Boolean = False)

	End Sub



Now how would you do that in C#?
Is This A Good Question/Topic? 0
  • +

Replies To: Optional Args

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Optional Args

Posted 30 August 2008 - 08:10 AM

C# doesn't allow optional parameters/arguments. The alternative would be to use Overloaded methods. If you think about it, a method with an optional argument is actually two different methods to begin with, so C# left that option out
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Optional Args

Posted 30 August 2008 - 08:13 AM

C# doesn't have this functionality. Overloaded methods can get you the same functionality though.

private void Optional()
{
      OptionalTest(string.Empty);
}

private void Optional(string test)
{

}


Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Optional Args

Posted 30 August 2008 - 08:21 AM

hmmm straight from http://tangiblesoftw...0Parameters.htm eh eclipsed4utoo? :P
Was This Post Helpful? 0
  • +
  • -

#5 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Optional Args

Posted 30 August 2008 - 08:30 AM

Ok Thanks Guys
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Optional Args

Posted 30 August 2008 - 09:46 AM

View PostPsychoCoder, on 30 Aug, 2008 - 11:21 AM, said:

hmmm straight from http://tangiblesoftw...0Parameters.htm eh eclipsed4utoo? :P


:ph34r:

I was going to say that optional parameters didn't exist in C#, but I wanted to make sure before I posted it. So I did a quick search and that was the site that I saw. B)
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Optional Args

Posted 30 August 2008 - 10:29 AM

I agree that overloading methods is what probably you're looking for.

However, in the interests of full disclosure, there is an optional parameters syntax. The keyword is params and this guy did a pretty good writeup on it.
Was This Post Helpful? 0
  • +
  • -

#8 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Optional Args

Posted 30 August 2008 - 12:09 PM

Well, technically there is a way in C#. Here is some function in VB.NET with an optional value:
    Public Sub OptionalThingy(Optional ByVal SomeParam As Integer = 5345342)

    End Sub


Would be the equivilent of this in C#: (using System.Runtime.InteropServices)
        public void OptionalThingy([Optional, DefaultParameterValue(0x51903e)] int SomeParam)
        {

        }


However, C# won't recognize it as optional, so you'll have to pass it a value anyway.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1