The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > The ProgrammersTalk > General Talk


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 09-18-2007, 11:58 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon10 What's the difference between static and non-static method?

Can anybody explain me what's the difference between static and non-static method? Why do we want to use one over another..? For some reason, it just hit me on my back while I'm doing this game project. VS showed me an error that says:

Quote:
Error 1 A field initializer cannot reference the nonstatic field, method, or property 'GameStateManagement.GameplayScreen.weapons' C:\Documents and Settings\HelloWorld\My Documents\Game Projects\GameStateManagementSample\GameStateManage mentSample\GameStateManagement\Screens\GameplayScr een.cs 53 29 GameStateManagementWindows
It's from this code below:

PHP Code:
        String[] weapons = { "Rocket""Machine Gun" };
        
String weaponType weapons[0]; 
Thanx for all

Solution is to declare weapons as static, though, I'm not asking for solution

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 09-19-2007, 05:49 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
A static method can be directly called from an object... whereas a non-static method must instantiate the object first.

Non-Static:
Dim Chet as New Person("Chet","aaa@bbb.com")
Msgbox(Chet.GetFirstName())

Static:
Msgbox(Chet.GetFirstName()) - No need to instantiate a Person Obj. but remember that your PRivate members that you rely on to "GetFirstName" must also be static... even if it is Private.

Generally if you will build a Utility class that is just all static methods you can directly call that your app or website uses a lot like IsNumeric or IsEmail, etc...
Reply With Quote
The Following User Says Thank You to ccoonen For This Useful Post:
HelloWorld (09-20-2007)
  #3 (permalink)  
Old 09-20-2007, 01:09 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Why do want to use one over another, is there any advantages and disadvantages by using static over non-staic method? Or just if we're lazy to instantiate it...?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 09-20-2007, 03:15 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 416
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
The very frequent example as justification for static variables is mathematical constants. No need to constantly instantiate a math class just to get Math.PI

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 09-21-2007, 06:21 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
I'm a big fan of Creating your class, having private setters/public getters and have Static Methods in the class to instantiate and return a list of itself

Like if you had a class Person


imports system.componentmodel

Public Class Person
Private _FirstName as String
Private _LastName as String

Public Property FirstName() as String
.......
.......

Public Sub New(ByRef FirstName as STring, byref LastName as STring)
me.firstname = firstname
me.lastname = lastname
end sub

Public Function GetAllPeople() as BindingList(Of Person)
Dim People as new BindingList(Of Person)
' Hit DB and get Datatable of all people
For each PDR as PeopleDataRow in PeopleDataTable
people.Add(PDR)
Next
return people
end function

end class

This way it's always a 1liner for you objects directly to a control... then just define what matches what

PeopleDropdownList.DataSource = Person.GetAllPeople()
PeopleDropdownList.DisplayMember = "FirstName"
PeopleDropdownList....... = whatever....
Reply With Quote
The Following User Says Thank You to ccoonen For This Useful Post:
HelloWorld (09-23-2007)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 10:39 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50