|
HI Guys,
Using C#,WPF, MVVM Application. I want to save a string or File path when my application exit and again if i relaunch the application That string has to Show.
Iam using the string for otherpurpose.
Kindly suggest me the way, and any hints are more appreciatable.
Thanks,
TV Krishna Rayalu
|
|
|
|
|
You can use the registry[^], or a .ini or .config file.
Use the best guess
|
|
|
|
|
Thank you. , Storing in Registry is secured ?
|
|
|
|
|
KRISHNARAYALU wrote: Storing in Registry is secured ? I'm not sure what you mean; everything is secured if you back up your system properly.
Use the best guess
|
|
|
|
|
But it is an army application, i need to confirm i have Registry access or not.
Thank you.
krishna
|
|
|
|
|
How can anyone here answer that? You need to talk to the administrator of the systems that you are using.
Use the best guess
|
|
|
|
|
I'm a bit confuse about C# convertion.
What is the difference between three convertion below?
txtValue = (string)a;
txtValue = a.ToString();
txtValue = Convert.ToString(a);
I appreciate any help.
|
|
|
|
|
Midnight Ahri wrote: txtValue = (string)a;
The typecast operation assumes that the object is of type or subclasses from the target type.
So in the above statement a is assumes needs to have string in it's inheritance hierarchy
You should try the above statement in your C# IDE. If 'a' is of type int or float (or anything non-string), the compiler throws an error since it would be a invalid typecast.
Midnight Ahri wrote: txtValue = a.ToString();
a.ToString call the ToString method that every class inherits from the System.Object[^] class. It does not typecast, it returns a new string representation.
a.ToString would throw a null reference exception if a is null
Midnight Ahri wrote: txtValue = Convert.ToString(a);
Convert.ToString[^] returns String.Empty in case the object passed to it is null, otherwise it first checks for IConvertible and IFormattable ToString implementations in the object and uses them. Finally if those are it functions the same as the object's ToString.
"It was when I found out I could make mistakes that I knew I was on to something."
-Ornette Coleman
"Philosophy is a study that lets us be unhappy more intelligently."
-Anon.
|
|
|
|
|
parths wrote: a.ToString would throw a null reference exception if a is null
No, it doesn't! It will throw a NullReferenceException, since it has no instance to call a method from. Try it!
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
I rechecked what I had typed. Isn't that what I had said?
"It was when I found out I could make mistakes that I knew I was on to something."
-Ornette Coleman
"Philosophy is a study that lets us be unhappy more intelligently."
-Anon.
|
|
|
|
|
Nice try - it almost worked...
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
I really hadn't hadn't got your point the first time around.
Did you mean that saying 'a.ToString would throw an exception' should actually be 'Calling a.ToString would throw an exception'?
Sounds like 2 very different things now that I think about it, although I had actually meant the latter. Thanks for correcting that.
I hope I got it right this time.
"It was when I found out I could make mistakes that I knew I was on to something."
-Ornette Coleman
"Philosophy is a study that lets us be unhappy more intelligently."
-Anon.
|
|
|
|
|
Thank you very much for the answer.
It's really useful.
|
|
|
|
|
parths wrote: The typecast operation assumes that the object is of type or subclasses from the target type. Actually no, it could be that the (static) type of a has an explicit conversion to string.
|
|
|
|
|
To add to (and correct) what parths said:
parths wrote: a.ToString would throw a null reference exception if a is null
No, it doesn't! It will throw a NullReferenceException, since it has no instance to call a method from.
There is also:
txtValue = a as string; Which will try to do a cast to a string, and return null if the cast cannot be performed instead of throwing an exception. This is frequently used when dealing with a variety of objects that derive from a single base class. For example:
foreach (Control c in Controls)
{
Button b = c as Button;
if ( b != null)
{
...
}
}
There is also an implicit call to ToString when you concatenate a strign with a non-string:
int i = 999;
string s = "The value is: " + i; Produces
The value is: 999 because the system does an implicit call to ToString to convert the data.
You should also note that unless a class specifically implements ToString as an override, the default Object version will be called. This returns a string which contains the fully qualified name of the instance type, which catches a lot of people out, who assume that
Image i = Image.FromFile(@"D:\Temp\MyPic.jpg");
string s = i.ToString();
string sql = "INSERT INTO MyTable (userPic) VALUES ('" + s + "')";
... will insert the image data into their database for later. What it actually inserts is the text
System.Drawing.Bitmap which confuses them when it fails to work as a picture on retrieval.
[edit]Spurious code block removed - Thanks Richard! - OriginalGriff[/edit]
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
modified 27-Mar-13 4:57am.
|
|
|
|
|
OriginalGriff wrote: Produces
<pre lang="text"> Oops!
Use the best guess
|
|
|
|
|
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
n00b
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
just to complete the information.
casting is often used in boxing/unboxing objects and is often an expensive operation.
I didn't test, but I think that
txtValue == (string)a;
is a heavier operation than
txtValue = a.ToString();
with the additional problem that you get an exception if it cannot be cast. try/catch is also heavier then if/else. (so if(a != null) { txtValue = a.ToString() } is better than using try/catch. (this is what I call defensive programming)
If you write your own class and override the ToString method, you can do some fancy stuff. eg. if you do NOT override, ToString will probably return something like "thenamespace.theclassname". If you do override it you can make a representation of your class like eg a JSON object or an XML string (or whatever format takes your fancy) with all property values in there. (serializing objects)
txtValue = Convert.ToString(a);
will convert the object to string, but in a safer way. In fact I rarely use the casting (option 1), only if I need to cast from a boxed object. I only use option 2 when I want to do fancy stuff with my class (because of the null reference exception problem.) I use option 3 in most normal cases.
hope this helps.
|
|
|
|
|
I'm trying to do a Left Join in Linq To Sql and I can't figure it out.
Here's my code with my changes undone. MaterialSizes may or may not be there in the data:
using (var dc = getDataContext())
{
ISystemDAL systemDAL = new SystemDAL();
var summaries = (from mh in dc.MaterialHeaders
join md in dc.MaterialDetails on mh.MaterialHeaderId equals md.MaterialHeaderId
join ms in dc.MaterialSizes on md.MaterialSizeId equals ms.MaterialSizeId
select new MaterialSummaryModel
{
Id = mh.MaterialHeaderId,
MaterialDetailId = md.MaterialDetailId,
ItemNumber = mh.ItemNumber,
ItemName = mh.ItemName,
Category = systemDAL.GetLookup(mh.MaterialTypeId),
Color = md.Color,
Fitting = md.FittingId.Value == null ? null : systemDAL.GetLookup(md.FittingId.Value),
Size = ms.Caption
}).ToList();
return summaries;
}
How do I turn MaterialSizes into a left join??
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
have a read of this
Stackoverflow : C# Linq Left outer join syntax[^]
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
I've got a service than runs on two machines and communicates back and forth through a socket connection.
To make debugging easier, I've wrapped the OnStart and OnStop stuff inside a console program. The only
difference in the two running instances is a) how the exe starts (SCM versus double-clicking the console exe) and b) the account the exe runs in (LocalSystem versus current user). Actual code of the "guts" of the program is the same.
When the following runs with two consoles communicating, everything is fine. When it runs with a console app on one machine talking to a service on the other, it fails on the indicated line, throwing a System.InvalidCastException with the message unable to cast object of type 'OverarchingServer.DerivedMessage' to type 'OverarchingServer.DerivedMessage'
A breakpoint in the debugger of the running service at the line in question shows that msg IS of the expected 'DerivedMessage' type and the fields are fully and correctly populated. The exception thrown indicates the types ARE the same. So why can't I do the type cast?
I haven't tried service to service yet - that is rather difficult to set up on my development system. Besides, I want to know why this doesn't work when run this way.
Any ideas?
Thanks,
Judy
namespace ClientMessages
{
[Serializable]
abstract public class BaseMessageClass
{
public int type;
}
}
namespace OverarchingServer
{
using ClientMessages;
public enum MessageTypeEnum : int
{
DerivedMessageType = 1,
AnotherType,
YetAnotherType
}
[Serializable]
public class DerivedMessage : BaseMessageClass
{
}
public class SendingServer
{
private Socket client;
public void DoingWork ()
{
DerivedMessage msg = new DerivedMessage ();
this.Send (msg);
}
public void Send (BaseMessageClass msg)
{
MemoryStream mem = new MemoryStream ();
BinaryFormatter bf = new BinaryFormatter ();
bf.Serialize (mem, msg);
byte[] data = mem.ToArray ();
NetworkStream stream = new NetworkStream (this.client);
BinaryWriter binWrite = new BinaryWriter (stream);
binWrite.Write ((Int32) data.Length);
binWrite.Write (data, 0, data.Length);
}
}
public class ReceivingServer
{
private Socket client;
private void LoopWithDetailsOmitted ()
{
NetworkStream stream = new NetworkStream (this.client);
BinaryReader binRead = new BinaryReader (stream);
int length;
byte[] data;
length = binRead.ReadInt32 ();
data = binRead.ReadBytes (length);
MemoryStream mem = new MemoryStream (data);
BinaryFormatter bf = new BinaryFormatter ();
BaseMessageClass msg = (BaseMessageClass) bf.Deserialize (mem);
this.HandleMessage (msg);
}
private void HandleMessage (BaseMessageClass msg)
{
MessageTypeEnum type = (MessageTypeEnum) msg.type;
switch (type)
{
case MessageTypeEnum.DerivedMessageType:
this.HandleMessage ((DerivedMessage) msg);
break;
}
}
private void HandleMessage (DerivedMessage msg) { }
}
}
Be wary of strong drink. It can make you shoot at tax collectors - and miss.
Lazarus Long, "Time Enough For Love" by Robert A. Heinlein
|
|
|
|
|
Oops forgot ... Windows XP and .NET 3.5
Be wary of strong drink. It can make you shoot at tax collectors - and miss.
Lazarus Long, "Time Enough For Love" by Robert A. Heinlein
|
|
|
|
|
Move your message classes (everything used by more than one project) to another project and reference that project from both your server service project and your console project.
|
|
|
|
|
I'll try that at work tomorrow ...
I'm guessing the point you're making is ... the same namespace in the same DLL running on both machines, versus the exact same namespace compiled into two distinct executables. My question is then, how will it deal with versioning? The messages can, and mostly likely will, change with the version of the program suite. I'll then have the same namespace in two differently versioned DLLs -- two different DLLs containing differently versioned messages on the two machines. They must still exchange messages, with the versioning controlled by the support in serialization. Am I going to have the same issue then?
Judy
Be wary of strong drink. It can make you shoot at tax collectors - and miss.
Lazarus Long, "Time Enough For Love" by Robert A. Heinlein
|
|
|
|