By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,627 Members | 2,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,627 IT Pros & Developers. It's quick & easy.

a few noob vb.net questions

P: n/a
Three noob questions. Please help.

1. Is there a way to have arguments on subroutine that are optional?


2. Say I need to convert datetime to string..in this format :
2006-09-07 23:00:00.000

datef.tostring()

produces:

'1/1/0001 12:00:00 AM'

I need it to produce something like this:

2006-09-07 23:00:00.000

I know you can do something like this:

..datef.ToString("MM\-dd\-yy")


3. I want to pass a subroutine an argument that might be type string or
might be type datetime.. how can I do this and then use it's value
accordingingly? type Object maybe?

as a bad example to demonstrate the problem.

say..

Public Sub UpdateJob(ByVal field as string, ByVal Value As Object)

will I be able to call UpdateJob

like this

Update(datetimef) (where datetimef is type datetime)
and like this
Update(stringf) (where stringf type string)
and then inside UpdateJob

SQL = "Update job set " + field + "='" + Value

The above of course does not work.

Thank you in advance for any help or information.

Jan 23 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"jobs" <jo**@webdos.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
Three noob questions. Please help.

1. Is there a way to have arguments on subroutine that are optional?
Use the optional keyword I believe.
2. Say I need to convert datetime to string..in this format :
2006-09-07 23:00:00.000

datef.tostring()

produces:

'1/1/0001 12:00:00 AM'

I need it to produce something like this:

2006-09-07 23:00:00.000

I know you can do something like this:

.datef.ToString("MM\-dd\-yy")
Yes.
3. I want to pass a subroutine an argument that might be type string or
might be type datetime.. how can I do this and then use it's value
accordingingly? type Object maybe?
Either object or define the function twice with each parameter type. This is
called overloading.

Michael
Jan 24 '07 #2

P: n/a
"jobs" <jo**@webdos.comschrieb
Three noob questions. Please help.

1. Is there a way to have arguments on subroutine that are optional?
See "Optional" keyword and "procedure overloading" in VB documentation.

2. Say I need to convert datetime to string..in this format :
2006-09-07 23:00:00.000

datef.tostring()

produces:

'1/1/0001 12:00:00 AM'

I need it to produce something like this:

2006-09-07 23:00:00.000

I know you can do something like this:

.datef.ToString("MM\-dd\-yy")

Use the format string that you need. See ToString documentation for details.
In your code, put the cursor on 'ToString' and press F1.
3. I want to pass a subroutine an argument that might be type string
or might be type datetime.. how can I do this and then use it's
value accordingingly? type Object maybe?

Write two procedures: Same name but different data types (different
signature). This is called overloading.

sub bla(byval x as string)
end sub

sub bla(byval x as date)
end sub
Armin

Jan 24 '07 #3

P: n/a
On 2007-01-23, jobs <jo**@webdos.comwrote:
Three noob questions. Please help.

1. Is there a way to have arguments on subroutine that are optional?
Yes - using the Optional keyword. But, may I suggest that you NOT use this
technique in any publicly accessible methods. In those cases, you should
really use the ability of VB.NET to allow overloading... Ex:

Public Class ExampleClass
Public Sub DoCoolStuff ()
' Do Cool Stuff With No Arguments
' A lot of times, you would just call one
' of the other overloads with a default value
Me.DoCoolStuff (100)
End Sub

Public Sub DoCoolStuff (ByVal i As Integer)
' Do Cool Stuff to i
End Sub
End Class

>

2. Say I need to convert datetime to string..in this format :
2006-09-07 23:00:00.000

datef.tostring()

produces:

'1/1/0001 12:00:00 AM'

I need it to produce something like this:

2006-09-07 23:00:00.000

I know you can do something like this:

.datef.ToString("MM\-dd\-yy")
Look up format strings in the documentation. Yes, you can do lot's of
different formats.
>
3. I want to pass a subroutine an argument that might be type string or
might be type datetime.. how can I do this and then use it's value
accordingingly? type Object maybe?

as a bad example to demonstrate the problem.

say..

Public Sub UpdateJob(ByVal field as string, ByVal Value As Object)

I would overload it :)

Public Sub UpdateJob (ByVal field As String, ByVal Value As DateTime)
Public Sub UpdateJob (ByVal field As String, ByVal Value As String)

Now you don't have to worry - the compiler will call the right one.

--
Tom Shelton
Jan 24 '07 #4

P: n/a


Thank you for all responses. Very good!

Jan 24 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.