|
|
I know JTJ has already answered your question, but just in case you are interested Dr. GUI.NET has written a very good article on the string objects on MSDN. Here is the link.
Strings in the .NET Framework
Nick Parker
|
|
|
|
|
I want to create a Hashtable of strings and function pointers. How can I do that in C#/ASP.NET?
I'm just guessing here...
public string MyFunc(string instr)
{
return instr + "blah";
}
Hashtable table = new Hashtable;
table.Add("myfunc", MyFunc);
string str = "blah";
string blahblah = (table["myfunc"])(str);
something like that? But I'm not sure how to store the function in the hashtable and how to retrieve it and use it.
Todd Smith
|
|
|
|
|
In C# a function pointer is a delegate, so you'll be storing a delegate in the hashtable, and using the string as the key.
The delegate that can be used for MyFunc above would be:
public delegate string FunctionDelegate(string instr);
To add that to the hashtable you would do something like this.
Hashtable table = new Hashtable();
table["myfunc"] = new FunctionDelegate(MyFunc); (you could also use the Add method, but I prefer the array-like access).
Now to get it back out and use it;
FunctionDelegate d = table["myfunc"] as FunctionDelegate;
d(str); I found understanding delegates the hardest part for me to learn; once you get it though you'll wonder why you didn't get it sooner
Good luck,
James
|
|
|
|
|
Sweet. Now I'll be able to pull specific fields out of an ASP.NET form and convert the values before they get updated into the database.
public delegate string HoursToSecsCallbackType(string hours);
HoursToSecsCallbackType fp = new HoursToSecsCallbackType(Utils.HoursToSecs);
Hashtable table = new Hashtable();
table.Add("hours", fp);
string column = "hours";
TextBox textbox = (TextBox)objArgs.Item.FindControl(column);
if (table.ContainsKey(column))
{
HoursToSecsCallbackType fp = (HoursToSecsCallbackType) table[column];
textbox.Text = fp(textbox.Text);
}
Todd Smith
|
|
|
|
|
Is there a good tutorial somewhere that includes basic file i/o?
I've found this:
FileStream fs = new FileStream(@"c:\mcb.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter filef = new StreamWriter(fs);
filef.BaseStream.Seek(0, SeekOrigin.End);
filef.Write(" File Write Operation Starts : ");
filef.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
filef.WriteLine(" First Line : Data is first line \n");
filef.WriteLine(" This is next line in the text file. \n ");
filef.Flush();
but it doesn't work... I guess I'm looking for a basic explanation of what each one does and how to use them. A simple working example would be great, a tutorial would be great. Any help would be great
Thanks for the help.
|
|
|
|
|
Aside from missing a call to the Close() method on the stream that should work; what errors are you getting?
James
|
|
|
|
|
|
This is the error I get(in runtime):
Application has generated an exception that could not be handled.
Then it gave me the process id and thread id (do you want those?).
Well anyways, here is my script. I an very new to C# so chances are I am making some stupid error where you wouldn't expect it (which is why I include the whole source).
using System;
using System.IO;
class functions
{
public static void writefile()
{
FileStream fs = new FileStream("data.txt" , FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("Testing\n TESTING!\n yay!");
sw.Flush();
}
}
class app
{
public static void Main()
{
Console.WriteLine("Before writefile()");
functions.writefile();
Console.WriteLine("After writefile()");
}
}
|
|
|
|
|
I was able execute the code you gave with no errors, file was created and contained the expected text.
Try putting sw.Close(); after sw.Flush() and see if that has any effect.
Try debuging the project to identify which line causes the problem- use "step over"(F10) and "step into"(F11), found under the debug menu in Visual studio.
Sorry this isn't much help, maybe the problem lies somewhere else- OS related maybe?
|
|
|
|
|
As Humpo said the code works fine here.
The error you stated doesn't help us, that error message is just saying some error happened. You should see something that says 'BlahBlahException'; that is the part we need to know so we know what error occured.
James
|
|
|
|
|
Sorry, using the command line compiler... thanks anyways for the help though.
I'll keep plowing through it.
|
|
|
|
|
Well i fixed it, Im not sure how- but i did.
Could you guys tell me how to move the file pointer around? My only real programming experience before this has been php. I know a open file has a internal pointer of some sort, i just dont know how to access it with c#.
Thanks
|
|
|
|
|
Think this is what you are looking for
FileStream objects support random access to files using the Seek method. The Seek method allows the read/write position to be moved to any position within the file.
here's an example of using seek to move to the last 128 bytes of a file...
System.IO.FileStream istream = theFile.Open(FileMode.Open);
long fileLength = istream.Length;
if(fileLength > MP3_LENGTH)
{
byte[] ba = new byte[MP3_LENGTH];
istream.Seek(fileLength-MP3_LENGTH, SeekOrigin.Begin);
istream.Read(ba, 0 , MP3_LENGTH);
GetFields(ba);
}
No doubt, as with all my code, there is a more efficient way
|
|
|
|
|
I would like to know what C# is, its not c++, ive worked that out, looks to me like a cross between c++ and vb.
==================================================
Homepage: www.onyeyiri.co.uk
Email: theeclypse@hotmail.com
"I think Microsoft has invented A.I. My computer has a mind of its own!"
|
|
|
|
|
I would classify it as a cross between C++, Java, and VB (C# borrows the VB construct for enumeration).
In the end though, language matters not because it is the .NET class libraries that do all of the work.
James
|
|
|
|
|
|
It'sa basically a reworking of the J++ code, with some code from VB I'd guess and it's purpose in life is to annoy Scott McNeally.
Christian
I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002
|
|
|
|
|
Christian Graus wrote:
it's purpose in life is to annoy Scott McNeally.
I have no problems with that, Java was a thorn in my behind during my time at MTU
In all actuality though; they needed another language to bridge the gap between VB and C++; VBs reputation is far too ruined for anyone to take MS seriously if it was the only language for .NET ("oh look! another toy").
James
|
|
|
|
|
I was looking at the dotNETSend GUI that was posted and is broken. Discussion on it stopped in April. What is up?
Anyone have a working version?
|
|
|
|
|
I need a sorted array data structure in my program and would like to know about the built in support for this in C#.
I looked into the System.Collections class and found a SortedList class, but this one doesn't allow duplicate keys.
If there isn't one, I might have to write one utlizing the IList interface.
Another option was to write a class that contained the ArrayList class and used the BinarySearch() method to maintain a sorted array.
Any comments would be helpful.
|
|
|
|
|
Think this bit of code might do what you’re looking for(sortable list), but I’m new to C# so there is probably a simpler or better way of doing this
//myArrayList is array of FileSystemInfo
myArrayList.Sort(new FileComparer());
public class FileComparer : IComparer
{
public int Compare(object a, object b)
{
FileSystemInfo o1 = (FileSystemInfo)a;
FileSystemInfo o2 = (FileSystemInfo)b;
return o1.Name.CompareTo(o2.Name);
}
}
|
|
|
|
|
Thanks, but this is a method to sort a list. I am looking for a Sorted List data structure. This would keep the list sorted as items are added to the list.
I am actually in the process of writing one.
A signature always reveals a man's character and sometimes even his name.
|
|
|
|
|
Hey,
How can i put a powerpoint presentation in a Windows Form?
I'm using Office XP
Thanks
Vanclei Matheus
Brazil
MSN Msgr - vmatheus@msn.com
|
|
|
|
|
My MainForm will receive a WM_DEVICECHANGE message from system.
I want to handle the message in the application.
And the MessageQueue is unavailable in my system.
I want to do someting when i recieve the message.
how to do?
Thanks for your advice.
|
|
|
|