|
That would work too, however I'm not fond of moving or renaming files (unless that is exactly what is wanted) as there are too many side-effects.
One example is MS Word editing a doc file on the desktop; it renames the old file resulting in a weird filename on the desktop, and creates a new one, in an "arbitrary" location, also on the desktop of course; when done, the old is removed, the new one renamed to its original name, not its original location.
And then there is the single responsibility idea, see SOLID. One file holding content, one file signaling new information!
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.4
|
|
|
|
|
Well, I think the SRP refers more to the functionality than the data being operated on (the files, in this case). You could have one bit of functionality that creates the file, then another to move the file (or rename it).
Now that I think about it, I'd probably go with the rename option (from a practical perspective). Less clutter IMO (no need to keep track of a second folder, and no extra file to clean up).
|
|
|
|
|
With renaming, you're potentially in for a surprise. You have to close the file before you can rename it, and in that short period of time the file could be opened by any of those utterly helpful little programs (indexers, virus scanners, etc) trying to inspect or process the new information, and thus preventing you from renaming the file. So you end up creating the same wait-and-retry loop you need to get reasonable chance of deleting a newly created file; and this logic needs to be added to the producer, who wouldn't really care much. That is why I prefer the sentinel file scheme.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.4
|
|
|
|
|
Hi guys
i wanna get url from navigators like ie, firefox nd opera. but when i run the navigator after my application, i get this exception "CallbackOnCollectedDelegate" xD
here's my code
[DllImport("USER32.DLL", EntryPoint = "DdeClientTransaction",
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr DdeClientTransaction(byte[] pData, uint cbData,
IntPtr hConv, IntPtr hszItem, ClipboardFormat wFmt, XTYP wType, uint dwTimeout, ref uint pdwResult);
public static byte[] Request(string item, ClipboardFormat wFormat)
{
try
{
if (hConv == IntPtr.Zero)
return null;
IntPtr hszItem = DdeCreateStringHandle(DDEid, item, CP_UNICODE);
if ((hszItem == IntPtr.Zero) && (item.Length != 0))
return null;
uint res = 0;
IntPtr data = DdeClientTransaction(null, 0, hConv, hszItem, wFormat, XTYP.REQUEST, timeout, ref res);
if (hszItem != IntPtr.Zero)
DdeFreeStringHandle(DDEid, hszItem);
if (data != IntPtr.Zero)
{
uint len = DdeGetData(data, null, 0, 0);
byte[] buffer = new byte[len];
DdeGetData(data, buffer, len, 0);
return buffer;
}
}
catch (NullReferenceException r)
{ System.Windows.Forms.MessageBox.Show(r.Message); }
in the main i call this method to get url like this
NavUrl _navigation = new NavUrl();
IntPtr hffconv = _navigation.Connect("IExplore", "WWW_GetWindowInfo", ref path);
if (hffconv != IntPtr.Zero)
{
byte[] wwwFF = _navigation.Request("0xFFFFFFFF", NavUrl.ClipboardFormat.TEXT);
if (wwwFF != null)
{
urls = System.Text.ASCIIEncoding.UTF7.GetString(wwwFF);
_navigation.Disconnect();
lastIe = urls;
indexTitle = urls.IndexOf("\",\"");
indexTitleEnd = urls.IndexOf("\"", indexTitle + 3);
thank u
|
|
|
|
|
Hi all, I need to format string and having some problems.
when user enter into a textbox i.e "12345678". I need to format
this input into this. "12 345 678". I am doing this way but not working
string p = TextBox.Text.Trim();
string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));
|
|
|
|
|
You should probably remove the comma's from your Format string. The rest is just about adding up your substring index and length values correctly.
|
|
|
|
|
You should replace commas to spaces and String.Format is uppercase in the S
|
|
|
|
|
mabrahao wrote: String.Format is uppercase in the S
Actually, both string and String will work.
|
|
|
|
|
Try this:
string p = TextBox.Text.Trim();
p = string.Format("{0} {1} {2}", p.Substring(0, 2), p.Substring(2, 3), p.Substring(5, 3));
MessageBox.Show(p);
Here is what was wrong with your code:
- Your sample output contains spaces, but your format strong contains commas.
- string.Format returns the modified string, yet you were not assigning the return value to anything.
- string.Substring has 2 parameters. The second prameter is the string length, not a second index. So where you used 4 and 7, you should have used 3.
|
|
|
|
|
Thanks for your reply. it is working for me.
|
|
|
|
|
One thing you should know - the second parameter in Substring is not the position to end on, it's the length of the substring to extract. It goes Substring(index, length);
|
|
|
|
|
|
You should NOT do this with string manipulations; it would go completely wrong if the user types strange things, such as the intended number prefixed with a lot of zeroes.
The proper way to do this consists of two steps:
1.
parse the user input, i.e. turn his characters into an actual number. The simplest way would be:
int val;
bool OK=int.TryParse(myTextBox.Text, out val);
Whatever the user does wrong (e.g. typing letters, or providing no input at all) will result in OK being false; if the input is acceptable, OK will be true and val will contain its value.
2.
format the number in the way you want it. A simple way to get two spaces in a number assumed to require 8 digits is:
string s=string.Format("{0:## ### ###.##}", val);
Assuming val=12345678 the result will be 12 345 678
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
I always like your answers.
|
|
|
|
|
Could be also
1. If the currect culture uses ' ' as a group separator:
string.Format("{0:n0}", val)
2. If it doesn't:
var provider = (CultureInfo) CultureInfo.CurrentCulture.Clone();
provider.NumberFormat.NumberGroupSeparator = " ";
string.Format(provider, "{0:n0}", val);
Greetings - Jacek
|
|
|
|
|
yes, there are alternatives for each of both steps. Having the two steps was the essence in my message.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.4
|
|
|
|
|
To be precise: My (1) and (2) were alternatives to your (2). They both assumes that your (1) was already done.
Greetings - Jacek
|
|
|
|
|
Of course; and that is how I understood it.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.4
|
|
|
|
|
Hi guys, i have a method like this: public List<t> listObj<t>() {}
and i want to instantiate a object with this type T, i tried this: T obj = new T(); doesnt work, so i tried T obj = default(T); and doesnt work too... how can i instantiete this obj??? Thanks.
The code:
public class MysqlDireto
{
private static MysqlDireto _instancia;
private static string connectionstring = "Server=;Database=;Uid=;Pwd=;";
public static MysqlDireto Instancia
{
get { return _instancia ?? (_instancia = new MysqlDireto()); }
}
public List<T> ObterLista<T>()
{
MySqlConnection conn = new MySqlConnection(connectionstring);
string comando = "SELECT * FROM " + typeof(T).Name;
MySqlCommand cmd = new MySqlCommand(comando, conn);
conn.Open();
MySqlDataReader leitor = cmd.ExecuteReader();
List<T> lista = new List<T>();
if (leitor.HasRows)
{
while (leitor.Read())
{
T obj = Foo<T>();
obj.GetType().GetProperty("Id").SetValue(obj, leitor["Id"], null);
lista.Add(obj);
}
}
conn.Close();
return lista;
}
public T Foo<T>() where T : class, new()
{
return new T();
}
}
modified on Wednesday, June 8, 2011 2:11 PM
|
|
|
|
|
you have to add new() in the where clause of the generic definition.
public T Foo<T>() where T : new(){
return new T();
}
Or such
|
|
|
|
|
i got this msg: Error 800 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method
|
|
|
|
|
I think the error message is clear about what you need to fix: pass your generic class a type that can be instantiated with new T() (i.e. a non-abstract, with a parameterless constructor).
|
|
|
|
|
but the T class is a non-abstract and has no constructor parameter
|
|
|
|
|
The class that you pass as the generic parameter for T must be like the 'Test' class in the example that Ennis posted below.
|
|
|
|
|
public class Test {
public Test() {
}
}
public class Bar {
public static void Test<T>() where T : Test, new() {
T t = new T();
}
}
...
Bar.Test <Test>();
|
|
|
|
|