Click here to Skip to main content
15,898,036 members
Home / Discussions / C#
   

C#

 
AnswerRe: working with VarBinary type field (SQL 2000) in C# 2.0 Pin
PIEBALDconsult1-May-07 18:58
mvePIEBALDconsult1-May-07 18:58 
GeneralRe: working with VarBinary type field (SQL 2000) in C# 2.0 Pin
Rocky#4-May-07 8:50
Rocky#4-May-07 8:50 
QuestionHelp with XML design. Pin
Lord Kixdemp1-May-07 14:27
Lord Kixdemp1-May-07 14:27 
AnswerRe: Help with XML design. Pin
PIEBALDconsult1-May-07 15:09
mvePIEBALDconsult1-May-07 15:09 
GeneralRe: Help with XML design. Pin
Lord Kixdemp2-May-07 1:42
Lord Kixdemp2-May-07 1:42 
GeneralRe: Help with XML design. Pin
mikker_1232-May-07 6:19
mikker_1232-May-07 6:19 
GeneralRe: Help with XML design. Pin
Lord Kixdemp2-May-07 14:28
Lord Kixdemp2-May-07 14:28 
Questionconvert an object to string and back (in a culture independent fashion) Pin
Super Lloyd1-May-07 14:17
Super Lloyd1-May-07 14:17 
to store user preference in our application we have an 'hand written' XML file. (as opposed to XmlSerializer written one, which crash sometimes, on some user's computer, for some unknown reason.. but I'm digressing)

In this XML file I store object value, which I convert to string (when writting) and from string (when reading) with a couple of simple function which (should) do a reversible conversion (using TypeConverter) (functions below).

Unfortunately, for some Enum, (namely System.Windows.Forms.Keys) I got a culture dependent string, even though I specify CultureInfo.InvariantCulture as an argument in my ConvertXXX method with the TypeConverter. Even using 'new CultureInfo("en")' do not fix this behavior.

Any tip on how to fix these function (below) so they provide a truly culture independent reversible object=>string=>object mechanism?
===================================================
public static bool TryGetStringValue<T>(object val, out string ret)
{
if (val is string)
{
ret = (string)val;
return true;
}
TypeConverter tc = TypeDescriptor.GetConverter(typeof(T));
if (tc == null)
{
ret = val.ToString();
return false;
}
try
{
ret = tc.ConvertToString(null, CultureInfo.InvariantCulture, val);
return true;
}
catch (ArgumentException) { }
catch (NotSupportedException) { }
ret = val.ToString();
return false;
}
public static bool TryGetTValue<T>(object val, out T tval)
{
if (val is T)
{
tval = (T)val;
return true;
}
TypeConverter tc = TypeDescriptor.GetConverter(typeof(T));
if (tc == null)
{
tval = default(T);
return false;
}
if (!tc.IsValid(val))
{
tval = default(T);
return false;
}
try
{
tval = (T)tc.ConvertFrom(null, CultureInfo.InvariantCulture, val);
return true;
}
catch (ArgumentException) { }
catch (NotSupportedException) { }
tval = default(T);
return false;
}
===================================================

QuestionChanging what BindingSource references Pin
mikker_1231-May-07 13:48
mikker_1231-May-07 13:48 
QuestionWebservice in C# - please help Pin
jhinze1-May-07 13:19
jhinze1-May-07 13:19 
AnswerRe: Webservice in C# - please help Pin
coolestCoder1-May-07 18:33
coolestCoder1-May-07 18:33 
QuestionWhat is this Control's name? Pin
freshonlineMax1-May-07 12:19
freshonlineMax1-May-07 12:19 
AnswerRe: What is this Control's name? Pin
Christian Graus1-May-07 12:58
protectorChristian Graus1-May-07 12:58 
AnswerRe: What is this Control's name? Pin
sharmit1-May-07 17:05
sharmit1-May-07 17:05 
GeneralRe: What is this Control's name? Pin
freshonlineMax1-May-07 19:35
freshonlineMax1-May-07 19:35 
GeneralRe: What is this Control's name? Pin
sharmit2-May-07 16:33
sharmit2-May-07 16:33 
QuestionGetting the bin directory location for a service??!! Pin
LongRange.Shooter1-May-07 11:54
LongRange.Shooter1-May-07 11:54 
AnswerRe: Getting the bin directory location for a service??!! Pin
PIEBALDconsult1-May-07 18:01
mvePIEBALDconsult1-May-07 18:01 
GeneralRe: Getting the bin directory location for a service??!! Pin
LongRange.Shooter2-May-07 5:30
LongRange.Shooter2-May-07 5:30 
GeneralRe: Getting the bin directory location for a service??!! [modified] Pin
PIEBALDconsult2-May-07 13:35
mvePIEBALDconsult2-May-07 13:35 
GeneralRe: Getting the bin directory location for a service??!! Pin
PIEBALDconsult2-May-07 15:02
mvePIEBALDconsult2-May-07 15:02 
GeneralRe: Getting the bin directory location for a service??!! Pin
LongRange.Shooter10-May-07 10:22
LongRange.Shooter10-May-07 10:22 
AnswerRe: Getting the bin directory location for a service??!! Pin
justintimberlake1-May-07 18:01
justintimberlake1-May-07 18:01 
AnswerRe: Getting the bin directory location for a service??!! Pin
PIEBALDconsult2-May-07 15:10
mvePIEBALDconsult2-May-07 15:10 
GeneralRe: Getting the bin directory location for a service??!! Pin
LongRange.Shooter2-May-07 5:21
LongRange.Shooter2-May-07 5:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.