|
Ok! Thank!!!!
My problem is solved.....
Hallo
|
|
|
|
|
I want this statement valid in C#:
Integer i = 0;
In C/C++ you can easily do it by define an alias name for it, for example
#define Integer Int32
But you can not do it in C#. So what should I do?
Thank you for your reading.
|
|
|
|
|
using Integer = System.Int32;
...
Integer i = 0;
Keep in mind that the C# int keyword is an alias for System.Int32.
|
|
|
|
|
|
hey Judah, thanks from me too!
i thought aliasing we can do only for namespaces!!
|
|
|
|
|
No, you can alias types, too. There is one caveat: unlike C/C++, aliases are per-file. Each file that wants to use the Integer alias described above would have to have to specify it in using declarations.
They are especially useful with generics, IMO.
using IntStringPair = System.Collections.Generic.KeyValuePair<int, string>;
...
List<IntStringPair> listOfPairs = ...;
|
|
|
|
|
Judah Himango wrote: using IntStringPair = System.Collections.Generic.KeyValuePair<int, string="">;
That's a shame, I hated when people did that in C++....
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Heh, well, at least it's limited in scope. It's true, I always hate having to track down aliases in C++. Or worse, aliases of aliases of aliases...etc. I don't think you can do alias an alias in C#, which is good.
Question is, is the aliasing worth it? Which one would you rather encounter:
List<KeyValuePair<int, string>> listOfPairs = new List<KeyValuePair<int, string>>();
foreach(KeyValuePair<int, string> pair in listOfPairs)
{
...
}
or
using IntStringPair = System.Collections.Generic.KeyValuePair<int, string>;
...
List<IntStringPair> listOfPairs = new List<IntStringPair>();
foreach(IntStringPair pair in listOfPairs)
{
...
}
|
|
|
|
|
I'd prefer the former, because there's no question of what it is. Actually, I would create a struct that names the int and string according to what they represent.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Interesting. I'd prefer the latter because it is descriptive enough by its name alone and it's easier on the eyes than the double generic type. But, I do see where you're coming from. Aliases are certainly open to abuse, albeit on a much smaller level than C++ #defines. Come to think of it, I can't say I've seen alias abuse in any project I've worked on, especially nothing so terrible as abominations like A Better C[^] or VB++[^].
|
|
|
|
|
I've worked on C++ projects where I was always going back to see what the typedefs meant. I hated it.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
I agree with Chris. I've dealt with C++ projects where the developer went crazy with typedefs. Even though C# has a similar ability, it is much simpler to use the native data type names. They may not be as short as we want or as clear within the specific application domain, but they are unambigous and standard. I don't have to worry about a new developer trying to understand what an IntStringPair is, or look for it on MSDN. KeyValuePair<int, string> clearly defines the object using terms that everyone is familar with.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Christian Graus wrote: That's a shame, I hated when people did that in C++....
I think they are useful, since you haven't to interpretate again and again complex expressions.
Anyway it's a matter of taste.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Christian Graus wrote: That's a shame, I hated when people did that in C++....
That depends how! With a define or a typedef?
|
|
|
|
|
I have a block of code like the following
sqlConnection1.Open();
using (SqlDataReader reader = mySqlQuery.ExecuteReader())
{
//use reader here
}
sqlConnection1.Close();
my question is, with reader inside a using block, reader.Close() is automatically called once execution goes out of the using block scope. do I have to call sqlConnection1.Close() then?
also if an exception occurs, do I have to put my sqlConnection1.Close() inside a finally block, if the call is actually necessary?
Thanks in advance!
|
|
|
|
|
To be safe you can do like this
using(sqlConnection1 = new SqlConnection())
{
sqlConnection1.Open();
using (SqlDataReader reader = mySqlQuery.ExecuteReader())
{
}
sqlConnection1.Close();
}
You can also do this:
sqlConnection1.Open();
using (SqlDataReader reader = mySqlQuery.ExecuteReader(CommandBehavior.CloseConnection))
{
}
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
|
|
|
|
|
My C# WinForms 1.1 application uses same datareader query a lot of times and it is allowed that it can use cached result from previous datareader creation.
How to cache result obtained by datareader in memory so that in next time
cached query result is returned instead of making trip to server?
Or is it better to use DataTable and cache DataTable in memory?
Andrus
|
|
|
|
|
A DataReader only reads the data from the database, it doesn't contain the data. So if you want to persist the data, a DataSet or a DataTable would be what you would use.
---
Year happy = new Year(2007);
|
|
|
|
|
Datareader is always connected, better go for datatable to persist stuff!
|
|
|
|
|
be more explicit, and tell my if you need still this answer yet
regards
|
|
|
|
|
I found issue in .NET 2 where MenuStrip is not removed from MDI child window.
How to fix this ?
Andrus.
To reproduce:
1. Run the code
2. Select File / open
Observed: child window contains MenuStrip with File menu item.
Expected: child window should not contain MenuStrip
Code to reproduce the issue:
---- program.cs:
using System.Windows.Forms;
using WindowsApplication1;
static class Program {
static void Main() {
Application.Run(new Form1());
}
}
--- form1.cs
using System.Windows.Forms;
using System;
namespace WindowsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e) {
Form2 newMDIChild = new Form2();
newMDIChild.MdiParent = this;
newMDIChild.Show();
}
}
}
----- form1.designer.cs
namespace WindowsApplication1 {
partial class Form1 {
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent() {
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(292, 24);
this.menuStrip1.TabIndex = 1;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.openToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
this.fileToolStripMenuItem.Text = "&File";
//
// openToolStripMenuItem
//
this.openToolStripMenuItem.Name = "openToolStripMenuItem";
this.openToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.openToolStripMenuItem.Text = "&open";
this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.menuStrip1);
this.IsMdiContainer = true;
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
}
}
-------- form2.designer.cs
namespace WindowsApplication1 {
partial class Form2 {
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent() {
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(292, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.saveToolStripMenuItem});
this.fileToolStripMenuItem.MergeAction = System.Windows.Forms.MergeAction.MatchOnly;
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
this.fileToolStripMenuItem.Text = "&File";
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.MergeAction = System.Windows.Forms.MergeAction.Insert;
this.saveToolStripMenuItem.MergeIndex = 3;
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.saveToolStripMenuItem.Text = "Save";
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form2";
this.Text = "Form2";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
}
}
-------- form2.cs
using System.Windows.Forms;
namespace WindowsApplication1 {
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
}
}
}
Andrus
|
|
|
|
|
If so, is it bad form to do this? The idea is that I have one process running in the background and my webpage being updated by what's stored in the session variable.
I know there's got to be a best practise way to do this. I just can't find much on it. My idea was to use what's stored in the session variable, refesh the page every 5 seconds and poll the session state. Any one?
|
|
|
|
|
Why don't you use AJAX to send a message back to your page when the thread is done, instead of updating every 5 seconds ?
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Well, I want to update more than just when the threead is done.
|
|
|
|
|
I'm not sure what you mean ? Once you know the thread is done, you can send the data through AJAX, or prompt the page to refresh itself, now that there's a reason to.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|