|
hai there,
how can i call a console window and console result(out put) in windows application.
hai, feel free to contact
Sreejith SS Nair
|
|
|
|
|
In VS if u make the target app a console app and in your main form make ti load just like a windows app it will load both the console window and the windows form. Best way to do it is to. Make a windows form application then goto project proporties and change ti from windows app to console. Then you can use Console.writeline(); and watch the code. Might be other ways too though.
|
|
|
|
|
for a static mothed in java i can synchronize this way :
public class A {
public synchronized static void f () {
//do something
}
}
but how can i do this in C# ?I want to use the keyword "lock",but for a static method you can't use "this" .
Can the below method work?
public class A {
public static void f () {
lock ( typeof (A) )
{
//do something
}
}
}
|
|
|
|
|
Yep, that will work. I usually create an extra object to lock on, though, whether I'm working in a static or instance context; that goes for Java, too. Why? Because I think that an object should control its own destiny; its code shouldn't block indefinitely because some jerkoff decided to write code that locked the object or class externally. Unless that's desired behavior, of course. Know what I mean, though? Check out the SyncRoot property, where the .NET designers apparently decided the same thing. The same sort of thinking is exposed in Java APIs too.
Obviously, if you're creating a zillion objects that may be locked on, you don't want to create an extra object for each one just to accomplish this. However, I try to avoid creating zillions of objects in my code anyway; this means that most of my objects, especially the ones that I need to synchronize on, are pretty large and/or long-lived, hence the overhead of creating a tiny empty object is negligible. Add to that judicious use of the Immutable Object pattern, and I'm able to synchronize "cleanly" with no appreciable overhead.
[EDIT]One more thing-- even if you're going to use the type itself to lock on in a static context, you should probably store it as a static variable instead of getting it over... and over... and over again. [/EDIT]
Regards,
Jeff Varszegi
|
|
|
|
|
Actually, using typeof(MyType) to lock against - while not a good idea (see Mike's reply, which I read about on MSDN some time back) - is fine. The typeof operator simply gets a handle to the type, which requires only two instructions as opposed to the one or two instructions (depending on the current state of the stack) to load the field.
Still, though - like you said - it's better to have a separate object to lock against at least for static methods. Locking against this for instance methods is still okay, but - as you said (again) - a separate object is often more desirable.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yep. It's often more desirable for instances, not less, because it's more likely when you're working on a large project that some slob will decide to lock on the objects of your class.
Java (and J#, I guess; dunno) is pretty strange in that its synchronization keyword, synchronized , can be used directly in the method signature, for both static and instance methods. I'll never forget the day that I discovered that the static one locked the type itself; it seemed pretty dumb, and almost all of the documentation released from Sun that treats of synchronization just uses the keyword in the method declaration. I guess their rationale is that you don't have to lock on something like the type if you don't want to, but it still seems pretty dumb.
So you're saying that typeof is fast? I haven't checked out the IL for any of this or actually tested it. The article Mike pointed out says that typeof is slow.
Regards,
Jeff Varszegi
|
|
|
|
|
The main reason the article discusses is that you shouldn't use it because it could create a race condition and dead-locking. As far as slow, I'm not sure about that. I supposed Dr. GUI would know better than I (or rather, his "nurses" as he calls them from time to time) but in my experience it seems okay. The instruction cound - while I know isn't everything - is really no different (for instance, if you locked against a property it could be from 2 to 5 instructions as opposed to 1 to 2 for a field). And getting a handle to the type I can't believe would be that slow. I could be wrong, though.
Anyway, the biggest reason not to use it because of the potential deadlocks that could occur.
To the original poster - if you're following along - you can see we all agree: don't use it!
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
Another good reason to lock on internal objects is that it frees you to lock on different objects for different purposes. This is a technique I use a lot to increase performance. If two different things can be changed simultaneously in a multithreaded situation, without affecting the integrity of your object, but both of them need synchronization, you should consider locking on separate objects.
Regards,
Jeff Varszegi
|
|
|
|
|
Hi
I am looking for a way to evaluate expression. An expression return exacly one result. The result may be a boolean, a number value a string, etc.
The idea is I give my user a tool to define expression. The expression is built in MSSQL style, and can use SQL functions. For example:
$Num1 > 100 AND $Num2 < 0
At run time my program replaces the variables with values, for example:
101 > 100 and 1<0
In the past, my application ran on the server. I used SQL itself for that: I passed it the string, received the return value and it was great.
No I am writing a client-server application, and this code runs on the client. So I cannot go to the server all the time.
Is there a way/class/object I can pass there expressions to on the client? I was thinking of using a dataset, but I can only see it can select records from a given data table. I cannot find how to perform such calculations.
Thank you!
|
|
|
|
|
I think your options are several. You can write your own parser and evaluator from scratch (I don't know your background, but you can look up recursive-descent parsing, the "Little Language" pattern, etc. for easy starters); use a code-emitting approach like the one in this Code Project article; or use a tool like YACC or JavaCC. I haven't taken the time to do a search, but I'm sure there's something similar to the last one out there for .NET, and if not, it would be a fun port to do.
I've also used database engines for this kind of stuff in the past, but I think if you can do it directly, it would be a much better approach. I was kind of iffy on the idea before I wrote my first small parser a long time ago, but it's really not a very big deal. Of course, I don't know how in-depth your evaluation requirements are, but I imagine they can't be all that intense if you were using something like SQL Server in the past.
.NET also has VBScript and JScript support; both of these scripting languages have built-in expression evaluation (check out the "eval" function in JScript). If you can call that stuff, you're probably golden.
Regards,
Jeff Varszegi
|
|
|
|
|
Thanks Jeff.
I found something usefull in the MSDN:
DataTable.Compute() and DataColumn.Expression()
DataSet myData = new DataSet();
//
myData.Tables.Add("Orders");
myData.Tables["Orders"].Columns.Add("ParentID", typeof(int));
myData.Tables["Orders"].Columns.Add("Exp", typeof(string));
myData.Tables["Orders"].Columns.Add("Exp2", typeof(string));
myData.Tables["Orders"].Columns["Exp"].Expression = "substring('abcdefg',2,3)";
myData.Tables["Orders"].Columns["Exp2"].Expression = "IIF(1=2,'A',iif(3>4,'B','C'))";
myData.Tables["Orders"].Rows.Add(new object[1] {123});
DataTable myTable;
myTable = myData.Tables["Orders"];
// Declare an object variable.
object objSum;
objSum = myTable.Compute("substring('123456',3,2)", null);
MessageBox.Show(objSum.ToString() + "\n"
+ myTable.Rows[0][1].ToString() + "\n"
+ myTable.Rows[0][2].ToString()
);
|
|
|
|
|
Hey, that's pretty cool. You should write an article on it! Wish I could've gotten that JScript call working-- I tried for about half an hour and gave up in disgust. You'd think they would've put a simple expression evaluator in the base class library, eh?
I wrapped it up in a little class for you-- I may use this code myself on a project:
<br />
public sealed class Evaluator {<br />
private static System.Data.DataTable table = new System.Data.DataTable("dummy");<br />
<br />
private Evaluator() {}<br />
<br />
public static object Evaluate(string _expression) {<br />
if (_expression == null) {<br />
throw new ArgumentNullException();<br />
}<br />
return table.Compute(_expression, null);<br />
}<br />
<br />
}<br />
Note that this simple implementation will simply throw any exception encountered when calling Compute on the DataTable , but I figured that that was desirable behavior.
Regards,
Jeff Varszegi
|
|
|
|
|
|
|
Another way is through DataSet s. These are disconnected recordsets you could get from the SQL Server/MSDE (or whatever data provider, so long as it has either an OLE DB provider or classes designed specifically for it using ADO.NET) using a DataAdapter or DataReader (the latter would force you to fill the DataSet yourself).
Using a DataView on a DataTable lets your sort and filter using SQL-like expressions (some simple and aggregate functions are supported, too). See the documentation for the DataView in the .NET Framework SDK for more information.
Also, if you plan on displaying these in a DataGrid , you can also use expressions in the columns.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
If I were to have two classes that refer to each other, how do I create them both? Here is an example of what I'm talking about:
test.cs
<br />
namespace yadda<br />
{<br />
public class A<br />
{<br />
public class A()<br />
{<br />
...<br />
}<br />
public B MethodForA()<br />
{<br />
...<br />
return B();<br />
}<br />
}<br />
<br />
public class B<br />
{<br />
private A variable1;<br />
<br />
public class B()<br />
{<br />
...<br />
}<br />
}<br />
}<br />
Notice how classes A and B refer to each other. If I were to do this, there would be a compile error in class A saying more or less that it doesn't know what "B" is. Should I split these classes out into separate class files (.cs)? I don't want to have to make them into separate projects or anything like that.
In C++, I remember being able to create class prototypes, where I could say:
<br />
class A;<br />
class B;<br />
<br />
class A<br />
{<br />
public:<br />
A()<br />
{<br />
...<br />
}<br />
<br />
B MethodForA()<br />
{<br />
...<br />
}<br />
};<br />
<br />
class B<br />
{<br />
private:<br />
A variable1;<br />
public:<br />
B()<br />
{<br />
...<br />
}<br />
};<br />
Any ideas? As you might have guessed, I'm still somewhat new to C#. Thanks in advance!
|
|
|
|
|
My advice would be to try something out for yourself before you ask a question like this-- not because you annoy people, but because you'll learn more that way. I only ask people for help if it's something I can't get myself.
In this case, it's obvious that you didn't try to compile this code, or you would've found out things like the fact that
return B();
should be
return new B();
and that a constructor method doesn't have the word class in its signature. You would've also found out that when you fix all these problems, your code compiles fine! The .NET compiler is pretty smart; it seems to've been built for dummies in some respects. Not that I'm saying yours is a stupid question-- it definitely isn't. I remember having to change my design once on a Java project due to circular references.
Regards,
Jeff Varszegi
|
|
|
|
|
I have a clock bitmap,and I want to draw the hour hand on the bitmap?
how can i do this?
|
|
|
|
|
Create a Graphics object like this:
Graphics g = Graphics.FromImage ( YourBimap );
and then you can draw text, lines, rectangles, ...
If you do not know anything about this topic look into MSDN for GDI+
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
I have another question.
if I want to show a picture in my aspx file and I want to add some something like line or rectangle beyone the jpeg picture(not the bitmap) before show the jpeg picture,how can i do this?
thank you
|
|
|
|
|
Same way. The Bitmap class represents all images - even indexed images like GIF. See the Image and Bitmap (a derivative of Image ) documentation in the .NET Framework SDK.
For ASP.NET, however, you can't generate an image inline with the page. You must either have a page that sets it's Response.ContentType to something like image/jpeg and output only an image.
This concept is covered in many articles here on CP and MSDN[^], like this article here: An ASP.NET Thumbnail Solution[^]. Many others exist, too - just search this site or google.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
first Thanks a lot to your answer.
But I got error.
the following code:
Bitmap myBitmap = new Bitmap(@"C:\My Documents\My Pictures\GaugeTime.gif");
Graphics g = Graphics.FromImage(myBitmap);
//error is:A Graphics object cannot be created from an image that has an indexed pixel format.
It seems the gif picture is not supported? what should I do?
thank you
|
|
|
|
|
You'll need to save myBitmap to a stream as a different format (the easiest way, though other ways exist - see the .NET Framework SDK for more information about the Bitmap class). You can do this by creating a MemoryStream , saving the Bitmap as a different format, and then get a Graphics object from that:
using (Bitmap myBitmap = new Bitmap(@"C:\...\GaugeTime.gif"))
{
using (MemoryStream ms = new MemoryStream())
{
myBitmap.Save(ms, ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
using (Bitmap newBitmap = new Bitmap(ms))
{
using (Graphics g = Graphics.FromImage(newBitmap))
{
}
}
}
} The various using statements just dispose the object when it falls out of scope - even if an Exception is thrown for some reason. Each using statement compiles to the following:
using (Bitmap bmp = new Bitmap(32, 32))
{
}
Bitmap bmp = new Bitmap(32, 32);
try
{
}
finally
{
if (bmp != null) bmp.Dispose();
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you very much.
I think I understand.
|
|
|
|
|