Search Results

Search found 24391 results on 976 pages for 'static methods'.

Page 5/976 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • PHP - static DB class vs DB singleton object

    - by Marco Demaio
    I don't want to create a discussion about singleton better than static or better than global, etc. I read dozens of questions about it on SO, but I couldn't come up with an answer to this SPECIFIC question, so I hope someone could now illuminate me buy answering this question with one (or more) real simple EXAMPLES, and not theoretical discussions. In my app I have the typical DB class needed to perform tasks on DB without having to write everywhere in code mysql_connect/mysql_select_db/mysql... (moreover in future I might decide to use another type of DB engine in place of mySQL so obviously I need a class of abstration). I could write the class either as a static class: class DB { private static $connection = FALSE; //connection to be opened //DB connection values private static $server = NULL; private static $usr = NULL; private static $psw = NULL; private static $name = NULL; public static function init($db_server, $db_usr, $db_psw, $db_name) { //simply stores connections values, withour opening connection } public static function query($query_string) { //performs query over alerady opened connection, if not open, it opens connection 1st } ... } or as a Singletonm class: class DBSingleton { private $inst = NULL; private $connection = FALSE; //connection to be opened //DB connection values private $server = NULL; private $usr = NULL; private $psw = NULL; private $name = NULL; public static function getInstance($db_server, $db_usr, $db_psw, $db_name) { //simply stores connections values, withour opening connection if($inst === NULL) $this->inst = new DBSingleton(); return $this->inst; } private __construct()... public function query($query_string) { //performs query over already opened connection, if connection is not open, it opens connection 1st } ... } Then after in my app if I wanto to query the DB i could do //Performing query using static DB object DB:init(HOST, USR, PSW, DB_NAME); DB::query("SELECT..."); //Performing query using DB singleton $temp = DBSingleton::getInstance(HOST, USR, PSW, DB_NAME); $temp->query("SELECT..."); My simple brain sees Singleton has got the only advantage to avoid declaring as 'static' each method of the class. I'm sure some of you could give me an EXAMPLE of real advantage of singleton in this specific case. Thanks in advance.

    Read the article

  • java unload static fields

    - by Alina Danila
    I have a java class that uses complex static fields which need special operations as close() so that they are safely cleaned by GC. For the initialization of static fields I use the static block. But I don't now how to unload the static field safely, so that I can call the close() method before the field is cleaned up by GC. Is there any way to unload a static field, similar to the static initialization block?

    Read the article

  • Control Reference Static Method Performance

    - by dotnetguts
    I have just asked which one is better? Static Vs Non-Static? http://stackoverflow.com/questions/3016717/static-vs-non-static-method-performance-c I would like to take this discussion one step ahead. Consider If i pass reference of Panel control as parameter to Public static method, will static method still rules in performance?

    Read the article

  • Static member object of a class in the same class

    - by Luv
    Suppose we have a class as class Egg { static Egg e; int i; Egg(int ii):i(ii) {} Egg(const Egg &); //Prevents copy-constructor to be called public: static Egg* instance() {return &e} }; Egg Egg::e(47); This code guarantees that we cannot create any object, but could use only the static object. But how could we declare static object of the same class in the class. And also one thing more since e is a static object, and static objects can call only static member functions, so how could the constructor been called here for static object e, also its constructors are private.

    Read the article

  • How to bundle extension methods requiring configuration in a library

    - by Greg
    Hi, I would like to develop a library that I can re-use to add various methods involved in navigating/searching through a graph (nodes/relationships, or if you like vertexs/edges). The generic requirements would be: There are existing classes in the main project that already implement the equivalent of the graph class (which contains the lists of nodes / relationships), node class and relationship class (which links nodes together) - the main project likely already has persistence mechanisms for the info (e.g. these classes might be built using Entity Framework for persistance) Methods would need to be added to each of these 3 classes: (a) graph class - methods like "search all nodes", (b) node class - methods such as "find all children to depth i", c) relationship class - methods like "return relationship type", "get parent node", "get child node". I assume there would be a need to inform the library with the extending methods the class names for the graph/node/relationships table (as different project might use different names). To some extent it would need to be like how a generics collection works (where you pass the classes to the collection so it knows what they are). Need to be a way to inform the library of which node property to use for equality checks perhaps (e.g. if it were a graph of webpages the equality field to use might be the URI path) I'm assuming that using abstract base classes wouldn't really work as this would tie usage down to have to use the same persistence approach, and same class names etc. Whereas really I want to be able to, for a project that has "graph-like" characteristics, the ability to add graph searching/walking methods to it.

    Read the article

  • Javascript static method intheritance

    - by Matteo Pagliazzi
    I want to create a javascript class/object that allow me to have various method: Model class Model.all() » static method Model.find() » static method Model delete() » instance method Model save() » instance method Model.create() » static that returns a new Model instance For static method I can define them using: Model.staticMethod(){ method } while for instance method is better to use: function Model(){ this.instanceMethod = function(){} } and then create a new instance or using prototype? var m = function Model(){ } m.prototype.method() = function(){ } Now let's say that I want to create a new class based on Model, how to inherit not only its prototypes but also its static methods?

    Read the article

  • when to make a method static

    - by Don
    Hi, I'd like to know how people decide whether to define a method as static. I'm aware that a method can only be defined as static if it doesn't require access to instance fields. So lets say we have a method that does not access instance fields, do you always define such a method as static, or only if you need to call it statically (without a reference to an instance). Perhaps another way of asking the same question, is whether you use static or non-static as the default? Thanks, Don

    Read the article

  • Call methods in main method

    - by Niloo
    this is my main method that gets 3 integers from command line and I parse then in my validating method. However I have one operation method that calls 3 other methods, but i don't know what type of data and howmany I have to put in my operatinMethod() " cuase switch only gets one); AND also in my mainMethod() for calling the operationMehod(); itself? please let me know if i'm not clear? Thanx! main method: public class test { // Global Constants final static int MIN_NUMBER = 1; final static int MAX_PRIME = 10000; final static int MAX_FACTORIAL = 12; final static int MAX_LEAPYEAR = 4000; //Global Variables static int a,b,c; public static void main (String[] args) { for(int i =0; i< args.length; i++){} if(validateInput(args[0],args[1],args[2])){ performOperations(); } } //Validate User Input public static boolean validateInput(String num1,String num2,String num3){ boolean isValid = false; try{ try{ try{ a = Integer.parseInt(num1); if(!withinRange(a,MIN_NUMBER, MAX_PRIME)) { System.out.println("The entered value " + num1 +" is out of range [1 TO 10000]."); } isValid = true; } catch(Exception ex) { System.out.println("The entered value " + num1 + " is not a valid integer. Please try again."); } b = Integer.parseInt(num2); if(!withinRange(b,MIN_NUMBER, MAX_FACTORIAL)) { System.out.println("The entered value " + num2 +" is out of range [1 TO 12]."); } isValid = true; } catch(Exception ex) { System.out.println("The entered value " + num2 + " is not a valid integer. Please try again."); } c = Integer.parseInt(num3); if(!withinRange(c,MIN_NUMBER, MAX_LEAPYEAR)) { System.out.println("The entered value " + num3 +" is out of range [1 TO 4000]."); } isValid = true; } catch(Exception ex) { System.out.println("The entered value " + num3 + " is not a valid integer. Please try again."); } return isValid; } //Check the value within the specified range private static boolean withinRange(int userInput ,int min, int max){ boolean isInRange = true; if(userInput < min || userInput > max){ isInRange = false; } return isInRange; } //Perform operations private static void performOperations(int userInput) { switch(userInput) { case 1: // count Prime numbers countPrimes(a); break; case 2: // Calculate factorial getFactorial(b); break; case 3: // find Leap year isLeapYear(c); break; } } // Verify Prime Number private static boolean isPrime(int prime) { for(int i = 2; i <= Math.sqrt(prime) ; i++) { if ((prime % i) == 0) { return false; } } return true; } // Calculate Prime private static int countPrimes(int userInput){ int count =0; for(int i=userInput; i<=MAX_PRIME; i++) { if(isPrime(i)){ count++; } } System.out.println("Exactly "+ count + " prime numbers exist between "+ a + " and 10,000."); return count; } // Calculate the factorial value private static int getFactorial(int userInput){ int ans = userInput; if(userInput >1 ){ ans*= (getFactorial(userInput-1)); //System.out.println("The value of "+ b +"! is "+ getFactorial(userInput)); } return ans; } // Determine whether the integer represents a leap year private static boolean isLeapYear(int userInput){ if (userInput % 4 == 0 && userInput % 400 == 0 && userInput % 100 ==0){ System.out.println("The year "+ c +" is a leap year"); } else { System.out.println("The year "+ c +" is a not leap year"); } return false; } }

    Read the article

  • static readonly field initializer vs static constructor initialization

    - by stackoverflowuser
    Below are 2 different ways to initialize static readonly fields. Is there a difference between the 2 approaches? If yes, when should one be preferred over the other? class A { private static readonly string connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString; } class B { private static readonly string connectionString; static B() { connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString; } } Thanks.

    Read the article

  • static thread function access non-static class member in C++

    - by user397390
    Class Test{ int value; static void* thread_func(void* args){ value++; } void newthread(){ pthread_create(&thread_func,...); } } I'm trying to create a thread in Class Test. Therefore compiler forces me to make thread_func static. However I cannot access the non-static member "value" anymore. It says: invalid use of member 'Class::value' in static member function Is there a way around it?

    Read the article

  • Static IPv6 address in Windows unused for outgoing connections

    - by Luc
    I'm running a Windows server and trying to get it to use a static IPv6 address for outgoing connections to other IPv6 hosts (such as Gmail). I need this because Gmail requires a ptr record, and I can't set one for random addresses. The static address is configured on the host, but it also has a temporary privacy address as well as a random address from the router it seems. By default Windows uses the privacy address; it seems this is the expected behavior (and it makes perfect sense for people/users that did not set a static address, but I did!). I've tried disabling the privacy address with: netsh int ipv6 set privacy disabled This indeed gets rid of the privacy address, but I still have the random address that the router assigned. To disable this, it was said I needed to disable "router discovery" using this command: net interface ipv6 set interface 14 routerdiscovery=disabled Upon doing this, all IPv6 connectivity is lost. If I do this while pinging Gmail, it will report "Destination host unreachable" as soon as I enter the command. In the static IPv6 configuration, I did configure the default gateway and prefix length, so I don't see why it's unable to connect. Probably has something to do with the lack of ARP in IPv6 and somehow being unable to resolve the router's MAC, but I wouldn't know how to fix this. Finally I've tried disabling the DHCPv6 lease with these commands: netsh interface ipv6 set interface "IDMZ Team" managedaddress=disabled netsh interface ipv6 set interface "IDMZ Team" otherstateful=disabled Which was to no avail; the host continues to obtain and use the router-assigned IPv6 address. The router is a FritzBox 7340, which shows me all the IPv4 and IPv6 addresses that the host (identified by MAC) utilizes, but I'm unable to change the assigned address. Maybe this could be done over the telnet interface of the router somehow, but again, I wouldn't know how to do this even if it's the way to go. In short, any of the following would probably solve my problem: Change Windows' source address selection behavior. Have Windows not get an address from the router and not generate a privacy address; Have the router hand out a static address and make Windows use that as source address. Recover connectivity after disabling router discovery on Windows. Alternatively I might use some (batch, perl, ...) script to throw away all IPv6 addresses except the desired one, but this feels rather hacky. If it's the only way (or less hacky than another hacky solution), it might be an option though. Thanks!

    Read the article

  • Swap static public IPs without creating DNS conflicts?

    - by Jakobud
    Our ISP is Comcast and we have 5 static public IPs from them that we use for various services, including customers connecting to our network, VPN, web, DNS, etc... We need more IP addresses from Comcast. Unfortunately, Comcast is telling us that they can't just simply give us 5 more addresses. They only give static IP addresses in blocks of 1, 5 or 13. In order for us to get more static IPs, they have to take away our current 5 static IPs and give us 13 new ones. How do we make this transition without causing all sorts of DNS chaos? We run public DNS servers, so we can make the DNS changes ourselves, but it will take some time obviously for those DNS changes to propagate throughout the internet. Are there any easy ways to make this transition? Like create some type of fallback DNS entry or something? Surely there must be some sort of procedure for this kind of thing. The Comcast support guy was useless.

    Read the article

  • Ubuntu 8.04 server is not retaining a static IP address

    - by James Pierce
    I recently setup a linux box running Ubuntu 8.04 (to match another server with 8.04). I need to insure that this box has a static IP address and I changed /etc/network/interfaces to set up the static IP address and when I run sudo /etc/init.d/networking restart it works fine for a while, but always reverts back to 10.0.1.24 after being idle for a while. I also tried stopping/removing the dhcp client, but that didn't help. sudo /etc/init.d/dhcp stop sudo apt-get remove dhcp3-client Here is my /etc/init.d/networking: # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static address 10.0.1.4 netmask 255.255.255.0 broadcast 10.0.1.255 gateway 10.0.1.1 Any thoughts? Thanks.

    Read the article

  • Static linking in Qt --> link errors in VS 2008

    - by ChruS
    Today I dediced to make static linking in Qt. I used Qt4 with Visual Studio and static C runtime article. The 3rd step took quite a long time. When it was finished I opened my project in VS 2008, made Build->Clean Solution and try to Release. Unfortunately I got link errors: warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library error LNK2019: unresolved external symbol "private: static struct QString::Data QString::shared_null" (?shared_null@QString@@0UData@1@A) referenced in function "public: __thiscall QString::QString(void)" (??0QString@@QAE@XZ) error LNK2019: unresolved external symbol "private: static struct QByteArray::Data QByteArray::shared_null" (?shared_null@QByteArray@@0UData@1@A) referenced in function "public: __thiscall QByteArray::QByteArray(void)" (??0QByteArray@@QAE@XZ) error LNK2001: unresolved external symbol "private: static struct QByteArray::Data QByteArray::shared_null" (?shared_null@QByteArray@@0UData@1@A) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2019: unresolved external symbol "public: static enum QSysInfo::WinVersion const QSysInfo::WindowsVersion" (?WindowsVersion@QSysInfo@@2W4WinVersion@1@B) referenced in function _WinMain@16 What i did wrong? Help to fix this pls.

    Read the article

  • Static route in conflict with a default route

    - by Ossan Sokiv
    Hi guys, I have a default route configured. 192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.1 I'd like to add a static route to pass traffic destined for 192.168.1.51 via a load balancer's redundant virtual interface at 192.168.1.2. ip route add 192.168.1.51 mask 255.255.255.255 via 192.168.1.2 When I try to add the static route I get this error. Error: either "to" is duplicate or "default" is garbage." It doesn't want to add the static route because it's in conflict with the default route. Is there a way around this? Regards Ossan

    Read the article

  • Problem Assigning Static IP to CentOS Server

    - by nategood
    We have a sandbox server running CentOS that we run inside our office. Our ISP has assigned us a block of 5 static IPs. We now want to assign it a static IP. DEVICE=eth0 BOOTPROTO=none # have also tried "static" here HWADDR=00:13:72:*:*:* ONBOOT=yes TYPE=Ethernet NETMASK=255.255.255.0 IPADDR=173.*.*.161 GATEWAY=10.1.10.1 /etc/resolv.cnf is also set with the appropriate name servers from our ISP. When I ifdown eth0 then ifup eth0 I get... SIOCADDRT: Network is unreachable When I switch to DCHP, the machine has an IP assigned and there are no connection problems. Any ideas?

    Read the article

  • nginx static files caching doesn't work

    - by user74344
    here is my conf file: usr/local/nginx/sites-available/default server { listen 80; server_name localhost; location / { root html; index index.php index.html index.htm; } # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # serve static files directly location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|swf)$ { expires 30d; } but it doesn't cache static files, how should I fix it? thanks a lot

    Read the article

  • Static linking in Qt --> link errors in VS 2008

    - by ChruS
    Today I dediced to make static linking in Qt. I used Qt4 with Visual Studio and static C runtime article. The 3rd step took quite a long time. When it was finished I opened my project in VS 2008, made Build->Clean Solution and try to Release. Unfortunately I got link errors: warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library error LNK2019: unresolved external symbol "private: static struct QString::Data QString::shared_null" (?shared_null@QString@@0UData@1@A) referenced in function "public: __thiscall QString::QString(void)" (??0QString@@QAE@XZ) error LNK2019: unresolved external symbol "private: static struct QByteArray::Data QByteArray::shared_null" (?shared_null@QByteArray@@0UData@1@A) referenced in function "public: __thiscall QByteArray::QByteArray(void)" (??0QByteArray@@QAE@XZ) error LNK2001: unresolved external symbol "private: static struct QByteArray::Data QByteArray::shared_null" (?shared_null@QByteArray@@0UData@1@A) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2019: unresolved external symbol "public: static enum QSysInfo::WinVersion const QSysInfo::WindowsVersion" (?WindowsVersion@QSysInfo@@2W4WinVersion@1@B) referenced in function _WinMain@16 What i did wrong? Help to fix this pls.

    Read the article

  • Why is this static routing not working ?

    - by geeko
    Greeting gurus, I'm trying to develop a DHCP enforcement extension like Microsoft NAP. My trick to block dynamic-IP requesting machines (that don't meet certain policy) is to strip the default gateway (no default gateway) stated in the IP lease and set the lease subnet mask to 255.255.255.255. Now I need the blocked machines to be able to reach some specific locations (IPs) on the network. To allow for this, I'm including some static routes in the lease. For example, I'm including 10.10.10.11 via router 10.10.10.254 (the one to which the blocked machine that needs to access 10.10.10.11 is connected). Unfortunately, as soon as I set the default gateway to nothing, blocked machines cannot reach any of the added static routes. I also tried classless static routes. Any ideas ? any one knows how MS NAP actually do it ? Geeko

    Read the article

  • Modify static variables while debugging in Eclipse

    - by sleske
    As an extension the the question "Modify/view static variables while debugging in Eclipse", I'd like to be able to modify static variables while debugging in Eclipse. For instance and local variables, I can just choose the variable in the "Variables" view of Eclipse, and use the context menu "Change value..." to change the value. This is not possible for arbitrary static variables, because they do not appear in the "Variables" view. What I tried: If you choose "Java / Show static variables" from the triangle menu in the "Variables" view, you can see and modify static member variables of the variables listed in the "Variables view". However, I did not find how to access a static member of a class whose instance does not appear in the "Variables view". You can of course enter a static member as an expression into the "Expression view" (using fully qualified name). Then you can see the value, but the "Expression view" does not have an option to modify the value (it does allow to modify members of an expression, but not the expression itself, even if the expression is a field). So, if I have a static variable like a boolean MyClass.disableAllBugs, is there a way to change MyClass.disableAllBugs during debugging? As an aside: I realize that even having public mutable static fields (i.e. mutable global variables) is very bad style. But some codebases have it, and then it's sometimes useful to modify it while debugging.

    Read the article

  • assign auto static ip on ubuntu 10.04

    - by ronakin
    I'm trying to set auto static ip. I've set the content of /etc/network/interfaces to be: auto lo auto eth0 iface eth0 inet static address 192.168.1.2 netmask 255.255.255.0 gateway 192.168.1.1. and /etc/resolv.conf to be: nameserver 192.168.1.1. It seems that the ip address have set successfully. However, when I plug out the lan cable and then plug it back, the ip address is not set. How can I make it automatically set the static ip when the lan cable is connected?

    Read the article

  • Using Google's App Engine as CDN for static files

    - by Saif Bechan
    I am planning on moving my static files to Google's App Engine. I was wondering if this is a good idea to do. I have read that is it possible that Google will cache your files on multiple locations, which is a good thing in my opinion. The setup should also be quite easy in eclipse with the GAE plugins. But i still have my doubts on the performance of this. Is the setup of App Engine optimized for serving static content. Now I have Nginx server my static content, will App Engine perform the same way. Are there any other ups or downs using this method?

    Read the article

  • Stepping into Ruby Meta-Programming: Generating proxy methods for multiple internal methods

    - by mstksg
    Hi all; I've multiply heard Ruby touted for its super spectacular meta-programming capabilities, and I was wondering if anyone could help me get started with this problem. I have a class that works as an "archive" of sorts, with internal methods that process and output data based on an input. However, the items in the archive in the class itself are represented and processed with integers, for performance purposes. The actual items outside of the archive are known by their string representation, which is simply number_representation.to_s(36). Because of this, I have hooked up each internal method with a "proxy method" that converts the input into the integer form that the archive recognizes, runs the internal method, and converts the output (either a single other item, or a collection of them) back into strings. The naming convention is this: internal methods are represented by _method_name; their corresponding proxy method is represented by method_name, with no leading underscore. For example: class Archive ## PROXY METHODS ## ## input: string representation of id's ## output: string representation of id's def do_something_with id result = _do_something_with id.to_i(36) return nil if result == nil return result.to_s(36) end def do_something_with_pair id_1,id_2 result = _do_something_with_pair id_1.to_i(36), id_2.to_i(36) return nil if result == nil return result.to_s(36) end def do_something_with_these ids result = _do_something_with_these ids.map { |n| n.to_i(36) } return nil if result == nil return result.to_s(36) end def get_many_from id result = _get_many_from id return nil if result == nil # no sparse arrays returned return result.map { |n| n.to_s(36) } end ## INTERNAL METHODS ## ## input: integer representation of id's ## output: integer representation of id's def _do_something_with id # does something with one integer-represented id, # returning an id represented as an integer end def do_something_with_pair id_1,id_2 # does something with two integer-represented id's, # returning an id represented as an integer end def _do_something_with_these ids # does something with multiple integer ids, # returning an id represented as an integer end def _get_many_from id # does something with one integer-represented id, # returns a collection of id's represented as integers end end There are a couple of reasons why I can't just convert them if id.class == String at the beginning of the internal methods: These internal methods are somewhat computationally-intensive recursive functions, and I don't want the overhead of checking multiple times at every step There is no way, without adding an extra parameter, to tell whether or not to re-convert at the end I want to think of this as an exercise in understanding ruby meta-programming Does anyone have any ideas? edit The solution I'd like would preferably be able to take an array of method names @@PROXY_METHODS = [:do_something_with, :do_something_with_pair, :do_something_with_these, :get_many_from] iterate through them, and in each iteration, put out the proxy method. I'm not sure what would be done with the arguments, but is there a way to test for arguments of a method? If not, then simple duck typing/analogous concept would do as well.

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >