|
VARGHESE--# RINSON # wrote: why v going for c#?
I don't know what this means to you, but it means nothing to us. If you have a programming problem please try to describe it clearly.
|
|
|
|
|
hmmm... good question?
...Maybe C# stole something from V and that's why he is going after him...
...I think this is not an appropriate question for this forum. I don't even know these two people let alone their motivations towards each other
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
what is the difference between ref and out variables(in situation basis)....i know that ref should be initialized before its usage ...why its like that(what is the need of initializing ref)?
|
|
|
|
|
VARGHESERINSON wrote: why its like that(what is the need of initializing ref)?
ref = in + out
in ==> must have value
the C# documentation explains all.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
Well an OUT wont compile if you don't set it in the function, a REF does not need settings. Thou, why would you use a REF if you didn't plan on using it anyway. Maybe one path uses it, other path does not.
Maybe a better example would be if you were to re-use the value in other functions, for example consider the following sample...
ref int SomeData;
public void Setup(ref int data)
{
SomeData = data;
}
public void Add5()
{
SomeData += 5;
}
...pointless? Yes. Doe's it compile? I don't know. But would not work with the OUT parameter.
[EDIT] It does not compile
Next attempt...
If you where to design a class library that was to be used for somebody else then it could be useful.
You could use REF to ensure that the value was set before passing in, and you could use out if you didn't care what the value was to start with. Examples...
public void (out int i)
{
i = 10;
}
public void (ref int i)
{
i = i + 5;
}
...Maybe this is what Luc was getting at
[EDIT] Just me again...
I worked out a better usage. Consider the following struct instead of a basic datatype.
struct Sample{
public string A;
public string B;
public Sample(string a, string b)
{
A = a;
B = b;
}
}
If you use this struct with REF, then you can change B and keep A the same, if you use OUT then you are required to initialize the data and thus lose all data.
This is the same for passing Controls (for whatever reason you would want to)
Life goes very fast. Tomorrow, today is already yesterday.
modified on Wednesday, October 21, 2009 9:12 AM
|
|
|
|
|
musefan wrote: Maybe this is what Luc was getting at
What Luc was getting at most of all is: read the fine manual, don't be so damned lazy.
And read a book about the programming language you want to use; this is after all basic knowledge.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
"out" parameters are like return values, you must assign them before returning.
"ref" causes arguments to be passed by reference: any changes made to the parameter in the method will be reflected in that variable.
|
|
|
|
|
"out" is used when you need to return more values to the caller (for example a return code and other data), "ref" is used when the method can change the argument.
|
|
|
|
|
hmmm... out will also produce the same result, i.e. changing the argument
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
"out" arguments are one-way, "ref" are bidirectional.
|
|
|
|
|
I did think of bringing that up, but when would you be able to alter the value from outside the function while the function is running? You would need multi-thread, in which case there would be other issues.
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Int32 someInt = 6;
SomeMethod(ref someInt);
MessageBox.Show("SomeInt now contains value" + someInt.ToString());
public void SomeMethod(ref Int32 data)
{
data = data * 2;
}
musefan wrote: I did think of bringing that up, but when would you be able to alter the value from outside the function while the function is running? You would need multi-thread, in which case there would be other issues.
that is the same think as using static, I really don't remember if it compiles if you access directly from different thread, but it is a programmer job to make it Thread Safe. Also as I think accessing a method in different thread you need to use Invoke() method, at least it is one of three possible ways
|
|
|
|
|
as in "ref = in + out" ?
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
Am I alone in thinking that SerialPort is very slow in returning all the incoming data? I'm only receiving 10 bytes and have to put in a 400ms wait to ensure that all of the data has been received. With a port at 9600baud I'd expect it to become available much quicker. It is a USB COM port but even so that's a lifetime in CPU cycles.
Any suggestions on how to speed up the reception of data please?
I hope you realise that hamsters are very creative when it comes to revenge. - Elaine
|
|
|
|
|
USB-to-serial cables turn the data into USB packets, they don't send a packet for each byte, that would hamper throughput too much. So they have a timeout before they decide to send what they have got so far. However I don't think it is anything near 400msec (which would correspond to some 400 bytes), it would rather be some 20 msec or so.
Did you measure those 400 msec? How? and where is it, i.e. what are the two points in time that span those 400 msec?
Are you using the DataReceived event? or using a read thread?
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
I'm using the DataReceived event, which is processing the data once it's received a full packet. The main thread sends a packet and waits 400ms for a returned packet, which should be received with just a few ms turnaround. After waiting the timeout period it checks to see if any data has been received and processed. If the timeout is less than about 200ms then the processing doesn't start until after the timeout, 200-400 it's pot luck which happens first.
I hope you realise that hamsters are very creative when it comes to revenge. - Elaine
|
|
|
|
|
Hi,
1.
if the 400msec is from sending a command to getting a result, that would include:
- the output time; I would expect this simply is around N msec for N characters; it could be more on a USB-to-serial cable, as again data would be packetized probably based on a timeout;
- the reaction time; I don't know what your peripheral is, it needs to collect the command characters, interpret them, come up with an answer and send it; a tiny microcontroller (or poor software) might spend quite some time here; and if it is a meter of some kind, maybe it first has to perform its measurement, then report.
- only now can the PC start processing input. As I said earlier, that too would get packetized by the cable.
2.
if the main thread sends and waits, maybe it would be better to have it send and receive, without DataReceived event.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
Hello,
I work in console application, whene the timer was excuted, my console don't wait the end of timer, the console close direcly, how i can wait the end of timer ? thank you verry mutch.
|
|
|
|
|
Hi,
The problem will lie in the code you have written. I'll leave you to figure out what you need to do next.
Alan.
|
|
|
|
|
Hello
i do this :
public static System.Timers.Timer timer1;
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.write("test");
}
static void Main(string[] args)
{
timer1 = new System.Timers.Timer();
timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer1.Interval = 10000;
timer1.Start();
}
Unfortunatly, the console close witout excuting the timer, please help me, thak you verry mutch.
|
|
|
|
|
static void Main(string[] args)
{
timer1 = new System.Timers.Timer();
timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer1.Interval = 10000;
timer1.Start();
Console.ReadLine()
}
|
|
|
|
|
If i put "Console.ReadLine()", the console don't close, i would close the console when the timer was stoped. thank you verry mutch.
|
|
|
|
|
Use a ManualResetEvent.
public static System.Timers.Timer timer1;
private static System.Threading.ManualResetEvent mrEvent = new System.Threading.ManualResetEvent(false);
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.write("test");
mrEvent.Set();
}
static void Main(string[] args)
{
timer1 = new System.Timers.Timer();
timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer1.Interval = 10000;
timer1.Start();
mrEvent.WaitOne();
}
|
|
|
|
|
Hi,
As written the main method will return and the application will terminate after the call to timer1.Start() . What is required is some form of wait and a simple and common way of doing this in console apps is to insert a call to Console.ReadLine()
timer1.Start();
Console.Readline();
Now the application will not terminate until the user presses the enter key.
Alan.
|
|
|
|
|
yes, we must to press enter key to exit environment, i would close the consol in end of timer, whene i excute timer.stop(); thank you.
|
|
|
|