|
I don't plan to reuse them, I just want them out of my sight when I'm trying to figure out what my Form is doing. I find it handy to debug them in the Form, but once they work, I don't want to look at them anymore. I created a Utilities class in a separate file tonight, then cut and pasted the material from the Form to the Utilities file. That should work for now.
Will Rogers never met me.
|
|
|
|
|
If I'm not going to reuse stuff, then I usually leave it in the form code but make a lot of use of #regions.
Group my UI stuff, BGW stuff, Logging, Helper methods, etc into seperate regions. Whatever makes sense. I then collapse all the regions and just look at the bits that interest me at the time.
|
|
|
|
|
RCoate wrote: just look at the bits that interest me at the time
Yes, but what does that have to do with code? 
|
|
|
|
|
One man's formatting is another man's ASCII pr0n.
Will Rogers never met me.
|
|
|
|
|
I completely forgot about Regions, and that's a perfectly good use for them. We didn't need no stinkin' regions back when I was punching Hollerith cards for FORTRAN programs, and I forgot that we have that option. Still, it does seem kludgy to me to keep details like that in the main Form file. I like a nice, clean file that treats functions like well known old friends, to be called when convenient, but otherwise out of sight.
Will Rogers never met me.
|
|
|
|
|
That's a great question.
I'd go with a helper class. Over the years I've built a slew of helper classes (each with just a few methods) for working with everything from string s to controls like ListView , TreeView , ListBox , etc. I get a lot of code reuse from them and find them indispensable.
/ravi
|
|
|
|
|
I'm new to dealing bigger projects in C#, but recently when I had this situation,I created a new Utility class and made all these functions static. And kept it so generic and independent that I can rip this class off from the project and make a class library.
Seriously dumping code on the main form is a bad idea to me at least. I consider that as the launch platform. And every other functionality I push them out as other classes.
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.
|
|
|
|
|
That seems to be the consensus, and that's what I've done with this bit I have so far. I expect to have some more helper functions to add later as I continue to work on this, so at least I'll have a repository of things I might need again to reference. In the form itself, I'll just stick in a comment to remind me what the function is supposed to do, but keep the details off the form; that seems so much cleaner to me, especially since I'm a perpetual beginner and easily confused.
Will Rogers never met me.
|
|
|
|
|
Like every other developer here I have a utility class(es) that hold these little tools. Caveat, make sure you document the utilities REALLY well, not just what you are doing but why you are doing it. I have the sinking feeling I get when ratting through a utility folder/class and wondering why the hell I would write such a thing.
Also keep you utilities in smaller classes, string, date function, treeview etc all should have their own class, don't shove them into one humongous Utilities.cs
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Rats. You're on to me.
I created a code file called Utilities.cs, added the project's namespace, then added a static class called Utilities. VS bitched and moaned about it without the class declaration, and was quite vexed with me when I tried to include it in the Form file with a using statement. My bad.
Anyway, I'll take your advice to heart, and make more classes if I need other types of functions. For now, I only have the one function, which just strips the quotation marks out of a string variable. That was only necessary because the data source I have quotes every value, including empty ones, then separates them with commas. String.Split() doesn't like that very much, but it works perfectly after I clean the string. Since my source data is all in csv form, I expect that any other functions I need will be related to text handling and belong in the same class, but who knows where this will take me. I still don't know what I want the program to do, just that I know that these daily emails of csv data are doing me no good at all. Once I get them sorted out and stored in SQL Server, I'll figure out what else I want to do with the stuff.
Talk about 'agile' programming... sheesh!
Will Rogers never met me.
|
|
|
|
|
:cough: Rive[^] :cough:
Oh, hey, it's Thursday!
|
|
|
|
|
Great. Now you tell me, after I spent two days on this.
Well actually, a couple of dull hours at work with nothing more interesting to fill them. Don't tell the boss...
Will Rogers never met me.
|
|
|
|
|
I too create static utility classes for things which are generic (in the non-technical sense of that word) functions that have nothing to do with your particular problem. De-quoting a string seems like a string utility to me.
|
|
|
|
|
Hi,
I'm trying to convert this string array to byte array. I keep getting exceptions no matter what method I try. I tried following the example on another website, but it's not working and I can't figure out how to get it into my returned byte array. So it takes strings 40, AA, and 00 and puts them in a byte array. Even if I comment out the wBuf addition, it's throwing an exception when I run it.
I'm not sure if I'm showing the string array in the code correctly. It's like this:
dataString =
"40 A8"
"AA A8"
"00 4E"
private int ExtractBytesFromDataSTringArr(string[] dataString, ref byte[] wBuf, ref byte[] sBuf)
{
int line = 0;
string [] dataString= {"40 A8", "AA A8", "00 4E"};
for (int i=0; i < myStringArr.Length; i++)
{
delimitedChar = ' ';
stringArray = dataString[i].Split(new char[] {delimitedChar}, StringSplitOptions.RemoveEmptyEntries);
int NumberChars = dataString[0].Length;
byte[] byteArr = new byte[NumberChars/2];
for (int j = 0; j < NumberChars; j+=2)
{
byteArr[j/2] = Convert.ToByte(dataString[0].Substring(j,2), 16);
wBuf[line] += byteArr;
}
line++;
}
|
|
|
|
|
It seems you split dataString[i] to form stringArray , but then don't access that. Your call to dataString[0].Substring(j,2) should probably be for stringArray , but I don't think you need to deal with the digits separately anyway (I think what you're doing with them is probably wrong); how about something like:
wBuf[line++] = System.Byte.Parse
(
stringArray [ j ]
,
System.Globalization.NumberStyles.HexNumber
,
null
) ;
|
|
|
|
|
Sorry about the mis-naming of my variables. myStringArr is the same as dataString. Yes, when I do the Convert.ToByte, I should be using stringArray instead. I had to re-name things to put on the website and I made mistakes.
|
|
|
|
|
Here is a quick and dirty way:
...
string[] dataString = { "40 A8", "AA A8", "00 4E" };
byte[] bytes = ConvertToBytes(dataString);
...
private byte[] ConvertToBytes(string[] data)
{
List<byte> bytes = new List<byte>();
foreach (string line in data)
{
string[] values = line.Split(' ');
foreach (string value in values)
{
bytes.Add(byte.Parse(value, System.Globalization.NumberStyles.HexNumber));
}
}
return bytes.ToArray();
}
In the real world, you'd want error checking, and probably to look at the efficiency a bit if it is going to be used a fair amount - that isn't very efficient at all.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Thank you so much! This works great. I just had to access values[0] and values[1] to store separately into the buffers. Plus I added some exception catching. Thanks!!!
Mich
|
|
|
|
|
You're welcome!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
What about if I'm going in the opposite direction? I want to take a string or byte array and each value which is shown in decimal, I want to convert to hex notation? wBuf is a byte[]. I tried this, but it's complaining:
int i = 0;
string[] values = new string[wBuf.Length];
foreach (byte value in wBuf)
{
string temp = wBuf[0].ToString();
values[i] = (temp, (System.Globalization.NumberStyles.HexNumber));
i++;
}
says "cannot convert lambda expression to type string because it is not delegate type".
I also tried:
foreach (byte value in wBuf)
{
values[i] = (byte.Parse(wBuf[i], (System.Globalization.NumberStyles.HexNumber)));
i++;
}
but it says "the best overloaded method match for 'byte.parse(string, system.iformatProvider)' has some invalid arguments".
Any ideas?? Thanks for your help.
modified 17-Apr-12 13:52pm.
|
|
|
|
|
Try:
byte[] wBuf = new byte[] { 23, 0xff, 17, 16 };
string[] values = new string[wBuf.Length];
int i = 0;
foreach (byte b in wBuf)
{
values[i++] = string.Format("{0:X00}", b);
}
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Thanks!! That works really well. The only problem is that when I have a 0, it's showing it as 0 instead of 00. Any thoughts?
|
|
|
|
|
My fault - change the X00 to X02:
values[i++] = string.Format("{0:X02}", b);
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
|
friend give some example about a small project.that must be unique.plz say what type of project is best for me.in C#..//(small project)becz its my first
|
|
|
|