Convert Optional Into C#?

Jun 26, 2012

I'm trying to convert come vb.net code into C# from another programmers old work but came across optional in one of the functions.[code]...

It seems like instead of using overloading, VB.Net has an option to create it into one method/function. Does C# have a similar equilvalent or do I have to create a method for each possbility?

View 3 Replies


ADVERTISEMENT

Optional Date Value In One Of My Sub - Handle The Default Value For An Optional Date?

Mar 6, 2010

I am trying to have an optional Date value in one of my sub but since you cant set Date to nothing, this doesn't work. And i cant set it to the Date.minvalue inline.

Private Sub abc (ByVal A As String, Optional ByVal B As Date = Nothing)
End Sub

So, i went and set it to some date in the past.

Private Sub abc (ByVal A As String, Optional ByVal B As Date = #1/1/2001#)
End Sub

what would be the proper way to handle the default value for an optional Date?

View 3 Replies

Using Optional Third Party DLL?

Dec 21, 2009

I have a third party dll that contain a class, SampleClass1

Public Class SampleClass1
Public SomeValue As Double
Public Function Test() As Double

[code]....

My intention is if the dll is found, use it. Else ignore it. However, if the dll is not found, the program throw an Invalid OperationException.

View 7 Replies

.net - Optional Where Clause In Query?

Jan 27, 2010

I have an application that does a search of our database (exposed via EF) for records meeting certain conditions. We have two main tables (Jobs and Recipients). Recipients are linked to the Jobs table by a Job ID.The job record has various fields (Process Date, NameJobType). The recipient has a lot of Name and ID fields (e.g. Account Number, Surname, etc)

View 1 Replies

Adding New Optional Node On XML?

Oct 15, 2009

I currently have this XML schema :

<PSC5>
<POI_ORI>
<CIT>LIM</CIT>
</POI_ORI>
</PSC5>

if user want to add a new option, the final schema will be :

[Code]...

View 3 Replies

Isnothing(foo) And Optional Args?

Mar 30, 2012

I'm looking at an old module to add some new features. I noticed this in a subroutine declaration:sub foo(...,optional fum as integer = 0)the later if not isnothing(fum) then <do something>I'm wondering, will <do something> ever be executed? I'm guessing not, since fum has a default value, but I want to be sure I'm getting this correctly.

View 12 Replies

Optional Structures As Arguments?

Mar 16, 2011

It says that I can't have structures as optional arguments.That's really annoying for my program. Isn't there any way to circumvent this?

Sub fa(Optional ByVal colz As System.Drawing.Color = Color.AliceBlue)
End Sub

This example cannot compile

View 14 Replies

Optional Where Clause In Query

Apr 25, 2012

I have an application that does a search of our database (exposed via EF) for records meeting certain conditions. We have two main tables (Jobs and Recipients). Recipients are linked to the Jobs table by a Job ID.The job record has various fields (Process Date, Name, JobType). The recipient has a lot of Name and ID fields (e.g. Account Number, Surname, etc)I want to present a search screen where they see a list of fields to search on. The ASP.net code then sees which textboxes the user typed in (or selected), and builds a query based on that. [code] Where I'm stuck is figuring out how to add in additional where clauses. Should I just use Entity SQL?Can this return strongly typed EF objects as well? And is it possible to return both the Jobs and Recipients objects?

View 1 Replies

.net - Call Method From C# With Optional Arguments?

Mar 3, 2010

I have a method written in VB.NET. It looks like this:

Shared Sub SomeMethod(ByVal Id As Guid,
Optional ByVal str1 As String = "foo",
Optional ByVal str2 As String = "")

I want to call this method from C# 3.0 and I want it to use its default arguments. I tried passing System.Reflection.Missing.Value, but I cannot cast it as String. Is there any way to do that?

View 3 Replies

.net - Utilize Optional Parameters In Delegates?

Aug 6, 2010

However, I am wondering if there are any workarounds or plans for incorporating this feature into VB.NET in the future?What I'd like to do:

Public Delegate Function Deserializer(Of T)(ByRef Buffer() As Byte, optional ByRef BufferPosition As Integer = 0) As T
'Implementation of a func that matches the delegate'
Class A
Public Function Deserialize(Byref Buffer() as Byte, optional Byref BufferPosition as integer = 0)
....

In the absence of specifying "optional" inside the actual delegate itself, it'd at least be nice to be able to do it in the function implementation only:

Public Delegate Function Deserializer(Of T)(ByRef Buffer() As Byte, ByRef BufferPosition As Integer) As T
'Implementation of a func that matches the delegate'
Class A
Public Function Deserialize(Byref Buffer() as Byte, optional Byref BufferPosition as integer = 0)
....

At least this second way, the functions for the delegate will always have a value mapped to each parameter, although some may come from the function side and not the calling side.

View 2 Replies

Add A Optional Parameter In Visual C# Function?

Mar 14, 2012

How can i create a optional parameter in C# as we create in VB.Net

Public Sub Demo(ByVal a As Integer,Optional ByVal b as integer=3)
End Sub
I want to declare this in C#

[code].....

View 2 Replies

Asp.net - Get By Reflection The Value Of A Property Whose Getter Has An Optional Value

Feb 23, 2012

I am retrieving several properties of a control. Here is how I used to retrieve properties (with pinfo of type PropertyInfo):

value = pinfo.GetValue(obj, nothing)

That worked well, but now I am facing a property that has a optional value, and I get an error message telling me that the number of parameters is incorrect. So I changed my code by this one:

Dim index As Object() = {Nothing}
value = pinfo.GetValue(obj, index)

At this point, I didn't get any error message, but this code doesn't retrieve the good value. It only works if I replace Nothing by the default value provided by the property accessor...

But I don't know in advance what this default value is! And this code is within a function that retrieves properties that doesn't have optional values, so I cannot change the code especially for one case or another..

I am working on .NET 2.0

EDIT: More precisions about the case leading to the problem

Here is an example of property leading to the problem:

ReadOnly Property Foo(Optional ByVal Number As Integer = -1) As String
Get
If Number = -1 Then

[Code]....

With this kind of property, none of the codes above retrieve the good string.

My best guess would be to try the first code for general purposes, catch the appropriate exception, and then dynamically retrieve the default value of the parameter (Number in that case) and its type, so that I can call getValue with this default value.

So, How can I retrieve the default value of the optional parameter?

View 2 Replies

Asp.net 1.1 - Assign Null Value To Optional Parameters In .NET 1.1?

Dec 21, 2010

I tried DbNull.Value but no luck. How do I assign a default value as null to a string parameter that is null in VB.NET? Its litte strange to see that VB does not have anything like plain null as most of the other languages do. Also what is the difference between null and DbNull and Nothing.

View 3 Replies

C# - Handling Optional .NET DataSource Parameters?

Aug 9, 2011

What is the standard way to implement optional query parameters in a .NET WinForms application?In other words, only query on a field if the value of a corresponding control is not null.

EDIT: I use a FillBy method which calls a query in my Access database. In the TableAdapter query editor, I just used WHERE (field1 = ?) AND (field2 = ?) ... I just can't find the "hook" to bind form controls to the table adapter query parameters and so that if a form uses the default value to not query on it.

View 2 Replies

Error : Optional Parameters Must Specify A Default Value

Jan 21, 2011

Public Function Foo(ByRef a As AClass, _
Optional ByRef b As BClass = Nothing, _
Optional ByRef c As CClass = Nothing) As XClass

Error : Optional parameters must specify a default value.

View 2 Replies

Have An Optional Parameter Of The Type List?

Sep 25, 2009

Is it possible to have an optional parameter of the type list?Ex.

Private Function TestFunction(Optional ByRef MyList As List(Of Double))

The real problem is giving the list a default value. When dimming a new list, you can use the code below to give it a starting value:

Dim MyList As New List(Of Double)(New Double() {1, 2, 3, 4, 5})

But even after trying many variations of the above code, I cannot get it to work as an optional parameter.

View 16 Replies

Insert Data To Optional Fields?

Feb 27, 2012

I have some fix fields in my database but user can add some another fields to my database if s/he needs. How can I insert data to this kind of fields if there are? For fixed fields I have used the follow cods

Dim rs
As New OleDb.OleDbCommand(
"INSERT INTO Wind_Parameters (ID, Name, CrX, CrY) VALUES ('" &
ID &

[Code]....

But I can't add optional fields to this code. I think I should use UPDATE syntax for fill optional fields after running this cod but I couldn't find correct syntax for it in visual basic 2010

View 8 Replies

Optional Fields In InfoPath; Getting The XML Node With VB

Apr 12, 2010

I use VB to get data through my form. I have some optional fields in my form and I have a problem with the following code:

MsgBox(myXPathNavigator.SelectSingleNode( _
"/my:Status/my:Questions/my:Questions1", Me.NamespaceManager _
).IsNode.ToString)

When the optional field 'Questions1' is inserted into the form I get the value 'true' by the IsNode() function.

If the field it is not inserted I have an exception stating that the reference is not correct (and it is indeed true). Is there a way to verify about a node, whether it is present or not in my form?

View 1 Replies

Optional Parameters Cannot Have Structure Types?

Jan 12, 2010

I would just like to know why. optional parameters cannot have structure types?

View 4 Replies

Optional Readonly Property In Interface

Aug 25, 2009

I am trying to develop a simple interface for allowing quick lists to be generated from classes. Basically, the interface needs to return an ID and a Name. However, some classes have a calculated name property which is read only, others just use a read/write name property. Basically, all I care is that it has a getter, it does not matter if the property has a setter. How can I write this interface to handle either or without throwing compile errors?

View 1 Replies

Passing An Optional Parameter To A Webservice

Nov 1, 2010

Is there a way to pass an optional parameter to a webservice, instead of having to overload the method?If the webservice user is accessing the webservice directly, I want to do ActionA, if the user is accessing the webservice through my web interface, I want to do ActionA + ActionB.

View 1 Replies

Possible To Make A Node Optional In XML Literals?

Nov 22, 2011

Dim task As XElement = <task>
<body>body</body>
<optional><%= myVar %></optional>

[code].....

View 1 Replies

Run-time Error '449': Argument Not Optional

Jun 30, 2009

I am getting this error when debugging a VB application. The method that is being called when I get this does not have any arguments. Definitions in code are as follows with names changed to protect the guilty

[Code]...

The question is, if the method is not defined to accept argments, then why am I having a debug assertion while debugging that says that the arguments are not optional?

View 15 Replies

RunTim Error '449': Argument Not Optional

Oct 12, 2010

I have an Image.OCX which is build on 2008. This is used by VB Application.Many OCX methods are not used in Application. I want to delete unused method from OCX.Step for method deletion.

Delete exists Method from .ODL File i.e //[id(181), helpcontext(11)] long CreateImage(ID id);.
Delete
CreateImage same function . Delete //DISP_FUNCTION(CImageCtrl, "CreateImage", CreateImage, VT_I4) from Ctrl.cpp.
Delete interface ID from AFX_DISP_ID.

[code]....

Following Error Message display "Run Tim error '449':Argument not optional."

let me know is that possible to delete exist method from OCX and How?

View 3 Replies

Set An Optional Byte Array In Function

May 2, 2011

I can't figure out the syntax for having an optional byte array in a .net sub Public SubMy Function(ByValFilename as String,OptionalFileData as Byte())The optional keyword requires me to set a default value - which is understandable, but I cannot figure out the syntax to do that here. Public Sub MyFunction(ByValFilenameasString,Optional FileData as Byte(){0})

View 1 Replies

Using Color As Optional Parameter In A Sub/function?

Nov 3, 2010

how can use color as an optional parameter in a sub/function?This doesn't work.

Code:
Public Sub WriteLog(ByVal sText As String, Optional ByVal lColor As Color = Color.Blue)
'Code
End Sub

View 5 Replies

Using Double.NaN In Optional Parameter On Interface?

May 27, 2010

I have come across something confusing or potentially a bug in Visual Studio 2010 when defining an interface in my VB application: When defining an interface method with a default parameter of type Double, using the Double.NaN constant as the default value causes the code editor/intellisense/precompiler some issues.The following code underlines "INaNTest" and "INaNTest.DoSomething" claiming that 'DoSomething' cannot implement 'DoSomething' because there is no matching sub on interface 'INaNTest':

[code]...

where now "NaN" has been divorced from the "Double." prefix and underlined ('NaN' is not declared. It may be inaccessible due to its protection level.) The code editor has inserted invalid code again.Is there a correct solution to using Double.NaN as the default value for a method as defined on an interface, in VB.net, or is there a fundamental reason why this is impossible?

View 2 Replies

VS 2010 Optional Parm In Property?

Jan 8, 2012

am trying to add an extra optional parameter to a property but it gives this message.Specifiers valid only at the beginning of a declaration. Public ReadOnly Property ReadValue(ByVal Selection As String, ByVal Key As String,Optional Default as string="") As String

View 3 Replies

Why Do Optional Class Attributes In .NET Have A Different Syntax

Mar 11, 2009

I'm just curious why class/property attributes in VB.NET have a weird optional syntax such as:

<TestAttr("a", "abc", Optional1:="foo", Optional2:=3)>

VB.NET allows you to set optional parameters like this to avoid order restrictions (which is lovely) but in this case it's forcing you to that.For example this is not possible:

<TestAttr("a", "abc", "foo", 3)>

even though the parameters are in the same order as the original definition.

View 2 Replies

.net - Declare An Optional ArrayList With Elements Inside?

Nov 19, 2010

I ve an interface like

[Code]....

but I want to declare an optional arraylist with default values... how to do that?

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved