• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

VB .net programming question

"new" declares a new instance of an object:

Dim w As New Widget()

w is now instantiated as an object of type "Widget".
 
I've never used VB.net, so I can only tell you what it did in prior versions. Declaring a variable as New Object causes an object of type Object to be created. The difference between

Dim myObj as oFoo

and

Dim myObj as New oFoo

was that in the first example, a reference was created to a instance of oFoo, but the object itself was not created and therefore any initialization code doesn't happen. The object wouldn't actually be created until the first usage of myObj in the function. The second example caused the item to be instantiated, and any code that ran at object creation would occur at declaration.

It's a minor difference, and often unnoticeable, but you do have to realize what's going on. Here's some pseudo-code to illustrate the difference:

Function myFunction
Dim myObj as oFoo

If lGlobalFooCounter > 0 Then Exit Function
myObj.DoYourThing
End Function

Object oFoo
Initialize
lGlobalFooCounter = lGlobalFooCounter + 1
End Initialize

Function DoYourThing
EndFunction
End Object


Ignoring the fact that this is horrible logic and design to begin with... not using the New keyword can cause issues. The initialization of oFoo increments a global counter. Without the New keyword, the object is not being initialized until after the counter has been checked in the function which creates an instance of oFoo. If for some reason you could only allow a single oFoo, perhaps it's using a file locked mode, this function would fail. If the New keyword had been used, the object would have been initialized at declaration, which would have incremented the counter such that checking the global would properly reflect the number of oFoo objects in use.

I hope this made sense. I just pulled an all nighter and I'm starting to drift off. 😛
 
Hate to try to hijack this, but does VB 6.0 have an OR statement available.
Example

If class = "I" OR "II" Then
lst_computer.AddItem "ZD-1"

ElseIf (etc...)

I did try googling, but sort of came up empty. 🙁
 
Uhhh, do you want a logical operator for doing bit operations? Or are you just wondering about multiple conditions for an If?

Logical Operators: AND, OR, XOR, NOT...

1 AND 2 = 0
1 OR 2 = 3


If Statement:

If (x = 1) Or (x = 2) Then
DoStuff
End If
 
Ah, that did the trick BoberFett. I think I was just missing the ( ). Goes to show that I should go to school and not try to do self-learning. 🙂

Thanks again!
 
Parentheses are not needed.

Also, you can use OrElse for short circuit evaluation... i.e.

If X=1 OR X=2 Then

End If

In the above, X=2 will be evaluated regardless if X=1.

If X=1 ORELSE X=2 Then

End If

In the above, X=2 will only be checked if X <> 1. This may help if X=2 is something that has a performance hit.
 
I tried that before torpid, didn't work for me. Not at the time I tried, but I'll be damned if it's working now. 😀
 
No, the parentheses are not required, but it's good practice since many languages do require them. It's also more readable IMO.
 
it is an excellent practice to use parentheses. comparison operators take precedence over logical operators, but its not guarenteed for all languages/compilers.
 
OP: You might want to do a bit a research into object oriented programming (OOP) in general before you go any further, especially if you didn't undertand BoberFett's answer. Tutorial websites are good, books are better.

 
Back
Top