|
I'm just curious about something here. The header for String.Format looks like:
string Format(string strInput, object objInput)
right? So, right away, a new string is created, along with a reference to the object.
Now, the code, would have to first find the starting and ending brace and pull out the formatting codes. Then, it would have to take the object and apply the formatting code to it and then return the part of the string before the brace, the formatted code, and the part of the string after the brace. In other words, if you had passed it "format {0} me", the return would look like:
return left(strInput, startingBraceIndex) & Format(objInput, formatCode) & right(strInput, strInput.Length - endingBraceIndex - 1)
or something like that. Of course, it is (hopefully) optimized a bit, so that it's not creating as many copies, but still, wouldn't more memory have to be used for the Format method, plus more processing cycles because or what all has to be done...whereas the second routine is much more straightforward.
As far as performance, I just don't see how the first is more efficient than the second. I also don't see how the first is more readable...even with more parameters, you would have to know what was in each spot in the array, so as far as readability, the second would be more readable as well.
Is
object[] objArray = {devNr, idNr, testNr};
return string.Format("Device #{0}, ID#{1}, Test#{2}", objArry);
really more readable than:
return "Device #" + devNr + ", ID#" + idNr + ", Test#" + testNr ?
Sure the first one is a bit cleaner, but IMO, it's not more readable.
Besides, whatever happened to Occam's Razor?
|
|
|
|
|
Interesting point... I was curious, so I checked String.Format with .NET Reflector... Actually it doesn't split string. Under the hood it uses StringBuilder.AppendFormat. Method is quite long and operates on char[] (from format.ToCharArray()). And I'm tired, so didn't trace the method body. Just took a look on it.
|
|
|
|
|
|
Hello, I need to pull data from another application. I have written to code to get it (using VirtualAllocEx, readmembory.. etc).
The program gets data for simple application, but for a particular application that I need, it doesn't seem to work, it returns empty string..
the application from which I need to retrieve data has a parent class named: Afx:00400000xx
I have used EnumChildWindow to loop through all the windows but it doesnt seem to produce any results
using Visual Studio 2003
Any help will be appreciated
Thanks
|
|
|
|
|
iceeeeman wrote: but for a particular application that I need, it doesn't seem to work, it returns empty string..
Please post some of your code to show where the problem occurs, the above tells us nothing.
It's time for a new signature.
|
|
|
|
|
Hi Richard, thanks for your prompt reply
ReturnString returns empty, here's the code that I've used
Win32.GetWindowThreadProcessId(ListViewHWND, out ProcessID);
ListViewProcessPointer = Win32.OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, 0, ProcessID);
ListViewItemPointer = Win32.VirtualAllocEx(ListViewProcessPointer, IntPtr.Zero, (uint)Marshal.SizeOf(typeof(Win32.LVITEM)), MEM_COMMIT, PAGE_READWRITE);
ListViewPointer_item = Win32.VirtualAllocEx(ListViewProcessPointer, IntPtr.Zero, 1512, MEM_COMMIT, PAGE_READWRITE);
ListViewPointer_subitem = Win32.VirtualAllocEx(ListViewProcessPointer, IntPtr.Zero, 1512, MEM_COMMIT, PAGE_READWRITE);
ListViewItem.cchTextMax = 1512;
string[,] ReturnString = new string[300, 2];
int ReturnStringIndex = 0;
for(int i = 0; i < totalItem; i++)
{
ListViewItem.iSubItem = 0;
ListViewItem.pszText = ListViewPointer_item;
Win32.WriteProcessMemory(ListViewProcessPointer, ListViewItemPointer, ref ListViewItem, Marshal.SizeOf(typeof(Win32.LVITEM)), IntPtr.Zero);
Win32.SendMessage(ListViewHWND, LVM_GETITEMTEXT, i, ListViewItemPointer);
ListViewItem.iSubItem = 0;
ListViewItem.pszText = ListViewPointer_subitem;
Win32.WriteProcessMemory(ListViewProcessPointer, ListViewItemPointer, ref ListViewItem, Marshal.SizeOf(typeof(Win32.LVITEM)), IntPtr.Zero);
Win32.SendMessage(ListViewHWND, LVM_GETITEMTEXT, i, ListViewItemPointer);
IntPtr bytesReaded;
Win32.ReadProcessMemory(ListViewProcessPointer, ListViewPointer_item, ListViewItemBuffer, 1512, out bytesReaded);
Win32.ReadProcessMemory(ListViewProcessPointer, ListViewPointer_subitem, ListViewSubItemBuffer, 1512, out bytesReaded);
MessageBox.Show(ListViewSubItemBuffer+"");
ReturnString[ReturnStringIndex, 0] = Encoding.ASCII.GetString(ListViewItemBuffer);
ReturnString[ReturnStringIndex, 1] = Encoding.ASCII.GetString(ListViewSubItemBuffer);
ReturnStringIndex++;
}
|
|
|
|
|
I can see nothing obvious in the above, but since this is an extract there are a number of declarations missing. I would suggest you try stepping through your code with the debugger and verifying the correct values for your variables and pointers. Also please use the "code block" button when posting code to ensure it is surrounded by <pre></pre> tags.
It's time for a new signature.
|
|
|
|
|
Hi Richard, I tried using Stealing Program's Memory[^] in C#, and also in C++. They worked fine for the demo application provided with it, but when I tried to use it with a another application, I am just able to get the total number of Items in the Listview, without the Items
Could you please help
Thanks
|
|
|
|
|
Sorry but this is a system that I have never used. You could try posting a message in the forum at the end of the article to see if the author or any other user has the answer.
It's time for a new signature.
|
|
|
|
|
Hi
Is there any way to configure ASP.Net, C# application to store all the session values into custom storage.
Session["UserName"] = "XXX";
this has to call my own adopter and send the values rather than In-Memory/SQL Server.
Thanks
|
|
|
|
|
I don't know if this will address your question or not but it is bad practice to directly use the Session object in code. By using a static wrapper class you can have all calls to session centralized as well as strongly and statically typed (a good thing). You then have the freedom to completely control the underlying implementation.
The link below addresses a custom provider.
http://msdn.microsoft.com/en-us/library/ms178587.aspx[^]
|
|
|
|
|
My Qestion: It is possible to call the Class instance name I get with
FieldInfo[] myFieldInfo;
.......
myFieldInfo[i].Name
(Example from MSDN)
Thanks
|
|
|
|
|
Your question is not very clear. Could you clarify what exactly it is that you want to do?
|
|
|
|
|
Sorry for my unclear question.
What I want is to call a method via its class-instance name (I have already create a instance) at runtime from another class in the same assembly.
The problem is, I don´t know the class instance name at compile time.
So I get the name via reflection (FieldInfo class).
Probably a method like "methodInfo.Invoke" can do the job, but so far I´m understand
the class instance must known at compile time.
Thanks
|
|
|
|
|
Just to be clear on that, you have a field of a particular type, and you want to invoke a method on this field instance at run-time. Is that right?
|
|
|
|
|
Something like this should work:
class SomeType
{
public void Foo()
{
Console.WriteLine("SomeType.Foo");
}
}
class AnotherType
{
private SomeType someType = new SomeType();
}
class App
{
static void Main()
{
AnotherType another = new AnotherType();
string fieldName = "someType";
FieldInfo fi = another.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
object fieldValue = fi.GetValue(another);
SomeType someType = fieldValue as SomeType;
if(someType != null)
{
someType.Foo();
}
}
}
|
|
|
|
|
Works perfect! Exactly what I´m searching for.
Thanks.
Regards
|
|
|
|
|
I've just spent a few days creating a User Control in a library, and for a project I want several similar controls. The new controls will contain some of the same elements, in different positions perhaps, and with different names, but the overall User Controls should be of the same size, color, font, etc.
Is there an easier way than manually creating each User Control and adding back all the elements, just to make something almost, but not quite identical to the original?
[EDIT]
I opened the project folder, located the three basic files for the control - .cs, .resx, .Designer - and copied them to a subfolder. I then renamed them and copied them back to the original project folder. Then I used Add Existing Item in VS to locate the renamed files and add them to my project. It choked, of course, because of ambiguity. Using Find and Replace, I opened all the new files, closed all the files I wanted to keep, and selected the option to Find and Replace in all open files, using the original filename and new file name as arguments. It worked like a charm, and will save me hours of manual editing. It even builds without error...
[/EDIT]
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
A good user control is like an onion...
But seriously, make a user control with the basic shape and colour and then specialize from that.
|
|
|
|
|
I have, but now I want to duplicate it several times within the project, but using different names for each copy. I'm thinking I might be able to get away with building the project now, then adding "existing items" from the project folder and giving them new names. Will that work?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Sounds dreadful... maybe you're looking implants[^].
|
|
|
|
|
Your Implant idea is excellent, but I've already done what I added to the original post. Easy stuff, and no tedious retyping. Change the text in the labels, drag 'em around a bit, file off the serial numbers and, voila! A new control is born.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
I have a treeview that I populate with data from various tables in mysql db. I also update the tables based on user actions. I was wondering, is it more efficient to use a mysqlDataAdapter for this or just CommandText and MySql Reader for populating and then separate CommandText statements for updating?
|
|
|
|
|
A reader done right is usually, almost always, faster than a data adapter, dataset solution. In the cases where timings show a data adapter executing faster there is usually a flaw in logic. Where the Data Adapter and data set solutions are usually faster is in Dev/Time not run time.
|
|
|
|
|
|