|
1. Using index is faster than using names, since using names does an additional index lookup, but it increases readability considerably and is also safer. Imagine a database developer changing your query from something like this
SELECT A,B,C FROM ...
to this
SELECT B,A,C FROM ...
|
|
|
|
|
Luc Pattyn wrote: Would it also negatively affect performance?
This tends to be treated as a micro-optimisation that is completely useless. There is a simple way to get around this performance hit, which is to use the ordinal based value, but use GetOrdinal to retrieve the position of the ordinal based on the field name.
The thing is, you only need to use GetOrdinal once for each field you are interested in - which means that you can use the ordinal in the loop over the reader.
Effectively, your code becomes something like this (with apologies because I'm just typing this off the top of my head in the CP editor):
int customerId = reader.GetOrdinal("CustomerID");
int customerName = reader.GetOrdinal("Name");
while (reader.Read())
{
Customer cust = new Customer(GetValue(reader, customerId), GetValue(reader, customerName));
}
private T GetValue(SqlDataReader dr, int position)
{
if (dr.IsDbNull(position))
return default(T);
return (T)dr[position];
}
|
|
|
|
|
I like it. The rule now is: don't use numeric field indexes ever; use field names directly, unless you can't afford the speed penalty, then use GetOrdinal as per your example (which is safe but somewhat more verbose).
And you could still make that an extension method on DataReader, as PIEBALD suggested.
Thanks Pete.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Luc Pattyn wrote: The rule now is: don't use numeric field indexes ever; use field names directly,
unless you can't afford the speed penalty, then use GetOrdinal as per your
example (which is safe but somewhat more verbose).
Guidelines to live by
|
|
|
|
|
How to make possible the activation of group-box controls by choosing one of the options in combo-box list?
Best regards
|
|
|
|
|
Look at the documentation first, then come ask questions.
DOCUMENTATION[^]
Looks like OnSelectedItemChanged or OnSelectedValueChanged should work quite well for you.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
|
|
|
|
|
Not sure what you mean by activation. I think you may be referring to either enabled or visible. Lets say it's the enabled property of the GroupBox. First you need to catch the SelectedIndexChanged event of the ComboBox, evalutate the selection and determine if the GroupBox should be enabled
private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
MyGoupbox.Enabled == (MyComboBox.SelectedValue == criteria);
}
Excuse the pseudocode but i think you get the idea
"You get that on the big jobs."
|
|
|
|
|
Hello.
I have a problem. I have two independient forms inside the same project and I want to call a function that is in the form 1 from form 2.
I am using c# for an window application
I have declared in the form 1 the following function:
public void Hola()
{
MessageBox.Show("Hello");
}
But I don´t know how to call from form2
Thank you very much!!
modified on Wednesday, May 18, 2011 11:22 AM
|
|
|
|
|
For a simple excercise, you could create a form level variable in form2 that holds a reference to form1. Then in form2 you can just call the function like this
public Form1()
{
InitializeComponent();
}
public void Hello(string name)
{
MessageBox.Show(string.Format("Hello, {0}", name)
}
public partial class Form2 : Form
{
Form1 form1;
public Form2()
{
InitializeComponent();
form1 = new Form1();
}
private void Form2_Load(object sender, EventArgs e)
{
form1.Hello("Wayne");
}
}
As a simple excercise, this is fine, but in a production app you would want to look into creating a Mediator class to communicate between classes, so that the entities have no need of any knowledge of each other.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
I am hoping this is not for your homework, but, I will answer nonetheless.
Two simple ways to accomplish this would be:
1. Declare your method in Form1 as static and invoke it from Form2 like so:
In Form1...
public static void Hola()
{
MessageBox.Show("Hello");
}
In Form2...
public void InvokeMessage()
{
Form1.Hola();
}
2. Using your declaration like so:
In Form2...
public void InvokeMessage()
{
using(Form1 otherForm = new Form1())
{
otherForm.Hola();
}
}
|
|
|
|
|
I'm assuming that Form1 creates the instance of Form2?
If so you should use an event in Form2 to signal and Form1 should subscribe to the event and call the method.
Basic theory is in this tip[^] which is about passing values but the same theory applies.
|
|
|
|
|
If you need to call a method in another form then the forms are not independent. Form1 is actually dependent on Form2. Sure you can make the method static but you should really consider refactoring this fuctionality into a separate class.
"You get that on the big jobs."
|
|
|
|
|
I have a problem, that is to mask data in certain format that is displayed in the browser
To solve the problem I came up with the following solution
1)Find all the browser window handles (class Internet Explorer_server)
2)Get the IHTMLDOCUMENT2 using the window handle
3)Use IHTMLDOCUMENT2::body::innerHTML to find the data I am looking for and modify it.
The code I have written below throws a COMException. [Could not set the innerHTML property. Invalid target element for this operation.]
IHTMLElementCollection dome = document.all;
foreach (IHTMLElement dom in dome)
{
StringBuilder bf = new StringBuilder(dom.innerHTML);
if (bf.ToString().Contains(pattern.ToString()))
{
bf.Replace(pattern.ToString(), "***-**-****");
dom.innerHTML = bf.ToString();
}
}
Why does this exception occur. How do I fix it so that the webpage contents will be altered.
I wrote another version of the code, it works, but not completely.
I found out that is because the webpage contains multiple frames and so it does not work. I does not work if the SSN number is inside a frame(or something like a frame which holds an embeded object).
StringBuilder bf = new StringBuilder(document.body.innerHTML);
if (bf.ToString().Contains(pattern.ToString()))
{
bf.Replace(pattern.ToString(), "***-**-****");
document.body.innerHTML = bf.ToString();
}
The first piece of code throws exceptions, the second one does not work if there are multiple frames.
What am I doing wrong in both the code above.
How do I go about achieving my solution. Is my approach correct, or is there a better way to do it.
Thanks and regards,
David
|
|
|
|
|
hi,
I want to build Multi user network Application
so what can i do .. for that..
|
|
|
|
|
You cannot really expect anyone to teach you how to build a complex application in a forum discussion. You need to do a lot more research yourself before you come here with more specific questions.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
Do you mean that you want to build an application that conforms to a layered architecture (commonly known as an n-tier architecture)? If so, there are plenty of resources available on the web (or in books) to get you started. I would suggest that you should take a look at Rocky Lhotka's CSLA framework to get some idea as to what's involved in n-tier applications.
|
|
|
|
|
Nah. It's just another way of saying: i go to develop IM app plz send codez its urgent
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
I want to fix the 5000 or so compile errors on our University's website because it hasn't been properly written since day 1.
You scratch my back?
|
|
|
|
|
Tequila, and lots of it, and the feeling will pass.
|
|
|
|
|
hemant saliya wrote: so what can i do .. for that..
Steps
1. Learn the basics of C#
2. Learn the basics of requirements gathering
3. Learn the basics of design
4. Learn the basics of "web" servers including various protocol types.
5. Learn how to program 4 in C#.
6. Use 2 to create your requirements
7. Use 3 and 6 to design your system.
8. Use 1, 4 and 7 to create your system.
9. Unit test your system.
|
|
|
|
|
I m using DataGridView.
It has a checkbox column, i want to check - uncheck this CheckBox control..
In asp.net FindControls() is used to get control, is there any methon like FindControls() for Windows ?
|
|
|
|
|
To set the value of a CheckBoxCell you need to cast it specifically like this:
(dataRow.Cells["your cell index"] as DataGridViewCheckBoxCell).Value = true;
This presumes dataRow is your current datagridview row.
Hope this helps
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
Hi, Currently I have a server-client application which works fine. I only want the server to serve for a predefined number of clients. Say, up to 20 clients. If I adjust the below behavior it should be okay.
But since each client subscribe to server with a client number, e.g. client22 or client29 which are greater than number 20 will connect to server when the total number of concurrent sessions or instances are lover than 20. And so, if these two clients connect and allocate two connections before, Any two clients with numbers upto 20 will not be able to connect afterwards. Any solution for this? Thanks
in short: I want only 20 clients, client1,client2.....client20 can connect but client21, client22 and so on can't connect
ServiceThrottlingBehavior behaviour = new ServiceThrottlingBehavior();<br />
behaviour.MaxConcurrentSessions = 20;<br />
behaviour.MaxConcurrentInstances = 20;
|
|
|
|
|
What do "client number"s and ServiceThrottling settings have to do with each other?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Isn't the servicethrottling onyl way of limiting numbers of clients that can connect to server by controlling service instances and concurrency? So I want to limit max connections to a specific number of clients
|
|
|
|