C#: can you make it so that a method parameter passes an object by reference but is read-only?
eg:
void MyMethod(int x, int y, read-only MyObject obj)
where obj is an object reference but this object cannot be modified during the method.
Can this be achieved in C#?
...
I am using SWIG to access C++ code from Python. How do I elegantly wrap a function that returns values in variables passed by reference like
void set(double&a) {
a = 42.;
}
I could not find out how to do this. In the best case I'd be able to use the function in Python with Python floats:
>>> b = 2.
>>> set(b)
>>> print b
42.0
At ...
Should I use "ref" to pass a list variable by reference to a method?
Is the answer that "ref" is not needed (as the list would be a reference variable), however for ease in readability put the "ref" in?
...
I was kind of baffled when I saw the following code did not work as expected.
I thought Java always passed variables by references into functions. Therefore, why the function assign the variable?
public static void main(String[] args) {
String nullTest = null;
setNotNull(nullTest);
System.out.println(nullTest);
}
private st...
Hey guys,
I have this very akward question...
void changeString(String str){
str = "Hello world":
}
main(){
String myStr = new String("");
changeString(myStr);
}
When main returns the value is still "" and not "Hello world" Why is that?
Also, how do I make it work? Lets say I want my function changeString to change the...
Hello everyone,
I am currently struggling to get the following code to compile. First the header file containing a class with a method template:
// ConfigurationContext.h
class ConfigurationContext
{
public:
template<typename T> T getValue(const std::string& name, T& default) const
{
...
}
}
Somew...
Hello
I know that the C++/CLI code
void foo(Bar^% x);
transforms into
Void foo(ref Bar x);
What is the C++/CLI code that becomes
Void foo(out Bar x);
?
...
In the class fooBase there is SomeProperty. In the class barBase, I have a field of type fooBase:
public class fooBase
{
public object SomeProperty { get; set; }
}
public class barBase
{
protected fooBase _foo;
}
It's clear that from barBase I can change SomeProperty by _foo.SomeProperty = whatever;.
Now in the derived clas...
Hello,
(Disclaimer: I have removed the Qt tag in case the problem is in my syntax / understanding of the references involved here)
I have a foreach loop with an object Member. When I enumerate through the list and try to access a member field, the debugger stops and I get a message:
Stopped: 'signal-received' -
The assert failure is...
I am a bit new to C# and was just wondering if there is a list of classes (commonly used) which are by default passed as copy. How can I identify them?
I know the basic basic object types — int, uint, float, strings, ... — are passed by copy.
...
With PHP is it even Possible to Pass arrays by Reference ? or its a Bug Only for Me.
class MyStack{
private $_storage = array();
public function push(&$elem){//See I am Storing References. Not Copy
$this->_storage[] = $elem;
}
public function pop(){
return array_pop($this->_storage);
}
publi...
Hi,
can somebody please tell me where i m wrong why this RMI chat application not working,the goal is to achieve decoupleing between client, server and logic by remote objects or serialized objects.
import javax.swing.*;
import java.awt.event.*;
import java.rmi.*;
import java.rmi.server.*;
public class ChatClient1...
Assume the following code, without any ref keyword, that obviously won't replace the variable passed, since it's passed as value.
class ProgramInt
{
public static void Test(int i) // Pass by Value
{
i = 2; // Working on copy.
}
static void Main(string[] args)
{
int i = 1;
ProgramInt.Test(i);
...
Hi,
I am using a method that has the following signature:
public static bool TryAuthenticate(string userName, string password,
string domainName, out AuthenticationFailure authenticationFailure)
The method declares: bool authenticated = false; then goes on to authenticate the user.
Whenever authenticated is set to true or fals...
Hi,
I'm currently working on an FTP client written in C, and it's working pretty good. I was successful in writing a function that connects to an FTP server and logs in with a username or password, but I'm having a problem with returning errors. I have setup a struct FTPError {}; with 3 fields:
int An error code
int An FTP error domain...
Possible Duplicates:
When pass-by-pointer is preferred to pass-by-reference in C++?
Are there benefits of passing by pointer over passing by reference in C++?
How to pass objects to functions in C++?
Hi all,
I'm working to develop a large object oriented c++ application framework as part of my chemical engineering graduate...
I have code similar to this in c++. It aborts when i try to run it. Would this type of code work ?
In the main function :
type* a = something
type* b = something
func1(a,b);
func1 declaration:
void func1(type* &a, type* &b){
func2(a,b);
// do something
}
func2 is as follows
void func2(type* &a, type* &b){
// do somethin...
I've seen both before, and as far as I know, it's pretty much subjective, but if given the option, which would you do and why? If the data were large, would there be any speed/memory benefit to one of them?
function processData(&$data_to_process) { // Pass by reference.
// do something to the data
}
// ... somewhere else
$this->p...
I have a function that takes a ref parameter and would like to use it in a linq query but the compiler complains.
The function is called BreakLine and breaks a string up into lines based on a line length, the ref parameter is used to keep track of where it is in the string on each call:
string BreakLine(string text, int lineLimit, ref ...
I would like to call a method which could potentially take on different versions, i.e. the same method for input parameters that are of type:
boolean
byte
short
int
long
The way I would like to do this is by "overloading" the method (I think that is the correct term?):
public void getValue(byte theByte) {...}
public void getValue(sh...