Help me go from PHP to VB.net.
August 21, 2008 10:58 PM   Subscribe

Help me go from PHP to VB.net.

I know PHP. I want to learn VB.net. Since I'm used to understanding basic concepts in one way, I'm having a hard time in VB.net rethinking things. Here are my specific questions:

- In PHP, if I want to call a method (I know, in PHP they're called functions, but go with me here), I use "Method_Name(Parameters_If_Any);". And that is always the syntax. But in VB.net, it seems to change. For example, "Console.WriteLine(strMyString)" is a method that prints something to the current console. But "myObject.GetType()" is also a method that gets the type of an object. In this second example, the function comes last, after an object, and there are no parameters. How do I know when to use which syntax? That doesn't make sense to me. What am I missing here?

- In VB.net, when I dimension a variable, I have to declare its type and its value. That makes sense to me. For example, "Dim strMyString as String = "This is a string"". But I don't understand how/why you would dimension a *variable* as an object. For example, "Dim variable As System.DBNull = System.DBNull.Value". What the heck does that mean? I'm dimensioning a variable as an object and initializing its value to another object?

- In VB.net, why do I sometimes dimension a variable as an object without creating a new instance (i.e., "Dim variable as Object") and sometimes I dimension a variable as an object by creating a new instance (i.e. "Dim variable as New Object").

Again, I'm brand spanking new to VB.net. I've got what seems to be a pretty good book, but these are just some stuff I'm having trouble wrapping my brain around that the book isn't specifically explaining. Sorry if I've used any incorrect syntax or terminology. Like I said, brand new, complete VB.net dummy. Go easy on me.

Any great newbie articles, blog posts, web series, books, videos, etc. that you might recommend? FYI: I'm much more interested in learning the actual code/syntax than I am in learning how to use an IDE to get around coding.
posted by JPowers to Computers & Internet (10 answers total) 3 users marked this as a favorite
 
For example, "Console.WriteLine(strMyString)" is a method that prints something to the current console. But "myObject.GetType()" is also a method that gets the type of an object. In this second example, the function comes last, after an object, and there are no parameters. How do I know when to use which syntax? That doesn't make sense to me. What am I missing here?

In the first example, "WriteLine" comes after (is called on) an object, "Console", which represents the current console. This particular method takes one argument, the string to be displayed. In the second, you call the method "GetType" on "myObject". It so happens that GetType has no parameter.

To know whether a method takes 0, 1, 2, or N arguments, you have to check the documentation, just like you would in PHP. If you're using an IDE, a parameter list should appear once you're typed the method name.
posted by Monday, stony Monday at 12:29 AM on August 22, 2008


I'm more accustomed to C# and its syntax makes more intuitive sense to me, but I'll have a go...
  1. I'm not entirely sure I understand your first question there, because I'm not sure why you regard those two examples as having different syntax. Maybe it's that "Console.WriteLine(strMyString)" is a static method being called from a type itself rather than an object, and hence you don't have to Dim anything to use it?
  2. It might be the same thing in this 2nd question - "System.DBNull.Value" is a static member of the type "System.DBNull". In this case it's the binary value that represents a null DB field to the .NET SQL classes internally. (System.DBNull is kind of a funky construct anyways by the way, it's sort of a square peg in a round hole trying to mesh SQL with .NET)
  3. There are a variety of reasons why you might want to do this and I can't think of a concise summary that would cover everything. "Dim variable as Object" is reserving memory for the variable, setting the variable's scope, and naming the variable, without defining a value for it yet. One reason to do that is for the sake of efficiency when using the variable inside a loop; take for example this code that uses System.Uri objects to pull the host names out of a list of web addresses and write them to the console:
    Dim webAddressList As String() = {"http://msdn.microsoft.com/en-us/vbasic/default.aspx", "http://www.google.com/codesearch", "http://www.codeproject.com/KB/vb/"} For Each uriStr In webAddressList      Dim uriObj As System.Uri = New System.Uri(uriStr)      Console.WriteLine(uriObj.Host) Next uriStr
    Because the code above is Dimming a new System.Uri object at every pass of the loop, it's going to reserve a new chunk of memory each time. That memory won't be freed up to be re-used until an automatic system process called "garbage collection" runs. In the case above, with just three loop passes, it's not a big concern, but the larger and more complex a piece of software becomes the more closely you have to pay attention to things like this. A more efficient way would be to place the Dim statement before the loop:
    Dim webAddressList As String() = {"http://msdn.microsoft.com/en-us/vbasic/default.aspx", "http://www.google.com/codesearch", "http://www.codeproject.com/KB/vb/"} Dim uriObj As System.Uri For Each uriStr In webAddressList      uriObj = New System.Uri(uriStr)      Console.WriteLine(uriObj.Host) Next uriStr
    Doing it this way will only reserve memory once and will effectively re-use that same chunk of memory on each pass of the loop.
Because I learned C# first I already knew all of the .NET conventions and classes and things, so there wasn't too much more to learn about VB.NET. So I've always been able to just work off of the MSDN site's reference pages. I'd advise that you keep it handy but it wouldn't be a great way to learn from scratch.

Definitely look through the ecosystem of Microsoft-funded sites out there (the place to begin is http://msdn.microsoft.com, which serves as a hub linking out to other resources.) MS really wants people to learn its languages so that they and their organizations become more dependent on MS technology and products, so they pump alot of money into making free, quality learning content available.
posted by XMLicious at 12:30 AM on August 22, 2008


One other thing in #2 above is that System.DBNull.Value is a static property attached to System.DBNull but it's at the same time a property of the type System.DBNull, so it's almost its own little existential chicken-and-the-egg dilemma. As to why it's done that way, I can't really say, except that it's a particular quirk of fitting SQL database stuff into an object-oriented framework the way they did. (There are other reasons for doing something like this you might encounter, such as implementing the singleton pattern in a class, but that's not the case for DBNull.)
posted by XMLicious at 12:40 AM on August 22, 2008


Okay, another attempt to say the same thing, hopefully clarifying:
System.DBNull.Value is to System.DBNull as
42 is to System.Integer and as
"wombat" is to System.String
posted by XMLicious at 12:55 AM on August 22, 2008


On the Visual Basic Developer Center look for the links under the heading "Learn Visual Basic"



Some light googling turned up these 2 free sites that seem to be good jumping off points:
http://www.programmersheaven.com/2/VB-NET-School
and
http://www.startvbdotnet.com/language/default.aspx

good luck
posted by askmehow at 1:57 AM on August 22, 2008


1. This is an object-oriented thing. In a less OO language, the design is usually a bunch of functions that work with basic data types. So most of the code would be in the form of DoSomething(someString, someNumber). In an OO language, the design is usually focused on objects that provide functions, in the form of thingDoer.DoSomething(someString, someNumber). The two biggest exceptions to this are when you have an object call a function on itself, such as having thingDoer call Me.DoSomething or just DoSomething, or using a Shared function such as Console.Writeline, which does not need an actual object to work.

2. I'm not sure I get what you're asking in this one, but in VB.NET, when you declare a variable as a given type, you can only assign values of that type to it.

3. Dim variable as Object by itself is exactly the same thing as Dim variable as Object = Nothing. When you declare a variable without assigning a value to it, it has the default value, which is Nothing (null) for Classes and a default value for Structures. When you do Dim variable as New Object, that's exactly the same thing as Dim variable as Object = New Object. So, in general a declared variable will always be given a value immediately, but it can be Nothing, the value of an existing variable, or a brand new value, depending on how you write the code.

XMLicious: Because the code above is Dimming a new System.Uri object at every pass of the loop, it's going to reserve a new chunk of memory each time.

Are you sure that the compiler isn't smart enough to optimize this?
posted by burnmp3s at 4:57 AM on August 22, 2008 [1 favorite]


It seems to me you're having problems grasping the Object Oriented nature of VB.Net. PHP (5 at least) does have OO support, but it's something that was tacked on as an afterthought; PHP is primarily a procedural language.

I would suggest, instead of looking into VB.Net specific documentation, that you look into a book or online resource giving you Object Oriented theory, discussing classes, objects, and the difference between the two, abstraction, inheritance, etc, etc.

This is the first tutorial I was able to find that seemed to use VB.NET as it's language of choice for examples. At first glance it looks to be very basic in nature, and I can't vouch for it's quality, but it seems to be an O'Reilly publication, so it should be okay.

Googling should give you a ton of further info, as well. I found this and this as well.
posted by cgg at 7:08 AM on August 22, 2008


2, also related to 1: Here's the rule of thumb...

- Foo.bar(): bar() is a method attached to an object called Foo. You can easily tell this because it's got parentheses, which is where you pass in parameters. It just happens that bar() is a method that takes no parameters; it could also be Foo.bar("snafu") if bar() took a string as an argument. It will return a specific data type, so if it returns a string, you would use it like so:

string strResponse = Foo.bar("snafu");

- Foo.bar: Note the lack of parenthesis. This means that bar is a property of Foo. It is something you read or write (or, to use proper terminology, "get" or "set"). Many system-level objects have properties you can get, but not set. You access it like so, assuming "bar" is a string:

string strValue = Foo.bar;
Foo.bar = strValue;


My general advice- I detest many of Microsoft's products, but Visual Studio.NET is a fantastic IDE. IntelliSense is your friend- you can quickly see members of objects as you type, and, more importantly, relevant datatypes. Everything is well-documented in MSDN (as opposed to PHP's documentation, which I always find frustrating). I've found CodeProject to be a handy resource- the actual code is sometimes hit-or-miss, but in general it's a good place to find answers when you know conceptually how to do something but just don't know the correct .NET syntax for it.
posted by mkultra at 7:42 AM on August 22, 2008


Note the lack of parenthesis. This means that bar is a property of Foo.

This isn't actually true in VB.NET. In VB.NET, Functions, Subs, and Properties can all take zero or more arguments. For all of those, if no argument is required, adding the () is optional.
posted by burnmp3s at 12:11 PM on August 22, 2008


Are you sure that the compiler isn't smart enough to optimize this?

Based on my experience in profiling and reducing memory usage in VB.NET applications I don't believe it would. Also, I think it would be a somewhat risky proposition for the compiler to try to automatically share memory among different variables in different scopes like that (based on the variable name, I guess?) and would probably introduce some thread-safety issues. But I certainly could be wrong; I've done work in x86 assembly and MSIL but I haven't written a compiler.
posted by XMLicious at 2:42 PM on August 22, 2008


« Older Need To Score Some Hope Pronto   |   Why on Earth did 17th Century Dutch painter... Newer »
This thread is closed to new comments.