|
If you have code that initializes the hashtable from an external source (file, db etc.) I would create a class that handles all the initialization in the constructor then you can create public methods to access the hashtable or make the hashtable a public readonly property. This will separate your hashtable logic from other objects in your program and make it cleaner all around.
Depending on where you need to access the hashtable you can initialize a new instance of your class at a program level, or just within a single form.
static class program
{
internal MyCustomHashtable mch = new MyCustomHashtable();
}
or...
public Form1 : form
{
internal MyCustomHashtable mch = new MyCustomHashtable();
button1_click(...)
{
string val = mch.GetValue(textBox1.text);
}
}
Not sure what exactly your after, hopefully this helps you out.
|
|
|
|
|
Trak4Net -
You absolutely helped me out Right now I am struggling with the proper placement and scope; basically making sure the hashtable is available when I need it. The example provided showed me where I was going wrong in my code. I will make sure to post code next time.
Thanks so much!!
|
|
|
|
|
Glad I could be helpful. One thing I'd like to warn of (I have seen this be a huge issue with some developers) is in the initialization of a program global variable. The example I gave might not be the best way to initialize it because it will get initialized when the program starts, if their was something coded wrong or a dependency within that class missing your program would crash without any information as to why (unless your in debugger). I would tend to initialize the way shown below. This really isn't necessary for most simpler variables, but lets say you depended on a file in the constructor of your custom hashtable class, and that dependent file was absent for some reason, and you hadn't wrapped that code in a try catch. Then your program would crash immediately when an exception is thrown trying to access a file that does not exist. With the example below you will catch the exception and can handle it gracefully.
Anyway, just thought I'd give my two cents on that, not sure if it would be helpful for you but maybe others???...
static program
{
internal MyCustomHashtable mch;
static void main()
{
try
{
mch = new MyCustomHashtable();
}
catch (Exception ex)
{
}
}
}
|
|
|
|
|
Is there a particular requirement for you to be using a Hashtable ? Would you not be able to use a generic Dictionary instead?
|
|
|
|
|
There is not really a "requirement" for me to use a Hashtable; it's just what I know how to use. I need to read the csv file and then have the user enter their ein number and then I pass back basic demographic information for user confirmation.
What are the advantages of using a generic Dictionary instead of a Hashtable?
I'm sure what I am doing is not best practices and I'm sure I'll cringe looking back at this program at some point in the future
Below is my work-in-progress code...
Thanks for taking the time to review my question and providing any feedback!
public partial class Form1 : Form
{
Hashtable empData = new Hashtable();
public Form1()
{
InitializeComponent();
string fName = "";
string mName = "";
string lName = "";
string userProfile = "";
string userEIN = "";
using (CsvReader csv = new CsvReader(new StreamReader(@"C:\temp\test.csv"), true))
{
int fieldCount = csv.FieldCount;
string[] headers = csv.GetFieldHeaders();
string[] data = new string[csv.FieldCount];
while (csv.ReadNextRecord())
{
csv.CopyCurrentRecordTo(data);
fName = data[1];
mName = data[2];
lName = data[3];
userProfile = data[5];
userEIN = data[11];
string concatData = (fName + "," + mName + "," + lName + "," + userProfile.ToLower());
empData.Add(userEIN, concatData);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string saveEIN = userEIN.Text;
string hashData = empData[saveEIN].ToString();
string userName = WindowsIdentity.GetAnonymous().Name;
}
}
|
|
|
|
|
As Pete would tell you Dictionary<string, string> is an implementation of a Hashtable that is type-safe, and more efficient (in that no boxing/unboxing is done). Just change the Hashtable in the empData declaration to Dictionary<string, string> (both of them), and remove the .ToString() where you set hashData (it is unnecessary).
Other simple suggestions I'd make for this code:
* You ought to validate that the FieldCount is at least large enough for the number of fields expected, to detect a malformed CSV file.
* If there is any chance you'll need the individual fields later and have to "tear-apart" the concatenated string, I'd suggest creating a class to hold the individual fields as properties and storing that in the Dictionary instead. It can keep the concatenated string as a property, or concatenation can be used where necessary.
* empData.Add(userEIN, ...); will throw an exception if a userEIN value is not unique. You should handle that case, or change to empData[userEIN] = ...; if you just want to keep the last value encountered for the given userEIN .
* You shouldn't need the data array and the use of csv.CopyCurrentRecordTo(data) . Your code appears to be using the CsvReader of Fast CSV Reader of Sebastien Lorion[^]. If so, it has the ability to get the field values of the current record by the name found in the headers row. So, I'd suggest making constant strings for the expected header names and use the indexer to get the field values. E.g.:
const string FirstName = "First Name";
const string MiddleName = "Middle Name";
fName = csv[FirstName];
mName = csv[MiddleName];
This is much more readable and self-documenting.
* If you use the constants for the expected header names, you should verify that those headers are actually present in the file. You can use, e.g. csv.GetFieldIndex(FirstName) >= 0
* If you don't want to use the header names to lookup the field values, then you can still use named constants for the column index values, instead of the hard-coded column numbers you use now.
const int FirstName = 1;
const int MiddleName = 2;
fName = csv[FirstName];
mName = csv[MiddleName];
I hope this all helps.
|
|
|
|
|
Thank you so much for the detailed explanation. I will implement the suggestion you provided!!
|
|
|
|
|
is this possible to load unmanaged dll? When changing "target platform" to x86. It does work ok. But we prefer to keep it AnyCPU state.
We keep getting "BadImageException" error on this when using ANYCPU state.
|
|
|
|
|
As far as I know, you can't mix 32-bit and 64-bit in the same process. A 32-bit EXE can't load a 64-bit DLL, and vice-versa.
So if your unmanaged DLL is only available as 32-bit, you need to build your process as 32-bit (x86)... The "AnyCPU" setting is probably defaulting to 64-bit, because it doesn't detect any managed 32-bit assemblies in the project.
|
|
|
|
|
As Ian said, Windows only deals with homogeneous processes, so all the code must be 32-bit or all of it 64-bit.
A managed app targetting "AnyCPU" gets launched as 64-bit on a 64-bit OS, and will refuse to load 32-bit code.
If you have unmanaged dependencies, the practical way is NOT to use "AnyCPU" and therefore it is a pitty that
(1) it is the default, and (2) Visual Studio Express doesn't really give you direct access to any other setting.
|
|
|
|
|
I'm trying to understand what's going on... When I created the custom control and dynamic buttons, and run it on my pc. i see everything on my GUI. But when running on my co-worker's computer, the dynamic buttons aren't showing. These buttons are hard coded in their GUI location.
I apparently discovered that the buttons were hidden behind the custom control on my co-worker's computer.
The computer and gui size are as follows:
my computer (x86):
Screen resolution - 32-bit (true color) 1440x990
GUI size: 549, 352
my co-worker (x64):
Screen resolution - 32-bit (true color) 1920x1080
GUI size: 732, 433
What gives? I had believed that the GUI would be at the same size. Care anyone explain? My co-worker got my source code and her visual studio (same version as mine) and the custom control size has shown to be bigger size than mine.
modified 9-Jul-12 13:03pm.
|
|
|
|
|
What DPI are you both viewing at?
|
|
|
|
|
Ahh! That explains it! My pc is set to 100% (96 DPI) and she has 125% (120 DPI).
So that would mean I would need to make adjustment on my code to calculate DPI value.
Gonna find out how to do this!
|
|
|
|
|
Or not. If you use anchoring, you effectively remove the dependency on resolution.
|
|
|
|
|
Rather than recalculating the positions yourself, you may get satisfactory results by using the Anchor or Dock properties of your Controls; setting Form.AutoSize often helps too.
|
|
|
|
|
Hello!
Could anyone please help me with this:
XAML:
<ContentPresenter Content={Binding Path=Navigation}/>
ViewModel
private UserControl navigation;
public UserControl Navigation{
get {return navigation;}
set {navigation = value; OnPropertyChanged(()=>Navigation);}
}
void LoadControl(UserControl control)
{
this.Navigation = control;
}
What I want to do is create an animation which changes the Width of the ContentPresenter stepwise.
If control != null step from 0 => control.Width
else step from control.Width => 0
Any ideas how this can be done (can this be done in XAML soley)?
Help, as always, would be highly appreciated.
|
|
|
|
|
Hi,
I've faced some problem when am trying to return the LINQ class type function's result as a DataSet type.
If any one Know About this, Help me.
thanks in Advance!
|
|
|
|
|
I am trying to create an Excel line-column combination chart using C#. I know how to create a line chart or a column chart but I don't know how to create a combined chart. Could anyone please point me a direction or provide a short sample code on how to create such chart? Thanks in advance.
|
|
|
|
|
You can achieve this adding SecondaryAxis to the chart.
"The worst code you'll come across is code you wrote last year.", wizardzz[ ^]
|
|
|
|
|
Can you show me example code?
I have tried and got error.
myChart.Series("Series 2").XAxisType = AxisType.Secondary;
|
|
|
|
|
Which office version are you using?
Basically, you will have to create a new chart type and then add a series to it. You will then have to add this to your Chart.
"The worst code you'll come across is code you wrote last year.", wizardzz[ ^]
|
|
|
|
|
How to draw a excel chart with combination of bar and line charts.
eg: left handside is frequency, right handside is percentile with the interval as x-axis.
And how to remove the legend entry?
|
|
|
|
|
|
That is a surface chart.
What I want is a line graph + column graph on the same chart
|
|
|
|
|
I have an array includes list of users session id.
I wanna expire all of them.
Is it possible in ASP.Net (c#), if so, how ?
else, how can I do it with C#?
Thanks in advanced
|
|
|
|