|
I'm a little confused about this conversion in C#.
Convert.ToByte('Š') throws an exception as (according to C#) the ordinal value of 'Š' is 352.
I've looked up the 'Š' character in the ASCII chart and the ordinal value is 138.
In Delphi the conversion works fine both ways and it gives me 138 as the ordinal value (just like the ASCII chart).
What am I doing wrong?
|
|
|
|
|
lackonagy wrote: I've looked up the 'Š' character in the ASCII chart and the ordinal value is 138.
On which ASCII chart did you look this up? The real ASCII[^]does not have anything greater than 127 as it is only a 7-bit character set. There are numerous variants of so-called Extended ASCII[^] to represent 8-bit character sets or even more but there is no universal standard and not all of these extended character sets even include the original 128 ASCII characters so it becomes a very slippery slope, as you have just found out.
I'm not sure why Delphi would have returned the ordinal value of 138 for the character 'Š' but I can only assume that it must be because your computer has specific regional settings which uses an extended ASCII character set in which 'Š' translates to 138. To the best of my knowledge the two most popular "extended ASCII" sets are CP437 and CP850, both in which the value 138 translates to 'è' (latin lower case E with grave accent).
In C# the ordinal value of a character is not the ASCII value as you are used to but the character's unicode value. For some more info on converting between character sets you might want to have a look at the Encoding Class[^].
|
|
|
|
|
The unicode value of 'Š' is 352. In C#, we use char to represent the unicode char. So the code as below:
Char s1 = Convert.ToChar("Š");
Hope this will help.
Tan Li
I Love KongFu~
|
|
|
|
|
i want to restrict my application to run only one instance at a time on one machine, is there any option or property which helps, i want to do this when creating a setup for my application.
|
|
|
|
|
|
thanx , i've used mutex before but the problem is how to apply mutex on this application.what should be the critical area?
|
|
|
|
|
|
How can I round a float to an integer?
If I do int i=(int)3.0f it's not good because if 3.0f is like 2.9999999 in memory, I'll get i=2, which is unacceptable. Math.Round only accepts doubles/decimals, which is not an option because I need a float.
Any idea?
|
|
|
|
|
Tony_P wrote: If I do int i=(int)3.0f it's not good because if 3.0f is like 2.9999999
Have you tried it?
Tony_P wrote: Math.Round only accepts doubles/decimals, which is not an option because I need a float
So what? Casting a single to a double is lossless.
I think you're concerned about non-existent problems.
|
|
|
|
|
I wrote a little application to try Remote Events
I have two versions of this application
The first one: both, server and the client are Console application
The first one is working fine
I did not write any methods in Client, just do the Console.Writeline()
The second : server is a console application and the client is winform application. I have problem with second:
The event riesed but this event need to update RichTextBox in my Main form
I get this exception:
Cross-thread operation not valid: Control 'rtb_in' accessed from a thread other than the thread it was created on.
I tryed many approuches nothing helped.
Please help me with this issue.
Here is my code sample
Server:
using REWFComm;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using System.Reflection;
namespace Server
{
class ServerRemote : MarshalByRefObject, ICommon
{
public void Message(string passed, object Client)
{
Console.WriteLine(passed);
MessageEvent(passed + ": your message is arrived",Client);
}
public event MessageHandler MessageEvent;
}
class Server
{
static void Main(string[] args)
{
Console.WriteLine("Server started\nListening...");
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 8888;
props["typeFilterLevel"] = TypeFilterLevel.Full;
TcpChannel chan = new TcpChannel(props, null, serverProvider);
ChannelServices.RegisterChannel(chan, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerRemote),
"ServerRemote",
WellKnownObjectMode.Singleton);
Console.ReadLine();
}
}
}
Common object:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
namespace REWFComm
{
public delegate void MessageHandler(string myStr, object Sender);
public interface ICommon
{
void Message(string passed, object Sender);
event MessageHandler MessageEvent;
}
public class Messages : MarshalByRefObject
{
public void MessageArrived(string myStr, object Sender)
{
((RichTextBox)Sender).AppendText(myStr); //-- HERE I GET THE EXCEPTION!!!!!
}
}
}
Client:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using REWFComm;
namespace Client
{
public partial class Main : Form
{
object formobj;
public Main()
{
InitializeComponent();
formobj = this.rtb_in;
}
private void btn_send_Click(object sender, EventArgs e)
{
ICommon obj = (ICommon)Activator.GetObject(typeof(ICommon), "tcp://localhost:8888/ServerRemote");
Messages ms = new Messages();
obj.MessageEvent+=new MessageHandler(ms.MessageArrived);
obj.Message(tb_out.Text,formobj);
}
}
}
modified on Saturday, January 3, 2009 2:13 PM
|
|
|
|
|
Hi,
the methods that get called by the other side are bound to be asynchronous with respect to your main
thread, hence they execute on a different thread; the same is true for most timers, for serial port
receive, etc.
And you are not allowed to touch any Controls (accessing properties, calling methods) from any thread
but the one that created the Control, which normally is the main or GUI thread.
To solve the problem the Control class offers InvokeRequired property and Invoke method.
If you are unfamiliar with them, look them up, there are plenty of examples around.
|
|
|
|
|
Thanks to your replay
I tryed this approuch with Invoke and InvokeRequired
But it doesn't help
Maybe I did not undestand it well
If You can modefy my code you will help me a lot
Thanks
|
|
|
|
|
You might show us the code with InvokeRequired/Invoke then...
|
|
|
|
|
Here is my client with InvokeReguired/Invoke methods
That I need to write in my Common object
If You need all files I can send you
Thanks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using REWFComm;
namespace Client
{
public partial class Main : Form
{
delegate void RTBUpdateDelegate(string strMess);
object formobj;
public Main()
{
InitializeComponent();
formobj = this.rtb_in;
}
private void btn_send_Click(object sender, EventArgs e)
{
ICommon obj = (ICommon)Activator.GetObject(typeof(ICommon), "tcp://localhost:8888/ServerRemote");
Messages ms = new Messages();
obj.MessageEvent+=new MessageHandler(ms.MessageArrived);
obj.Message(tb_out.Text,formobj);
}
public void UpdateRTB(string strMess)
{
if (rtb_in.InvokeRequired)
rtb_in.Invoke(new RTBUpdateDelegate(NowUpdateRTB), new object[] { strMess });
else
NowUpdateRTB(strMess);
}
private void NowUpdateRTB(string strMess)
{
rtb_in.AppendText(strMess + "\n");
}
}
}
|
|
|
|
|
Hi,
the client code you now showed looks fine.
Except I see you are trying to pass rtb_in from client to server; I don't think that is possible.
If the server needs to modify rtb_in at the client, it should not try and do that itself, instead
it should tell the client to modify it.
|
|
|
|
|
This is a problem
How can I tell the client to modify?
Here is Common object code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
namespace REWFComm
{
public delegate void MessageHandler(string myStr, object sender);
public interface ICommon
{
void Message(string passed, object sender);
event MessageHandler MessageEvent;
}
public class Messages : MarshalByRefObject
{
public void MessageArrived(string myStr, object sender)
{
From HERE I need to tell to client to modefy the control
}
}
}
|
|
|
|
|
Hi,
what you need is a remoting callback. I am not familiar with it, but there are several articles
here on CP that deal with it, this is one of them.[^]. It all starts with a delegate, as usual...
|
|
|
|
|
Thanks for link. but still I have blackout (((((
Any suggestions?
Thanks a lot
Ilan
|
|
|
|
|
Ok here's what I'm trying to do:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Runtime.Remoting;
using System.Drawing;
using System.Windows.Forms;
namespace GetConstructorTest
{
public class GooConsistency
{
public GooConsistency()
{
}
}
public class Monster
{
public Monster()
{
}
}
public class GooeyMonster: Monster
{
public GooeyMonster(GooConsistency gooness)
{
}
}
public class SqueltcheyMonster : Monster
{
public SqueltcheyMonster(Color tongueColor, bool isBackflipping)
{
}
}
public partial class Form1 : Form
{
public Form1()
{
object[] MonsterData1 = new object[] { new GooConsistency() };
Monster Monster1 = CreateMonster("GooeyMonster", MonsterData1);
}
public Monster CreateMonster(string type, Object[] vConstructorArgumentValues)
{
Type[] argtypes = Type.GetTypeArray(vConstructorArgumentValues);
Type t = Type.GetType(type);
ConstructorInfo info = t.GetConstructor(argtypes);
return info.Invoke(vConstructorArgumentValues) as Monster;
}
}
}
But on the line: ConstructorInfo info = t.GetConstructor(argtypes);
I get this exception:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="GetConstructorTest"
StackTrace:
at GetConstructorTest.Form1.CreateMonster(String type, Object[] vConstructorArgumentValues) in G:\Portable Documents\Visual Studio 2008\Projects\GetConstructorTest\GetConstructorTest\Form1.cs:line 50
at GetConstructorTest.Form1..ctor() in G:\Portable Documents\Visual Studio 2008\Projects\GetConstructorTest\GetConstructorTest\Form1.cs:line 44
at GetConstructorTest.Program.Main() in G:\Portable Documents\Visual Studio 2008\Projects\GetConstructorTest\GetConstructorTest\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Why?
|
|
|
|
|
Probably Type.GetType(type) returned null , that's why t is null as well.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hi,
as the exception says, you have a null reference; there is only one reference on that line, so t is null.
reason seems to be you did not specify the full type name in the line above.
MSDN on Type.GetType() says "typeName can be the type name qualified by its namespace or an assembly-qualified name that includes an assembly name specification."
|
|
|
|
|
Hi,
Can you please give me a practical example to distingish below two methods..
Method 1
string.Format("something","Something");
Method 2
string.Format(CultureInfo.CurrentCulture,"something","Something");
Thanks!
Charith Jayasundara
|
|
|
|
|
Your first example simply calls your second example but with null as the first parameter. The second example exlicitly uses the current culture to render the formatting. In other words, if you ask it to format a number it will place the delimiters and decimal markers in the correct place and with the correct characters. For example, if your culuture is set to EN-GB then 1.234 will be rendered, whereas if your culture is NL-NL the same number would be rendered as 1,234
The .NET documentation has extesive examples of how to use Format and on formatting in general, that should be your starting point.
|
|
|
|
|
Hi,
Thank you for the reply. I understand how it works for numbers. But what is the purpose of setting the culture for normal strings?
Charith Jayasundara
|
|
|
|
|
Charith Jayasundara wrote: But what is the purpose of setting the culture for normal strings?
Because some people like consistency and will set all parameters explicitly all the time. Other than that, I don't think it makes any difference for strings.
|
|
|
|