|
Why do language designers tout how you don't have to think about passing by value vs. reference anymore in languages like C# and Java, when in reality, you reall DO have to think about it all the time!!! In fact, I find it more confusing, because these languages hide the issue of value/reference, and I find myself constantly worrying about references going out of scope. Is this just me, being a die hard C++ programmer, or what?
Especially confusing is that native types (even though they say they're objects!) are passed by value, whereas objects are passed by reference.
So:
int i=5;
foobar(i);
string s="abc";
fizbin(s);
foobar passes by value and fizbin passes by reference. I have to explicitly say:
foobar(ref i) !!!
What is with this? This seems ridiculously confusing. On one hand they say both things are objects, then they treat them differently.
Enlighten the grouch. Am I missing something really spectacular about these languages?
Marc
|
|
|
|
|
I don't really see where the confusion comes from considering all the "intrinsic" types derive from System.ValueType .
Its also much easier and safer to mark a paramater as ref than passing pointers.
|
|
|
|
|
i never understood these either and probably don't, but i have made myself a nice rule of thumb for when to use the ref parameter.
When you need more than 1 returned type, return one object and ref the others.
I know this is a very limited scope, but if you are learning like me it cuts down on extra confusion.
|
|
|
|
|
With Managed C++, we have it easy. All reference types are accessed via pointers and all value types are accessed as direct variables.
Thus I can only have a String* and never a String which won't compile.
But I can have an int or a char
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
|
|
|
|
|
now that has just totally confused me again.
COMMAND: erase Nish's previous reply from Brain.
COMMAND OK: Brain deleted. Oops
|
|
|
|
|
The way I look at it, an "object" variable is a pointer to the object, and that pointer gets passed by value (unless you specify otherwise).
You can access the methods and properties of the original object, but you can't return changes to the pointer - if you set the parameter to a different object, the original variable will still point to the original object.
Nothing confusing about that!
|
|
|
|
|
That is a good way to think about it. Thanks!
(But, in C#, all those intrinsic types are actually aliases to objects, so in theory, a pointer would be passed, which would mean they're being passed by reference!!!)
But I still like your way of thinking about it. Most of my brain can handle it.
Marc
|
|
|
|
|
Marc Clifton wrote:
But, in C#, all those intrinsic types are actually aliases to objects
Actually, anything that inherits from System.ValueType is treated as a value type - it gets allocated and passed around on the stack, not the heap. If everything really was an object, the performance would be appalling!
You can treat a value type as an object, but that involves boxing/unboxing the value to the heap.
|
|
|
|
|
Ah! I didn't realize that. Thank you for the enlightenment!
Marc
|
|
|
|
|
OK, I've got a bool data type, and I want to convert this to a 0 or 1 so I can create an SQL statement, having defined a field in Access as Yes/No.
How do I do that? Do I really have to code:
bool b;
int i=b==false ? 0 : 1;
I can't believe there isn't a conversion to a 0/1!!!
Or even worse, do I subclass the System.Boolean and create my conversion in a class? Argh. Does anyone else share my frustration?
Thanks in advance!
Marc
|
|
|
|
|
You have a couple of options without having to go that far. First you can use the System.Convert class to Convert a bool to any number type like:
bool b = true;
Convert.ToByte(b); //Returns 1
Also if you are using ADO.NET you can use paramaters instead of String building your SQL statement. It will handle the boolean to SQL bit field for you.
|
|
|
|
|
Ah. I had tried that with ToInt32 and it didn't work, so I went on this bizarre quest. However, trying your example, using ToInt32, now it does work. I must have done something wrong originally!
Thank you for your response, and I'll look at parameters too!
Marc
|
|
|
|
|
Is there any way to clear the Console screen with managed code ?
Mauricio Ritter - Brazil
Sonorking now: 100.13560 Trank
The alcohol is one of the greatest enemys of man, but a man who flee from his enemys is a coward.
|
|
|
|
|
If you still haven't found a solution, you could do a *work around* by creating a function with a for loop to clear it.
void ClearScreen()
{
for(i = 0; i < 24; i++)
{
Console.WriteLine("\n");
}
}
HTH
Nick Parker
|
|
|
|
|
In my code I have created a new process (with CreateProcess)
and I need the handle (HWND) of the process's window.
but I couldn't figure how to find the new process's related window.
For example, I create a new "NotePad" process.
later I need to send a message to the "NotePad"
how???
if any one can help me
thanks!
Eynat
|
|
|
|
|
I think you can use the DllImport to use the window's api in dll to finish this job.
I'm amumu, and you?
|
|
|
|
|
Thank you for your respond.
Can U please write me a small example - couse I didn't understand what you ment.
Eynat.
|
|
|
|
|
He means you can use API.Here an example of importing kernel32.dll:
http://www.codeproject.com/cs/miscctrl/drivecombobox.asp
Mazy
"If I go crazy then will you still
Call me Superman
If I’m alive and well, will you be
There holding my hand
I’ll keep you by my side with
My superhuman might
Kryptonite"Kryptonite-3 Doors Down
|
|
|
|
|
You mentioned CreateProcess, are you using the WinAPI CreateProcess? Or are you referring to a process created with System.Diagnostics.Process.Start()? Either way you can use this class to bind to a process if you have its PID or even just the process name (executable). Once bound use the Handle property to get the HWND.
|
|
|
|
|
here some code i used for the winamp api
<br />
[DllImport("user32.dll")]<br />
public static extern int FindWindow(string c,int d);<br />
[DllImport("user32.dll")]<br />
public static extern int SendMessage(int hWnd,int msg, int wparam, int lparam);<br />
hope it helps
|
|
|
|
|
Hi, dunno if this can be done or how...
text from MIRC help file:
This section contains technical information for programmers who want to create DLLs for use with mIRC.
The routine in the DLL being called must be of the form:
int __stdcall procname(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause)
The question is how would implement such a function in C# the can be accessed by MIRC. MIRC also allows for calling COM objects. Would this be an easier solution perhaps
Any help would be really grateful
|
|
|
|
|
Can you code like the following?
[DllImport("kernel32", SetLastError=true)]
static extern unsafe int CreateFile(
string filename,
uint desiredAccess,
uint shareMode,
uint attributes,
uint creationDisposition,
uint flagsAndAttributes,
uint templateFile);
I'm amumu, and you?
|
|
|
|
|
hello?
from what i can see you are using an external function in C# here. I want an external app to use a C# dll.
|
|
|
|
|
|
Thx for all the quick replies
Just one reason to come back for more
After some investigation it seems the COM way is the way to go.
|
|
|
|