|
Thanks a lot.
I was hoping there could be a class
|
|
|
|
|
Like all primitives, Boolean has a Parse method for that very reason. You can also use Convert.ToBoolean (which uses Parse , but provides a little more abstract access to passing any primitive type (and a few other value types) to ToBoolean (though, in that case, few actually can be parsed into a bool ).
[EDIT] Oops, I didn't read that quite right. Nevermind; though this is still handy information if you didn't know it already. [/EDIT]
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
There are a couple ways you could do this, including one way in the BCL (though not easy).
The first is to reference the Microsoft.JScript.dll assembly and use the Eval class. In JScript .NET, this would be equivalent of the eval method (also in Javascript and JScript, though those aren't compiled languages nor do they target the CLR). I believe I saw an article on this some time back on MSDN. I could be wrong, but you could try searching for it or just give it a try.
The other way is to use Reflection Emit or the CodeDOM. Basically, you create a class that contains those variables and contains a method which evaluates those variables and returns a bool .
Read Emitting Dynamic Assemblies[^] in the .NET Framework SDK for more information about Reflection Emit and Generating and Compiling Source Code Dynamically in Multiple Languages[^] for more information on CodeDOM.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
The other way is to use Reflection Emit or the CodeDOM. Basically, you create a class that contains those variables and contains a method which evaluates those variables and returns a bool.
All that for "?1+1=2". Seems kind of like having a nuclear power plant to power your USB notebook fan. I'd rather smash my head into a board of nails than go through that!
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
narada108,
There is no class in .net to do what you would like. However, depending on the format of your Boolean expression this may not be a difficult task. This task can be broken down into two easier problems. Convert the expression to a postfix expression, and then evaluate the postfix expression.
Typically a Boolean expression looks like this "1 and 1". This is called infix notation since the operator is between the operands. The postfix equivalent to this expression would be "1 1 and". In postfix notation the operator comes after the operands. A more complicated example follows:
infix: "1 and 1 and not 0 or 0"
postfix: "1 1 0 not and and 0 or"
It looks difficult but you get used to it really fast. The main advantage of writing the expressions this way is that they are easy to evaluate. How to evaluate a postfix expression is covered in many introductory computer science classes. The following code will evaluate an expression in this format.
<br />
private bool EvaluatePostfixExpression(string expression)<br />
{<br />
string[] pieces = expression.Split(new char[]{' '},100);<br />
<br />
System.Collections.Stack stack = new Stack();<br />
<br />
foreach(string cur in pieces)<br />
{<br />
if (cur == "0")<br />
stack.Push(false);<br />
else if (cur == "1")<br />
stack.Push(true);<br />
else if (cur == "and")<br />
{<br />
bool second = (bool)stack.Pop();<br />
bool first = (bool)stack.Pop();<br />
stack.Push(first && second);<br />
}<br />
else if (cur == "or")<br />
{<br />
bool second = (bool)stack.Pop();<br />
bool first = (bool)stack.Pop();<br />
stack.Push(first || second);<br />
}<br />
else if (cur == "not")<br />
{<br />
stack.Push(!(bool)stack.Pop());<br />
}<br />
<br />
}<br />
<br />
return (bool)stack.Pop();<br />
<br />
}<br />
For example EvaluatePostfixExpression("1 1 0 not and and 0 or") will return true;
I would recommend trying to get your Boolean expressions in postfix notation. If you can't then another simple stack based algorithm can be used to convert infix notation to postfix notation. I don't feel like writing that algorithm at the moment, so let me know if you go this way and need help with it. You can probably find these algorithms in a book on data structures.
Karl Baum
CEO of KGB Technologies
Specializing in custom software development.
|
|
|
|
|
|
Hi Everyone, this is my first time here and wonder if anyone out there can help me. I'm using VS.net 2003, and trying to display an "Open Dialog Box" on form MyForm.aspx. On MyForm.aspx, there is one button called "Browse", what i would like is that when the user click on the "Browse" button, the open dialog box pops up and allow user to select a *.csv file from any directory user choose to. It's like upload a file up on the sql server. If anyone have an example or give me a tour through the codes would be great, b/c I have never done anything like this before. Many thanx
*HyVong*
|
|
|
|
|
There already is a component to do this. Read the documentation for the OpenFileDialog component in the .NET Framework SDK. While designing your form or user control in VS.NET, you can drag and drop this component from the "Components" section in your toolbox (where all the controls and components can be found when designing your controls).
You should really skim through the class library documentation and learn what's available. There are many controls and components to help you create rich apps but you'll only find them if you look.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you very much, though i did go through the help search but i don't have much experience in the field as a student, that's why i'm seeking for help. Thank you.
*HyVong*
|
|
|
|
|
HyVong wrote:
but i don't have much experience in the field as a student
Sorry, but that's really a poor excuse. I was studying C/C++, COM, Perl, and HTML/DHTML long before I even went to college, and learned a lot while I was in college (just not at). College (or whatever educational system you're in) won't teach you everything you need to know. The sooner you realize that, the better off in the future you'll be. Don't wait - study all you can and in-depth so that you gain a good understanding, not just knowledge of what you're told.
Things change constantly in this field and you have to keep up or get left behing. You do that through research and development and in-depth studies. That's what makes a good developer. Anyone can be taught to write code.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thank you, but if i'm that smart i wouldn't write this message today
I'm learning as i'm doing it, hopefully i'll get this project works. thanks again.
*HyVong*
|
|
|
|
|
b/c i'm using asp.net, it doesn't work the way i thought , so i had to use javascript instead, now i have to figure out how to upload the file on the server and put that file onto the dataset....any suggestion anyone?
*HyVong*
|
|
|
|
|
i want to use a query builer in my project, as well as SQL Server query analyser.
tahnks.
|
|
|
|
|
Could somebody give me a hint where I can find some documentation and advice how to handle MS Project 2003 data through programming with .NET and C#
Thanks
|
|
|
|
|
The best place to find the programming information about Office products is in the developer help that can be installed (and is installed if you choose a "Complete" install) with Office. MSDN[^] contains only the object model diagram (no clicketies) and conceptual articles (still good to read, though).
Even though the object model discusses VBA, the same is available in managed languages. VBA depends upon typelibs to provide OLE/COM automation interfaces and related types. This is the same information that VS.NET and the tlbimp.exe utility use to create a Runtime Callable Wrapper (RCW, often called an "interop assembly"). If you have Office XP, you can download the official interop assemblies (Primary Interop Assemblies, or PIAs) from http://www.microsoft.com/downloads/details.aspx?FamilyId=C41BD61E-3060-4F71-A6B4-01FEBA508E52&displaylang=en[^]. If you have Office 2003 Professional, these can be installed along with Office (Microsoft .NET Programming Support).
If you want to support older Office versions, use tlbimp.exe (and specify signing options, which is important if you want to install into the GAC) to generate interop assemblies from those products. These will typically work with newer versions of Office products as well since the typelibs are versioned and class interfaces are almost always backward compatible (at least for the basics).
So, just remember that whatever you read about the object model for VB/VBA, it will work in managed code as well using interop assemblies (though sometimes a little extra programming is required...sometimes).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
How can get all server name oline in network local?
Quoc
|
|
|
|
|
What? Can you please rephrase your question a little more clearly?
If you want to get the remote computer name in an ASP.NET web application (i.e., the requesting machine), use Request.ServerVariables["REMOTE_HOST"] .
If you want to get the name of the computer on which your code is currently running, use Environment.MachineName .
If you want to enumerate the machines on your local network, you can use the System.DirectoryServices to enumerate domain machines or P/Invoke the Network Management APIs which is discussed in several articles here on CodeProject. Try the following search: http://www.codeproject.com/info/search.asp?cats=3&cats=5&searchkw=NetServerEnum[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
How can get all server name oline in network local?
|
|
|
|
|
I want to create a Listening UDP socket in Async Mode.
here is my code:
private void button7_Click(object sender, System.EventArgs e)
{
sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
iep = new IPEndPoint(IPAddress.Any,20100);
sock.Bind(iep);
sock.BeginReceive(data,0,1024,SocketFlags.None,new AsyncCallback(ReceiveData),sock);
}
private void ReceiveData(IAsyncResult iar)
{
Socket remote = (Socket)iar.AsyncState;
int recv = remote.EndReceive(iar);
Console.WriteLine("Recv {0}:",recv);
remote.BeginReceive(data,0,1024,SocketFlags.None,new AsyncCallback(ReceiveData),sock);
}
//////////////////////////////
i want the sock to keep listening.
However, once i send a udp message with UDPClient from another pc, the ReceiveData(IAsyncResult iar) method turns into a loop keeping writeline.
what is wrong ?
|
|
|
|
|
Is it the same data (assuming that you're sending unique data in the UDP datagrams)?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i send the udp datagrams only once using this code:
UdpClient udp = new UdpClient();
byte[] data = new byte[]{0x11,0x22,0x33};
udp.Send(data,3,ip,port);
at the server, Console keep showing this:
recv :16
recv :18
recv :16
recv :16
...
it seems there ocurrs a loop in the ReceiveData(Async iar) method.
i dont know what happened.
|
|
|
|
|
Assuming that the code you posted is the exact same code (people sometimes post sample code that is nothing like their actual code and doesn't contain the problem, so I just have to say it), I see no loop. What I would check out is the other machine's code. Perhaps it's looping infinitely and your client code is working correctly (seeing each UDP packet as it arrives).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank for your help.
While i test my code again, there comes out correct result.
i am confused for my code being not changed at all.
|
|
|
|
|
Hi,
I have a DataSet that has a calculated column (note: DataSet not originally populated by a source file, not that that matters I guess). I want to save the DataSet to an MS Access db file with the DataAdapter (not a problem with no computed columns). The DataAdapter.Update does not calculate the values (too easy!). Can anyone tell me how I could convert/save/copy this calculated column to a column that is not seen as calculated? Any help on this is greatly appreciated (please show code sample as I'm new to programming).
Thanks,
Ron
|
|
|
|
|
You could write the DataSet out to a stream and read it back into a generic (i.e., not typed) DataSet without writing the schema.
For example, les say your typed DataSet is called CustomOrders :
using (MemoryStream ms = new MemoryStream())
{
customOrders1.WriteXml(ms, XmlWriteMode.IgnoreSchema);
ms.Seek(0, SeekOrigin.Begin);
DataSet ds = new DataSet("CustomerOrders");
ds.ReadXml(ms);
oleDbDataAdapter1.Update(ds);
} The column - without the schema - would seem as a normal, non-computed column.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|