|
I've got a bit of code that works nicely, but it's really ugly and it offends my sense of style. To wit,
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static string Header = @"^(D|""D|T|S|I|E|""Z)";
private static string Blank = @"^$";
private static string Total = @"^""To";
private static string Sum = @"^""\d\d/\d\d/\d\d\d\d"","""",";
private static string NewData = @"^""\d\d/\d\d/\d\d\d\d"",";
private static string Data = @"^"""",""\w";
private string[] SourceFiles = new string[100];
private int FileCount;
public string WkgDirectory;
public DataSet MeterReads = new DataSet();
public Regex RgxHeader = new Regex(Header);
public Regex RgxData = new Regex(Data);
public Regex RgxNewData = new Regex(NewData);
public Regex RgxTotal = new Regex(Total);
public Regex RgxBlank = new Regex(Blank);
public Regex RgxSum = new Regex(Sum);
public Queue<string> OutQueue = new Queue<string>();
private int Classify(string s)
{
Match m;
m= RgxHeader.Match(s);
if (m.Success) return 0;
m= RgxBlank.Match(s);
if (m.Success) return 1;
m= RgxTotal.Match(s);
if (m.Success) return 2;
m= RgxSum.Match(s);
if (m.Success) return 3;
m= RgxData.Match(s);
if (m.Success) return 4;
m= RgxNewData.Match(s);
if (m.Success) return 5;
return 99;
}
Mind you, I was fairly drunk when I wrote it, but it works perfectly and I hesitate to change it. But it's still ugly.
I don't like having variables stuck in the Form when they're only used in one method. I'd slide all the declarations down into the Classify() method, but I'm afraid that this would cause them to be created and destroyed every time the method was called. That could really bog things down. My understanding of static methods is a bit fuzzy, but I suspect that if I put all the declarations in the method, and make the method static, this might eliminate the problem. Is that true, or should I just hide all the ugly bits in a region and get along with finishing the app?
Or is there a better way?
Will Rogers never met me.
|
|
|
|
|
Break it out into a library class. Possibly use a classification enumeration and a Dictionary<RegEx,Classification> -- dang, you could write a RegExMatchOmatic<Classification>.
Whaddaya think? Is tomorrow a good day to drive to L.A.?
|
|
|
|
|
PIEBALDconsult wrote: Is tomorrow a good day to drive to L.A.?
No. If there's any way that you can delay the trip a day, do so. Tonight and tomorrow they're supposed to get the worst of it, but by Thursday they roads should be drained, at least. Where are you heading? By what route? The Grapevine (I-5) will be closed by morning, I'm sure, if the temperature drops. And I-15 at Cajon Pass (North of San Bernardino) always closes when the snow starts to fall below 5,000 feet.
Will Rogers never met me.
|
|
|
|
|
We're heading straight west on the 10 to the 5. My in-laws are just off the 5 near Griffith Park so there's only about a mile of surface streets involved. I just hope their hill doesn't slide.
We've lived in SoCal during some big storms and remember the mayhem. We could hold off until tomorrow, but we don't see much point... although it would be good to spend the night in Laughlin...
|
|
|
|
|
PIEBALDconsult wrote: it would be good to spend the night in Laughlin...
Hehehe... That might be risky, too. They're calling for tremendous rains here, as well. But the fact is, it's 8:00 and we still haven't seen anything but a light, sporadic drizzle. I did get new wipers, though, just in case.
Will Rogers never met me.
|
|
|
|
|
We made it safely. We did stop a couple of times during the worst downpours because visibility was very poor.
|
|
|
|
|
Enjoy your holiday, then! The rain is supposed to stop tomorrow, then sunny to partly cloudy the rest of the week(end).
Will Rogers never met me.
|
|
|
|
|
There is a static Regex.IsMatch(input,pattern) method which could fit the bill, and avoid all class members. I am not sure how it would compare performance wise, I don't expect a significant difference but could be wrong.
|
|
|
|
|
Well, for a start, if you want to continue using this, make Classify static. There's no real reason for it to be an instance method. Then, I would consider creating a Dictionary of the patterns. You could rewrite this as:
private enum DataMatches
{
Header = 0,
Blank = 1,
Total = 2,
Sum = 3,
NewData = 4,
Data = 5,
Unmatched = 99
}
private static void SetupRegex()
{
if (dataPatterns != null && dataPatterns.Count > 0) return;
dataPatterns = new Dictionary<string, DataMatches>();
dataPatterns.Add(@"^(D|""D|T|S|I|E|""Z)", DataMatches.Header);
dataPatterns.Add(@"^$", DataMatches.Blank);
dataPatterns.Add(@"^""To", DataMatches.Total);
dataPatterns.Add(@"^""\d\d/\d\d/\d\d\d\d"","""",", DataMatches.Sum);
dataPatterns.Add(@"^""\d\d/\d\d/\d\d\d\d"",", DataMatches.NewData);
dataPatterns.Add(@"^"""",""\w", DataMatches.Data);
}
private static DataMatches Classify(string s)
{
SetupRegex();
foreach (KeyValuePair<string, DataMatches> kvp in dataPatterns)
{
if (Regex.IsMatch(kvp.Key)) return kvp.Value;
}
return DataMatches.Unmatched;
}
|
|
|
|
|
Yes, that's what I'd do, but I'd wrap it in a class and have an Attribute to associate the RegEx with the enum member.
private enum DataMatches
{
[RegExAttribute("pattern",Options=options)]
Header = 0
,
|
|
|
|
|
This always returns the same hashcode:
static Regex myRegex = new Regex("");
public static int Bar()
{
return myRegex.GetHashCode();
}
This doesn't,
public static int Foo()
{
Regex myRegex = new Regex("");
return myRegex.GetHashCode();
}
So the object is being created and destroyed at each call, which is expected I suppose. Whether this is heavy enough to have impact on the performance significantly is another matter. A suck-it-and-see approach is best in these circumstances (assuming performance is an issue in the first place, otherwise I'd just go for the cleanest code).
|
|
|
|
|
That is correct, and as expected. Of course in VB you could make the variable static and it wouldn't change.
|
|
|
|
|
|
You're in the wrong forum since C# cannot make any use of ATL.
You're looking for the Visual C++ forum, right above the C# forum in the left-side navigation of this page.
|
|
|
|
|
|
I'd like to be able to see the instant a legal banner appears in a remote desktop window and send an enter key to it to dismiss it during logon.
Does anyone know how to read the windows of a remote desktop window? Are they encrypted?
|
|
|
|
|
I'm pretty sure that what I'd try won't work, but I can't try it until I get back to work on Tuesday.
|
|
|
|
|
Is it complicated or can you post something here?
I'm wondering if the remote desktop session is encrypted or not? If it is encrypted, I should be able to figure out the key since I am the initiator of the session? At least I would think so?
|
|
|
|
|
It looks like it is indeed encrypted
Using Encryption
You can use encryption to protect the data that travels between the terminal server and the terminal services client. If you fear unauthorized interception of the data as it travels between the two, you should enable encryption. RSA RC4 encryption is used; encryption can be set to one of the following three levels:
* High: encrypts both the data sent from client to server and the data sent from server to client using a 128 bit key.
* Medium: encrypts both the data sent from client to server and the data sent from server to client using a 56 bit key if the client is a Windows 2000 or above client, or a 40 bit key if the client is an earlier version.
* Low: encrypts only the data sent from client to server, using either a 56 or 40 bit key, depending on the client version. Useful to protect usernames and passwords sent from client to server.
To change the encryption level, you must be an administrator. In Programs | Administrative Tools, select Terminal Services Configuration and perform these steps:
1. In the left console pane, select Connections.
2. In the right details pane, right click RDP-TCP and select Properties.
3. Click the General tab.
4. Under Encryption level, select the desired level in the drop down box and click OK.
|
|
|
|
|
Hi Experts,
I have an issue with the GridView for wich I want to set a column htmlencode property to false.
Here is the data source. I have no databound columns because I'm not using SQLDataSource.
public void LoadStories()
{
SqlConnection con = new SqlConnection("Data Source=dpeta-ro;Initial Catalog=MINTPRINT;Integrated Security=True");
SqlCommand cmdSelect = new SqlCommand("select news_id \"id\", title \"title\", news_text \"story\", created_date \"created on\", active from news", con);
SqlDataReader rd;
try
{
con.Open();
rd = cmdSelect.ExecuteReader();
gwNews.DataSource = rd;
gwNews.DataBind();
con.Close();
}
catch (Exception err)
{
lblError.Text = err.Message;
con.Close();
}
}
The news_text column has HTML code that i want the GridView to render as HTML code, without encoding it. Because the GW has the Encode set to true by default i need to change it dynamically.
Any ideas?
Thank you,
Alex
“Be the change you want to see in the world.”
|
|
|
|
|
Hi,
in my project I use:
command = new SqlCommand(SQLStatement, DataBase.SqlDataReader
reader = command.ExecuteReader();
to query "part_ID" from large table. Each part_ID can have more subparts up to n level.
They are all stored in same table. At the moment I use foreach statement to recursivly query
parts from table until they have no sub parts. As there are up to 10000 subparts for some of parts
it takes up to almost a minute to get all subparts (each query takes app. 15 ms).
Is there a faster (more efficient) way to get the same job done?
|
|
|
|
|
|
Use a DataSet and fill it with each table and set a DataRelationship. Although it may use more memory depending on how much data you have it beats 10000 individual queries.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Could you show some of the schema?
|
|
|
|
|
All queries are done from the same table (lets call it "parts_table").
Let say I start with "part_ID" = 1001 --> query returns 120 "subpart_id" (1002, 1004, 2030 ...)
then i execute same query 120 times and get n subparts for each of 120 subpart_ID from first query and so on
until there are no subparts left. As you can see the number of queries can increase very fast ...
Table: "parts_table"
id part_id subpart_id
1 1001 1002
2 1001 1004
3 1002 2150
4 1002 3250
5 1004 1250
.
Part description for "part_id" is in separate table "part_info". I use join to get info for "part_id" ...
|
|
|
|
|