Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,381 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,203 people online right now. Registration is fast and FREE... Join Now!




Optional Args

 
Reply to this topicStart new topic

Optional Args

gbertoli3
post 30 Aug, 2008 - 07:04 AM
Post #1


DIC at Heart + Code

Group Icon
Joined: 23 Jun, 2008
Posts: 1,026



Thanked 16 times

Dream Kudos: 950
My Contributions


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
CODE

    Private Sub someSub(Optional ByVal TrueOrFalse As Boolean = False)

    End Sub


Now how would you do that in C#?
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 30 Aug, 2008 - 07:10 AM
Post #2


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,923



Thanked 118 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 30 Aug, 2008 - 07:13 AM
Post #3


D.I.C Regular

Group Icon
Joined: 21 Mar, 2008
Posts: 314



Thanked 17 times

Dream Kudos: 25
My Contributions


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

c#

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

private void Optional(string test)
{

}
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 30 Aug, 2008 - 07:21 AM
Post #4


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,923



Thanked 118 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


hmmm straight from http://tangiblesoftwaresolutions.com/Artic...0Parameters.htm eh eclipsed4utoo? tongue.gif
User is offlineProfile CardPM

Go to the top of the page

gbertoli3
post 30 Aug, 2008 - 07:30 AM
Post #5


DIC at Heart + Code

Group Icon
Joined: 23 Jun, 2008
Posts: 1,026



Thanked 16 times

Dream Kudos: 950
My Contributions


Ok Thanks Guys
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 30 Aug, 2008 - 08:46 AM
Post #6


D.I.C Regular

Group Icon
Joined: 21 Mar, 2008
Posts: 314



Thanked 17 times

Dream Kudos: 25
My Contributions


QUOTE(PsychoCoder @ 30 Aug, 2008 - 11:21 AM) *


ph34r.gif

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. cool.gif
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 30 Aug, 2008 - 09:29 AM
Post #7


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,962



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

jacobjordan
post 30 Aug, 2008 - 11:09 AM
Post #8


class Me : Perfection

Group Icon
Joined: 11 Jun, 2008
Posts: 1,105



Thanked 22 times

Dream Kudos: 1575
My Contributions


Well, technically there is a way in C#. Here is some function in VB.NET with an optional value:
vb

Public Sub OptionalThingy(Optional ByVal SomeParam As Integer = 5345342)

End Sub

Would be the equivilent of this in C#: (using System.Runtime.InteropServices)
csharp

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.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 06:25AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month