Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I know how sub programs operate and all but whats the difference with these two sub programs ?

 

 

Private Sub testing(byval this as string, byval that as string)

.....

End sub

 

 

Private Sub new(byval this as string, byval that as string)

.....

End sub

 

P.S. In fact I want to know what happens when you put 'new' there instead of the sub program name.... so... TAKE MY HAND!!!!!!!!!!!!!!!! :D

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

Posted
In Visual Basic normally is used for the constructor of the class, so what will happen is that when you create an instance of that class all the code inside this sub will be executed.
Fat kids are harder to kidnap
Posted (edited)

OK thx, can you be more specific and try not to use words like an instance and constructor :D ok I know whats a constructor in C++ ...but VB....some example would lighten me .... :)

 

actually what is an instance ? lol

Edited by DR00ME

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

Posted

Try this code, just call it from a button and see the difference

 

Dim X As New String("ABCDEFG", 2, 1)
       Dim Y As New String("C")
       Dim Z As New String("C"c, 10)
       MsgBox("X = " & X & vbCrLf & _
           "Y = " & Y & vbCrLf & _
           "Z = " & Z)

 

the X variable is equivilent to the substring function/method

 

Y is the equivilent to just making a new string

 

Z is feeding it a character (the little c tells it that its a character and not a string - which is an array of characters) and telling it how many times to repeat itself.

 

Each uses an overloaded New method.

 

When you create your variable, some classes require that you use the new command, because they require input.

 

In this case a string doesn't. You can make a string just empty and fill it later.

 

If you don't want to, you can declare it with one of these three built in constructors. A constructor is the method that builds - or sets up the class.

 

A common constructor is passing in variables you don't want to expose as properties... maybe a filepath for a class that saves data to your hard drive... maybe its just parameters to tell the class how to act.

 

But regardless, whenever you declare an object (a variable is an object) and use the "New" keyword, you're initializing the object with the parameters you feed in.

 

You can use this to pass different kinds of objects between each other, like dataAdapters, datasets, datarows, strings, collections... Collections are really big in OOP.

 

Well I hope that helped some.

 

If I may suggest, theres a book called Object Oriented Programming using Visual Basic by O'Reilly books. Its pretty damn awesome. I'm on like page 70 (of 300 pages) and its already covered all of this stuff and moved onto overloading, shadowing, inheritence, etc.

Posted
OK thx, can you be more specific and try not to use words like an instance and constructor :D ok I know whats a constructor in C++ ...but VB....some example would lighten me .... :)

 

actually what is an instance ? lol

 

You need to either;

 

1) Buy a "how to program" VB.NET book.

2) Read "how to program" VB.NET tutorials on the net.

 

I suggest getting a book.

Gamer extraordinaire. Programmer wannabe.
Posted (edited)

Denaes cheers, that lightened me.

 

 

P.S. One question more, if I construct something with new() do I have to destruct it with finalize() ?

Edited by DR00ME

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

Posted
yes, but in .NET is used the dispose method, so sometimes you will need to use the IDisposable interface for that. Read the book, LEARN VB .NET IN 21 DAYS, will be helpful, you need to get familiar with the terms used, like instances, constructors, destructors, types, etc.
Fat kids are harder to kidnap
Posted

Heres the link to the O'Reilly site: http://www.oreilly.com/catalog/objectvbnet/

 

Object-Oriented Programming with Visual Basic .NET

By J. P. Hamilton

 

Honestly its one of the best written books I've read. I just read 90 pages without touching a computer. Just straight through. True, I do need to go back and practice some of those concepts, but its very readable.

 

It doesn't have grand Apps to built, but it does build classes to example what its teaching... it fully teaches you delegates before telling you (in the next chapter) how to get the same effect with events.

 

P.S. One question more, if I construct something with new() do I have to destruct it with finalize() ?

 

Its a very good idea to have a finalize method to destroy everything and a Dispose method to allow users to mark it for pickup. This is all in that OOP in VB.Net book, you see examples of existing classes, descriptions of what finalize does, why to include Dispose, etc.

 

Even if you don't use New, you still want to have a finalize and dispose method. You still probobly have objects floating around inside of your class, wether you use new or not.

 

Even if you don't really want to OOP, You can learn a lot from this book, which really picks up were most "learn vb in 20 days" and "Master VB.Net" books leave off. Some overlap, but its more of a mid ranged book.

 

If you don't know the basics, you might want to pick up VB.Net in a Nutshell or something

Posted
You only need a Dispose (inherit the interface IDisposable) method if you need to cleanup managed and/or unmanaged resources that your class uses. So no, just because you have a constructor does not mean you need to have a Dispose method. And the only time you ever need a destructor is if you have Dispose, and that's only used "just in case" the client forgot to Dispose of your class. The destructor should never be relied on.
Gamer extraordinaire. Programmer wannabe.
  • Administrators
Posted (edited)

Creating a Finalize method can result in your class taking longer to be garbage collected by the runtime. All class that have a finalize method get place in a queue behind the scenes and when the object goes out of scope it waits till garbage collection occurs, then calls the .Finalize method and only then is this particular object eligible for Garbage collection i.e. on the next pass of the collector.

If your object maintains non-managed / expensive / limited resources (file handles, data base connections etc.) then a finalizer is probably required, any other scenario and they probably are not.

If you are going to implement a Finalizer then you should also implement IDisposable as wyrd suggested - this way you allow the calling code to call your clean-up at a known point in time, but if you forget then the GC will get it eventually (note the word eventually - there is no way to determine exactly when).

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

Good stuff.... I think I'm gonna "check out" Hamilton's book and if it is worth to buy...

 

I got this one book by Michael Halvorson but it is useless... black book which got this triangle on it....there's nothing new in it... I think I need one of those OOP books...

 

P.S. I have used .dispose whenever possible to clean up the garbage...

Edited by DR00ME

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

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...