Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2005

Tip: Looking for answers? Try searching our database.

what the c# equivalent of ...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jose g. de jesus jr mcp, mcdba - 29 Jul 2005 09:31 GMT
vb.net val function

c=val(a$)+val(b$)

thanks
Fabien Bezagu - 29 Jul 2005 10:02 GMT
This could be :

double c = double.Parse(a) + double.Parse(b)

Fabien

> vb.net val function
>
> c=val(a$)+val(b$)
>
> thanks
Nick Malik [Microsoft] - 29 Jul 2005 15:04 GMT
given VB's tendency to hide errors, the Val() call would return a zero if
the string couldn't be parsed.

Therefore, the equivalent would more likely be:

string a = "233";
string b = "abc";
double conv_a, conv_b;
double.TryParse(a,NumberStyles.Integer,
CultureInfo.CurrentCulture.NumberFormat, conv_a);
double.TryParse(b,NumberStyles.Integer,
CultureInfo.CurrentCulture.NumberFormat, conv_b);
double c = conv_a + conv_b;

Signature

--- Nick Malik [Microsoft]
   MCSD, CFPS, Certified Scrummaster
   http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
  I do not answer questions on behalf of my employer.  I'm just a
programmer helping programmers.
--

> This could be :
>
[quoted text clipped - 7 lines]
>>
>> thanks
Nicholas Paldino [.NET/C# MVP] - 29 Jul 2005 19:54 GMT
   Actually, I would think that the best choice would be to actually use
the Val function in VB, by adding a reference to Microsoft.VisualBasic.dll
and then use the static Val method on the Conversion class in the
Microsoft.VisualBasic namespace?

   Hope this helps.

Signature

              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard.caspershouse.com

> given VB's tendency to hide errors, the Val() call would return a zero if
> the string couldn't be parsed.
[quoted text clipped - 21 lines]
>>>
>>> thanks
Paul E Collins - 29 Jul 2005 22:38 GMT
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com>
wrote:

> Actually, I would think that the best choice would be
> to actually use the Val function in VB, by adding a
> reference to Microsoft.VisualBasic.dll and then use
> the static Val method on the Conversion class in the
> Microsoft.VisualBasic namespace?

Since C# is supposed to be a complete language in its own right, I'd
advise against using the contents of this namespace unless you're in a
big hurry to port some old code. Yeah, I know this is a matter of
debate, but IMHO when you include the VisualBasic namespace you might
as well be flagging your code with ObsoleteAttribute.

P.
Bob Grommes - 31 Jul 2005 14:16 GMT
But as another poster pointed out, many VB backward-compatibility functions
have subtle (and sometimes evil) behaviors that are designed for VB6
compatibility, and if the code expressly or unconsciously relies on these
behaviors, then using straightforward CLR code may get the job done but
without the VB6 side effects and therefore without the exact same execution
path.

I don't know how much of a newbie the OP was but really the simplest answer
is:

string a = "123";
int c = Convert.ToInt32(a);

... or the equivalent ToDouble() or whatever.  But you have to ask yourself
what happens with boundary conditions such as an empty, null or non-integer
string with the original Val() function, and then if the behavior is
different you have to decide whether it matters, or whether you want to use
more standard or robust methods of dealing with those boundary conditions.

One of VB.NET's dirty secrets is that in an effort to ease the VB6 to VB.NET
transition, developers have been encouraged to use Val() and CInt() and
CType() and so forth, rather than write standard .NET API calls.  And it
creates issues such as what we're now discussing when you want to port code,
and sometimes creates issues in mixed-language projects.  Since I never did
much VB6 and learned C# as my first and primary .NET language, when I get
involved in a VB.NET project I tend to write VB.NET versions of how
something would be done in C#.  This produces somewhat different code than
most VB coders tend to write.  Not wildly so, but more CLR oriented than VB6
oriented.  My VB.NET code could get by without VisualBasic.dll.

By contrast someone who started with, say, QuickBasic and has stayed with
the language ever since understandably tends to want to write code the way
they always have, out of affection and nostalgia.  And that's arguably fine
if you really want to stay exclusively in a parochial VB-centric
environment.  But I think it cuts your flexibility down.

--Bob

>> Actually, I would think that the best choice would be
>> to actually use the Val function in VB, by adding a
[quoted text clipped - 9 lines]
>
> P.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.