best awy to create a function?

B

Bob

Hi,

I defined in a class a function which inserts records into a table. There is
no return.
I defined it first like this:
Public Shared Function myfunction(ByVal param1 As Integer) As Boolean
.....
return true
end function

and then without As Boolean:
Public Shared Function myfunction(ByVal param1 As Integer)
.....
end function

But i cant' see any difference between both.
What's the best way to do: with or without As Boolean?
Thanks
Bob
 
R

rowe_newsgroups

Hi,

I defined in a class a function which inserts records into a table. There is
no return.
I defined it first like this:
Public Shared Function myfunction(ByVal param1 As Integer) As Boolean
....
return true
end function

and then without As Boolean:
Public Shared Function myfunction(ByVal param1 As Integer)
....
end function

But i cant' see any difference between both.
What's the best way to do: with or without As Boolean?
Thanks
Bob

First of all, before you do anything else, turn on Option Strict. IMO,
you should always have this on, unless you are working on one of the
very few scenarios where it isn't possible. With Option Strict On, you
can see your second function is not valid.

As far as which one is better, you must look at what the method needs
to do. If it needs to return a boolean value representing if the
insert was successful, it must be a boolean function. If it does not
need to return a value, you should define it as a regular method, like
the below:

////////////////
Public Shared Sub Foo(ByVal meaningfulName As Integer)

End Sub
///////////////

Thanks,

Seth Rowe
 
R

rowe_newsgroups

There may also be a project or solution setting for this

Tools-->Options-->Projects and Solutions-->VB Defaults
From there you can set the Option Explicit, Strict, Compare and Infer
(VS 2008) defaults.

Thanks,

Seth Rowe
 
S

sloan

Just the one caveat about this.

If you're in a multi developer environment, another use can check code out,
and have a different setting.

Aka, "environment setup wars".

And the these are the "default" values. So a preexisting project/solution
might gotcha as well.

...

But if you're a single developer, then yes, set the defaults up to make life
easier.

My opinion is that when its a multi developer environment, throwing the
lines at the top of the code is a good habit.
But its an opinion.
 
R

rowe_newsgroups

Just the one caveat about this.

If you're in a multi developer environment, another use can check code out,
and have a different setting.

Aka, "environment setup wars".

And the these are the "default" values. So a preexisting project/solution
might gotcha as well.

..

But if you're a single developer, then yes, set the defaults up to make life
easier.

My opinion is that when its a multi developer environment, throwing the
lines at the top of the code is a good habit.
But its an opinion.

Very good points, one's I have never thought about.

Another area to watch out for is when developing ASP.NET code files in
VB, as these settings are completely ignored. Hopefully this was fixed
in VS 2008 (haven't checked beta 2 yet), as it was/is a major source
of confusion.

Thanks,

Seth Rowe
 
S

sloan

//Quote//
Another area to watch out for is when developing ASP.NET code files in
VB, as these settings are completely ignored. Hopefully this was fixed
in VS 2008 (haven't checked beta 2 yet), as it was/is a major source
of confusion.
//End Quote


Youch and Boooooo.

I didn't know about that one.
 
R

rowe_newsgroups

Youch and Boooooo.

lol
I didn't know about that one.

At least you lucked out - I found out about it the hard way:

-- remembering the past --

"What the **** do you mean the web site's down?"
...
"Oh, it's says a boolean can't equal "hello, world"?"

-- nightmare fades --

I admit I made that scenario up, but it did take me a while to figure
out why a previous project was acting funny. Since then I've switched
most web apps over to C#, as it is brutal (in a good way) when it
comes to preventing those types of errors. Juan Llibre over in the
ASP.NET newsgroup posted a solution which involved modifying the
templates to include the Option lines a long time ago, but that's
still plagued by the "environment setup wars" you mentioned above.

Thanks,

Seth Rowe
 
S

sloan

Yeah, if there was some kind of

Option JustLikeCSharp On

that couldn't be turned off...then I'd been ok with VB.Net.

But after 100 times of correcting other developers (the worst I saw is when
we turned it "on" at the project level, this one developer's code generated
over 200 errors)......
you get worn down.

I code 95% in csharp now, only maintenance in vb.net....and those kind of
situations just don't happen.
 
R

rowe_newsgroups

Yeah, if there was some kind of
Option JustLikeCSharp On

I actually started a thread on this a long time ago when I realized
the following difference between C# and VB.Net

//////////////
public int MyIntProperty {
get {

}
}
//////////////

Will throw the "not all code path's return a value" exception, while
it's VB equivalent:

//////////////
Public ReadOnly Property MyIntegerProperty() As Integer
Get

End Get
End Property
//////////////

Will not give any notification of a problem, it will happily return 0
(or the default value for other Value types) at runtime, often to your
program's doom. It can also be a pain to debug and fix - especially if
the missing return is in a deeply nested if or other nasty structure.
But after 100 times of correcting other developers (the worst I saw is when
we turned it "on" at the project level, this one developer's code generated
over 200 errors)......
you get worn down.

lol - I bet
I code 95% in csharp now, only maintenance in vb.net....and those kind of
situations just don't happen.

I still love to program in Visual Basic (especially for Office
interop) but I do lean towards C# for quit a few projects. If nothing
else I at least like staying sharp (pun intended) when it comes to VB.
It's just a shame that VB will allow (usually new) developers the
option to be a poor programmer. The brutal (again in a good way) error
checking of C# often makes maintenance much easier, especially when
you're looking at someone else's code.

Thanks,

Seth Rowe
 
H

Herfried K. Wagner [MVP]

Bob said:
I defined in a class a function which inserts records into a table. There
is no return.
I defined it first like this:
Public Shared Function myfunction(ByVal param1 As Integer) As Boolean
....
return true
end function

and then without As Boolean:
Public Shared Function myfunction(ByVal param1 As Integer)
....
end function

But i cant' see any difference between both.
What's the best way to do: with or without As Boolean?

In addition to the other replies, note that the second function returns
'Object' and the function declaration is semantically equal to 'Public
Shared Function MyFunction(ByVal param1 As Integer) As Object'.
 

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