VB .net programming question

GoHAnSoN

Senior member
Mar 21, 2001
732
0
0
fed up searching 'new' in google.... can someone help ?

what's the "new" do when i declair something ?
 

Stuxnet

Diamond Member
Jun 16, 2005
8,392
1
0
"new" declares a new instance of an object:

Dim w As New Widget()

w is now instantiated as an object of type "Widget".
 

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
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.
 

microAmp

Diamond Member
Jul 5, 2000
5,988
110
106
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.
 

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
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
 

microAmp

Diamond Member
Jul 5, 2000
5,988
110
106
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!
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
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.
 

microAmp

Diamond Member
Jul 5, 2000
5,988
110
106
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.
 

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
No, the parentheses are not required, but it's good practice since many languages do require them. It's also more readable IMO.
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
it is an excellent practice to use parentheses. comparison operators take precedence over logical operators, but its not guarenteed for all languages/compilers.
 

Atheus

Diamond Member
Jun 7, 2005
7,313
2
0
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.

 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |