|
-
Jul 16th, 2004, 01:57 PM
#1
Thread Starter
Lively Member
Trying to help...
I don't know if anyone reading this now will ned this.
Anyway, there is a small thing I see in many code examples programmers offer in this forum and outside of it, this is about the intialization of objects using the New keyword.
Why using this
VB Code:
Dim Obj as SomeType
Obj = New SomeType(...)
if you can use this?
VB Code:
Dim Obj As New SomeType(...)
? ? ? a modern programmer should always look for the compact and shortest way.
Last edited by TLord; Jul 16th, 2004 at 04:46 PM.
Do you think my life is easy?
Do you think it's good to win?
do you think it's nice to kill?
Do you think learning is a must?
Do you think computers are nothing?
Do you think this post is stupid?
Do ypu think we're really humen?
DO YOU THINK IT'S GOOD TO THINK AT ALL? ? ? ! ! !
-
Jul 16th, 2004, 02:07 PM
#2
you may not always want to fire code that is in the new method when declaring the object
-
Jul 16th, 2004, 03:03 PM
#3
Frenzied Member
? ? ? a modern programmer should always look for the compact and sortest way.
That is inherently the wrong way to think. Why should it be compact and short? Better to be descritpive and informative. There have long been short cuts to make code compact, but better to be maintainable and easily understood by others.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 16th, 2004, 04:23 PM
#4
If you are doing excally this
VB Code:
Dim Obj as SomeType
'without anycode inbetween here
Obj = New SomeType(...)
there is no reason not to do this:
VB Code:
Dim Obj As New SomeType(...)
however, there was no option in vb6 to do it in one line and since most vb.net programmers come from vb6, you will see the first method by habbit.
You should Never sacrfice quality for time/code length
Although, when working on some projects, it may be acceptable to do this because of no other option.
Even in temporary projects, or samples i create for ppl on the forum i still follow all the programming guidelines that i follow on a regular project. (force of habbit, but this one is good )
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Jul 16th, 2004, 04:44 PM
#5
Thread Starter
Lively Member
Originally posted by SeanGrebey
That is inherently the wrong way to think. Why should it be compact and short? Better to be descritpive and informative. There have long been short cuts to make code compact, but better to be maintainable and easily understood by others.
So what's the job of Comments?!?!?
Originally posted by <ABX
however, there was no option in vb6 to do it in one line and since most vb.net programmers come from vb6, you will see the first method by habbit.
Me too, I've came to VB.Net from VB6, byt I use the best out of the new features which can be a better replacement.
And as a public answer, It's rarely needed to initialize the objects several lines after they're declared. For me I do this almost only when manually defining controls for a class that inherits System.Windows.Forms.Form class, ie coding a windows form manually without the help of WFD (Windows Forms Designer).
YOU PEOPLE SHOULD REMEMBER THE TITLE OF THIS THREAD: "TRYING TO HELP...", I WANT ALSO TO HELP PEOPLE WHO CAME FROM VB6 AND DON'T KNOW ABOUT THIS KEYWORD
Last edited by TLord; Jul 16th, 2004 at 04:53 PM.
Do you think my life is easy?
Do you think it's good to win?
do you think it's nice to kill?
Do you think learning is a must?
Do you think computers are nothing?
Do you think this post is stupid?
Do ypu think we're really humen?
DO YOU THINK IT'S GOOD TO THINK AT ALL? ? ? ! ! !
-
Jul 16th, 2004, 05:14 PM
#6
Frenzied Member
I seem to recall a recent post where it made a difference between early and late binding, but won't swear to it, or may have misunderstood.
Ok, here's the post I was thinking of.
Last edited by salvelinus; Jul 16th, 2004 at 05:17 PM.
-
Jul 16th, 2004, 05:57 PM
#7
Sleep mode
-
Jul 17th, 2004, 07:40 AM
#8
PowerPoster
Hi,
The important point to bear in mind is not the difference between the two methods but the EFFECT of the difference.
If you use early binding, you will not run into any problem later.
If you use late binding you may run into a problem by carelessly trying to reference before the code runs trhough the binding. I know that if good practice is followed, you will not have a problem, but you could. This was discussed in a thread during the past week or so but I cannot remember it's title. Maybe Cyberhawke can.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 17th, 2004, 03:35 PM
#9
Sleep mode
Originally posted by taxes
Hi,
The important point to bear in mind is not the difference between the two methods but the EFFECT of the difference.
If you use early binding, you will not run into any problem later.
If you use late binding you may run into a problem by carelessly trying to reference before the code runs trhough the binding. I know that if good practice is followed, you will not have a problem, but you could. This was discussed in a thread during the past week or so but I cannot remember it's title. Maybe Cyberhawke can.
True , I always use early binding with the objs I create .
-
Jul 17th, 2004, 10:34 PM
#10
Frenzied Member
ooh! my post got a plug! lol
-
Jul 18th, 2004, 12:30 AM
#11
PowerPoster
I think you just need to make a judgement call. I normally do it all on one line, but in situations like the one below, I deviate because I only need one variable instead of two different ones.
C#:
Code:
MyObject myObj;
if(someValue > 0)
myObj = new MyObject(someValue);
else
myObj = new MyObject();
// Use myObj more down here.
or VB.NET
Code:
Dim myObj As MyObject
If someValue > 0 Then
myObj = new MyObject(someValue)
Else
myObj = new MyObject()
End If
Notice how I didn't make a new instance of it right away, because it would have been a waste of processing because I might have needed to call a different constructor after the object was created. In the case above, this was the most efficient I could do it. (unless someone else knows otherwise).
But, other than that case, I usually do this:
MyObject myObj = new MyObject();
or in VB.NET
Dim myObj As New MyObject()
-
Jul 18th, 2004, 08:10 AM
#12
PowerPoster
Originally posted by Grimfort
where as in .Net the variable is only available below the declaration.
Not quite correct.
You can declare a form or public scope variable anywhere within the class after the Inherits statement provided it is outside the procedures or events and it will be recognised in an earlier positioned procedure or event.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|