C Sharp :: Why Use Static Method In C#
Feb 17, 2013what is static method?
why we can use static method inc#?
what is static method?
why we can use static method inc#?
How to use static in a class, function and variable.
View 1 Replies View RelatedI've been trying for more than one month to access a method found in a library called libcocosnas_static.a. I'm using Cocos2d-X version 2.0.4. The library has been used many times by my company to make games using cocos2d-1.0.1-x-0.12.0 without any problem.
This is what I've done:
1- I added the include paths of the library to both eclipse and Android.mk
2- Included the .h file using #include "NASPlatformUtil.h"
3- Added the libcocosnas_static.a file to the proj.androidobjlocalarmeabi folder
4- Added "LOCAL_WHOLE_STATIC_LIBRARIES += cocosnas_static" to the Android.mk file
5- Called the function using: NASPlatformUtil:: openUrl("http://xxx.xxx.com/");
I can right click on the function, click Open Declaration and get it without any problem, but the compiler keeps on giving me that dreaded error...
Have following code:
class Program
{
static void Main(string[] args)
{
[Code]....
My question according to what i just wrote:
1. Is that mean that Do() is only available for use by Dog itself because Dog is 'oryginal' Dog, and if i create new dogs - instances of oryginal Dog (dog1, dog2 ...) they cant access because Do is only available fo 'oryginal' one? Is that correct thinking?
2. If i would want to have something common (e.g value) for all dogs is that good way to create static field/method for Dog instead of non-static once then all instances of Dog would access Dog static member to get/change it? Just stupid example: static method GetAmountOfLegs() which return 4 Then all instances can take/call that value from Dog. Is that correct thinking?
I declared a member method to a class in its header file and implemented it in the cpp file. When I build and run the project in XCode, everything works fine. When I try to do it with a makefile, I get undefined symbols linker errors.
parser.h
Code:
#ifndef ENGINE_SCRIPT_PARSER_H
#define ENGINE_SCRIPT_PARSER_H
#include <string>
#include "variable.h"
namespace ninja_game_engine {
[Code] ....
The exact makefile looks like this:
Code:
test:
g++
Code/Engine/Modules/Timer/timer.cpp
Code/Engine/Modules/list.cpp
[Code] ....
I'm pretty sure that there is a weird namespace gotcha that I'm unaware of that LLVM (default OSX compiler) is compensating for that g++ isn't. Or maybe something weird with the optimization? I want the tests running at that level to make sure everything that is volatile is declared as such.
Rest of the code located here: [URL] ....
I would like to avoid throwing things in constructors as much as possible.
Is this good design to have a static class method that checks arguments the caller will give to the constructor. The documentation of the class will say, thou shall call this method to validate thine arguments before calling the constructor, or else segfault may befall thoust.
I have function that returns historical data. I can access it, using file name. If I use file name, it reads that file and saves it to dictionary, so that in the future, if historical data is required for the same file, it does not read it again (it's lazy loading). If no file is supplied to the function, it tries to read file which is given in app settings.
However, for unit testing, I do not want to read any file. Instead, I want it to use small sample of hardcoded historical data. In order to do that, I think, I need to introduce interface to it. Then I can use some IoC to choose between different implementation for unit testing purpose and ordinary launch of application.
Function to get history is given as follows:
public static class Auxiliary
{
private static Dictionary<string, MyData> _myData;
public static MyData GetData(string fileName = null)
{
// ...
}
}
I have created default Unit Test project with Visual Studio so, as far as I know, by default it uses MSTest as test runner and MSUnit as unit testing framework but it does not have any IoC container so I should manage NuGet packages for solution and install Unity.
As far as I know, MSUnit (aka Moles) can unit test static methods (it's unconstrained isolation framework, like Typemock Isolator, unlike NUnit) but still many people suggest not to use any static methods for unit testing.
Should I use shim or stub [URL] Stubs should be used for faking external dependencies and here it is not external library, but my own code.
I am writing my program on C++ language. I have one promblem. I need to set signal handler for my process. As the signal is related with the process on system level I have faced the problem.
My program consists several classes. They are connected together. But it doesn't metter in this case. The problem is that I need to access to member and methods of the class from my signal handler. For instance , I have class named Foo at it has some members and methods.
So from my handler I need to call its function and change members.
I understand that compiler should know that this class instances will exist during all program execution.
I have tried to set static member class Foo instance in another class , but this didn't solve the problem.
What is correct approach to do this. How to correctly implement signal handling in such case.
Here is example of my code
Code:
class MyContainer{
private:
std::vector<Foo> container;
public:
int removeFromContainer(Foo* aFoo) {
[Code] .....
can we give the restriction to method for a particular class
View 1 Replies View RelatedI have the following method:
public string[,] StringConvert_tblVFWPost(DataTable dt1) {
string[,] stringArray = new string[dt1.Rows.Count, dt1.Columns.Count];
for(int row = 0; row < dt1.Rows.Count; ++row) {
for(int col = 0; col < dt1.Columns.Count; col++)
[Code] ....
The error I'm getting is "Cannot implicitly convert type 'string[*,*]' to 'string'". So that tells me what the issue is. However, I'm not sure how to fix it.
How do I change this method, so that it properly returns my two dimensional array?
In my code, I wanted to write log exception to some file.
So I created a Utility class & wrote a static method in that to write log message to file.
public static void WriteLog(string message) {
using (FileStream fs = new FileStream(@"c:log.txt", FileMode.Append)) {
using (StreamWriter streamWriter = new StreamWriter(fs)) {
streamWriter.WriteLine(message);
}
}
}
This works fine so far but I fear if two methods call this function simultaneously.. what will happen? Also, I want to access this same Utility library in my other "WEB" projects... will it work there too?Or else.. what will be the best way to log exceptions in any project?
I sew lot of sample for ASP WEB API. In althose link the post method is using
HttpResponseMessage as return tyope
Is it possible to use return type bool in WEB API post method?
From my book:
"A static function might have this prototype:
static void Afunction(int n);
A static function can be called in relation to a particular object by a statement such as the following:
aBox.Afunction(10);
The function has no access to the non-static members of aBox. The same function could also be called without reference to an object. In this case, the statement would be:
CBox::Afunction(10);
where CBox is the class name. Using the class name and the scope resolution operator tells the compiler to which class Afunction() belongs."
Why exactly cant Afunction access non-static members?
What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B. Any pointers?
View 7 Replies View RelatedI have a question similar to the one here: [URL] .....
The main difference is I would like to pass a method of derived class as a parameter to some other method in its template base class.
template <typename BaseType>
class Base {
public:
typedef void (Base::*Callback)(int A);
[Code] .....
The above is an example which does not compile. My compiler complains that the two BaseMethod() calls in DerivedMethod() are invalid uses of non-static member function.
Is this not possible to do, or is my syntax simply wrong? All I want is to be able to pass as an an argument to a method in the base class from the derived class some callback as a variable for the base class to invoke later.
Here is the code,
Code:
class A {
};
A& CreateObject() {
static A a;
return a;
} static A aa;
int main() {
return 0;
}
So is there any difference between a defined in CreateObject and aa?
This problem relates to a program i am developing to ping all hosts on a network.
So far I've managed to access a PostgreSQL database to retrieve a CIDR network address e.g. 192.168.52.14/24.
I have then managed to mask the bits to get the network address.
So for e.g. above its 192.168.52.0
I now need some kind of algorithm to loop through available hosts so from.
192.168.52.0 - 192.168.52.255
I have got as far as deriving the network and separating it into an int array
So I have : ip_start[]= {192,168,52,0}
#include <iostream>
using namespace std;
struct A {
virtual void f() { cout<<"A
"; }
};
[code]...
I would expect that both examples 2 & 3 will give me the same result.I tried to figure it out but I could not. Both are references of a base class type, that get a derived object.
Q1 : why is the difference between them ?
As I see it, its kind of a mix between pointer - which in case of virtual method that was override in derived class - would give me the derived method (e.g. "B") and between regular object - which in case of virtual method that was override - would give me the specific method (Still "B"). So, example 2 "use" it as a regular object and example 3, "use" it as pointer.
Q2 : How should I refer to it ? I am using VS2008.
I have 2 classes with a Function with the same definition (both inherited from the same base class) and in my derived class I derive from both of those 2. Is it possible to use the Methods of both classes? for example with an other name?
class A {
protected:
int print(int a) { std::cout << "int A: " << a << std::endl; }
};
class B : A
[Code] ....
is there something like using C::print as printc;?
The Problem, I have a Sprite class that derives from a Rectangle with properties Position, Size, Origin and Angle and a Text class that derives from Rectangle. Now i have a Button class deriving from both Sprite and Text.
- The Position, when moving the Button i have to change the position of both so i Implemented a new Method which calls SetPosition from the Sprite and the Text.
- The SetSize just affects the Button so i just did using Sprite::SetSize;
- The angle affects both so i just implemented a new Method and hide the other two
The problem is here:
- The Origin: writing button.SetOrigin(-1,0) should set the Origin of the Button and writing button.SetTextOrigin should set the Origin of the text.
Should i just reimplement a Mehtod named SetTextOrigin and call Text::SetOrigin from there and hide the button.Text::SetOrigin or is there something like using Text::SetOrigin as SetTextOrigin?
It has been a few years since I have had to do this, but I need to declare a method in my base class, but produce no code for it. Then when this library is used by my second project I will derive a class from this base class and put the code into it there. How is this possible? I used to know how but do not remember how now.
The library is a static library designed for linking with both 32bit and 64bit Windows applications to handle a lot of the tedious stuff with Windows programming. The method in question handles specific command inputs. However, since each program that uses this library will have different uses for these commands, I want to leave it up to the user to code their own handling, but require it to be coded in the derived class.
Here is the Method Code:
public void DeleteCustomer(string firstName, string lastName,
string address1, string address2, string city,
string state, string phoneNumber, int customerID)
{
[Code]....
.... And here is the response I get:
Error Deleting customer, please check form data. Syntax error in FROM clause.
I want to access the body of the Add() of a list in c# to see how it works, but it only just gives me the declaration.
[DebuggerTypeProxy(typeof (Mscorlib_CollectionDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
[Serializable]
public class List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable {
public void Add(T item); // thats all. I tried go to declaration but still gives me this line of code. This is from metadata in Visual studio.
}
How this thing work. It just a declaration not a definition yet its still doing something. How is that possible.
how to write a "charge simulation method" program in C or C++? It's to calculate electric distribution and also electric potential of two different dielectrics. I have attached the diagram of the shape of the electrode that needs to be investigated.
View 1 Replies View RelatedAllow users to enter their name and favorite saying in a single method that gets invoked two times. If I can only return one value at a time, how am I suppose to get name and favorite saying out of UserInput()?
static void Main(string[] args) {
string displayName, favoriteSaying;
DisplayInstructions();
Console.WriteLine();//readability
[Code] ....
I have to create a program that accepts 10 numbers from user, and then I display a list of the numbers, the smallest one and the higher number. I have problems with displaying the smallest and higher number, I tried to Array.Sort and Array.Reverse, but I don't know what I'm doing wrong, this is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SmallAndLargeGUI
[Code] ...
Also I tried to set
if (x == 10)
numberTextBox.Enabled = false;
to block the textbox when the user has entered 10 numbers, but didn't work either...
I am trying to input a recursion method. The code compiles, however, it is only giving me a value of 1. I am wanting the value of 5 when it is compiled. Why is this?
#include <iostream>
using namespace std;
int number(int x) {
if (x == 1)
[Code] ....