Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

What are the .NET replacements for these VB6 Legacy functions...

 

Chr

Asc

IsNumeric

IsDate

InStr

 

Is there a reference list somewhere that I can look at for these and others?

 

By the way, I noticed some of my Window forms generated code uses the Microsoft.VisualBasic. What's up with that?

Posted
And "Format" as well. (I had assumed .Format property of the string type worked the same as the old VB6 "Format", but I now see that's not the case.
Posted

chr = convert.tochar(integerhere)

asc = convert.toint16(charhere)

'or int32, it doesn't really matter

isnumeric = Double.TryParse("string", Globalization.NumberStyles.Integer, Nothing, 0.0#) 'Probably better ways, but at least this one is one line.

I've never used Isdate, so I don't know what it does..>

Nor have I used InStr before..

 

I've never needed to use any of these though..

Posted
And "Format" as well. (I had assumed .Format property of the string type worked the same as the old VB6 "Format"' date=' but I now see that's not the case.[/quote']String.Format and other methods that take a format string, like StreamWriter.WriteLine, work the same way as the "printf" method from the C language. This is because all .NET Framework classes should work the same in all .NET languages. C has been the benchmark for programming languages for a long time, and C++, C# and even J# indirectly, are derived from it. I've never used Microsoft.VisualBasic.Format before but String.Format has handled everything I've thrown at it.

 

Using Microsoft.VisualBasic is not a crime, but it is best avoided if at all possible. I'd be interested to know what auto-generated code it is that uses that namespace. There are many references to it throughout the help files, but there is almost always a .NET altermative or you can create your own.

 

Edit:

If you're ever using an unfamiliar method you should always read the relevant help topic first. Never assume that the same name means the same functionality.

Posted

And for InStrRev

 

Dim s as String = "asdfa"

Dim x as Integer = s.LastIndexOf("a")

 

Gives x=4

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted

From MSDN

Many of the methods in the Visual Basic Runtime actually use methods and properties from the System namespace (for example, Len() returns String.Length). In some cases you can achieve equivalent results by accessing .NET Framework class library classes directly,... In other cases, such as IsDate, there is no directly equivalent functionality in the System namespace.

 

The suggested method is to use DateTime.Parse or DateTime.ParseExact and catch the exception thrown which I don't find particularly satisfying. However if this is to check user input then investigate the DateTimePicker control (instead of textboxes for example) which will always return valid dates for you as DateTime objects without the need to check the format.

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted
Further to IsDate the use of regular expressions would allow you to check that a string matches a valid Date format and save you from having to catch the exception thrown by using DateTime.Parse on an invalidly formatted string.

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted

Thanks for the help. I'll check out these functions and may have more questions later. So no one knows the Microsoft.VisualBasic.Format equivalent? For those who may not know, it formats your string with a type of "mask" or "conversion." (Not sure what the word would be.) Examples...

 

Microsoft.VisualBasic.Format(5.2, "$0.00")

... returns "$5.20"

 

Microsoft.VisualBasic.Format("1/1/1989", "MMMM d, yyyy")

... returns "January 1, 1989"

 

Microsoft.VisualBasic.Format("2002 Sep 5", "mm/dd/yy")

... returns "09/05/02"

 

Microsoft.VisualBasic.Format(Now(), "hh:mm:ss ampm")

... returns "01:55:04 PM"

 

etc.

  • Administrators
Posted

Instead of format just use .ToString(), most data types overload it to accept formating information.

       Dim s As String
       Dim i As Decimal = 5.2
       s = i.ToString("C")
       'or
       s = i.ToString("$#,##0.00")

       s = DateTime.Now.ToLongDateString()
       s = DateTime.Now.ToString("MMMM d, yyyy")

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • 2 weeks later...
Posted
What about the "Collection" object? I just realized it's in the Microsoft.VisualBasic namespace, so does it have a new .NET equivalent? Arrays aren't considered legacy code now, are they?
Posted
Neither of those allow for a "Key" as Collections do. Is there no .NET equivalent that will allow me to add entries, to which I can give a key that I later use to look up values?
Posted

As PlausiblyDamp said, look at the System.Collections namespace. It contains a variety of collections including ArrayList, Stack, Queue, HashTable, SortedList and base classes to be used to create your own custom collections. .NET 2.0 introduces Generics as well, which, amongst other things, provide for strongly typed collections without creating a custom collection for each type.

 

Arrays still exist in .NET but have been improved upon, in that all arrays are now instances of the Array class. As such, arrays have properties and methods of their own, so they don't have to rely on module functions for copying and the like.

Posted

OK, I'll look through all that stuff. I just thought someone would know off the top of their head which would be most similiar to a collection, but no biggee.

 

 

Another question. Up above, I was told to use .ToString(format_string) to format Numbers and Dates. This works fine if my variables are declared as Numbers or Dates. However, how can I format a string that may have a number or date in it? It seems cumbersome to declare a temporary number or date variable, assign the string's value to it, then do a .ToString from the temp variable back to the original string, but is that the only way?

 

Something like the following would be nice, but the second line isn't allowed...

 

str = "04/01/2005"

str = str.ToString("MMMM yyyy")

 

So all I can determine is doing something like this...

 

str = "04/01/2005"

Dim d As DateTime = str

str = d.ToString("MMMM yyyy")

 

Which in and of itself is not a big deal, but it seems like rather sloppy code to have all these temporary variables declared throughout my program. Or is that the only way to do it and/or the preferred .NET way?

Posted

Just as a question, why would you have something you intend to have a date or number in that isn't defined as such?

 

Though you can take typing shortcuts to avoid the tempvariables eg

str=DateTime.Parse(str).ToString("MMMM yyyy")

 

Although this will create a temp instance of a datetime variable (to store the result of DateTime.parse) it will immediately be free for garbage collection.

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted
It's just a dummy example to represent basic user input into a textbox. Thanks for the example, which I'll use unless someone says otherwise.
Posted

If you require user input as a date, time or both then you should be using a DateTimePicker rather than a TextBox. You can format the Text of your DTP however you want, but the Value property is always a Date object.

 

As a point of interest for those who aren't aware, the Date and DateTime types are exactly the same thing. The in-built Date data type is implemented using the System.DateTime structure. All in-built data types (String, Integer, Boolean, etc.) are implemented using a class or structure from the System namespace.

 

As for VB6 Collections, I've never used one myself. From what little you've said, though, I'd say that the HashTable is the closest thing as it has key/value pairs. The SortedList is the same, except the collection is always sorted by key. You should check out all the collections available in .NET, however, and use the the most appropriate one for each occassion.

Posted
What about the "Collection" object? I just realized it's in the Microsoft.VisualBasic namespace' date=' so does it have a new .NET equivalent? Arrays aren't considered legacy code now, are they?[/quote']Try HashTable

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
If you require user input as a date' date=' time or both then you should be using a DateTimePicker...[/quote']

 

I'm afraid that control isn't an option for me.

 

But I'll look into HashTable. Thanks jmcilhinney and JM.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...