opional Parameter

G

Guest

We are migrating the vb6 project to C#.Net 2005.
In this migration process we are facing one big issue with optional
parameter. Actually in C#.Net there is not optional parameter concept . For
that C# introduced 2 concepts like 1)overloading 2)params .
But problem is
1) I am not able to see access 0these overloading methods in vb6(for eg:
NewUser(string a), NewUser(string a, string b)). In vb6 its showing the
method name like(NewUser and NewUser_1)
2)If i use Params string[] concept: Not able to access/ see this in vb6.
giving error like vb6 doesn't support this method.

I Would appreciate if any one helps me in resolving this issue.

Regards,
Sriman
 
S

sloan

are you talking about constructors or methods?

either way, here is a sample


public class Employee
{

private readonly int DEFAULT_WORK_WEEK_HOURS = 40;

private string _ssn = string.Empty;

public Employee( string SSN )
{
this._ssn = SSN;
}

public Employee() : this ("000-00-0000")
{

}

//methods

public int DetermineWage ( int weeklyHours , int payPerHour )
{
return weeklyHours * payPerHour;
}

public int DetermineWage ( int payPerHour )
{
return this.DetermineWage ( DEFAULT_WORK_WEEK_HOURS , payPerHour );
}


}


There are some clues. If you want an "optional" method, you overload the
method, and call the (most parameterized) method with default values.

Constructors, you can use the "this( parameter1, parameter2)" ... to get the
same type of behavior. (Seen above as this("000-00-0000")


You can also check the
nullable
HasValue
keywords, and use nullable types as optional parameters.
(2.0 or greater specific)
 
P

Peter Duniho

[...]
There are some clues. If you want an "optional" method, you overload the
method, and call the (most parameterized) method with default values.

Constructors, you can use the "this( parameter1, parameter2)" ... to get the
same type of behavior. (Seen above as this("000-00-0000")

From the original post, it appears to me that dealing with the issue in
C# isn't actually the problem, but rather how to get at the overloaded
methods from VB.NET.

I don't actually know the answer. I think it's possible a better
answer might be obtained in a VB group since, after all, this is really
more a question about writing VB code than about writing C# code
(though obviously there's some overlap). But if VB doesn't itself
support overloading, I suppose it's possible that there is no better
solution than to use the method names the OP mentioned, at least by
calling the method directly.

Of course, an alternative might be to wrap the calls in VB code that
does have the optional parameter, calling a single C# method with the
full parameter list. That might be better than trying to shoehorn the
method overloads into a language that simply doesn't understand the
concept.

Pete
 
C

Chris Shepherd

Sriman said:
We are migrating the vb6 project to C#.Net 2005.
In this migration process we are facing one big issue with optional
parameter. Actually in C#.Net there is not optional parameter concept . For
that C# introduced 2 concepts like 1)overloading 2)params .
But problem is
1) I am not able to see access 0these overloading methods in vb6(for eg:
NewUser(string a), NewUser(string a, string b)). In vb6 its showing the
method name like(NewUser and NewUser_1)
2)If i use Params string[] concept: Not able to access/ see this in vb6.
giving error like vb6 doesn't support this method.

This is the reverse of the Interop thing that C# deals with. VB doesn't
support overloading, instead it supports optional parameters (C# is the
reverse).

Is there a possibility you could use Invoke to call the method?

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top