|
Computafreak wrote: You just drop though each case. Why?
This is a state-machine with a selectable first state.
If C# had a "computed goto" (as Fortran has) then the switch would not be necessary.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Luc Pattyn wrote: If C# had a "computed goto"
I'd have stuck with C++.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Luc Pattyn wrote: state-machine
I prefer to put the switch in a while , rather than use fall-through for that.
|
|
|
|
|
Luc Pattyn wrote: This is a state-machine with a selectable first state.
If C# had a "computed goto" (as Fortran has) then the switch would not be necessary.
Or do the state machine as a "while...switch..." loop, and set the initial state before entry. That's how I've always done it (and I do go back to the old Fortran days as well ).
There are three kinds of people in the world - those who can count and those who can't...
|
|
|
|
|
I wrote this code to test:
int i=int.Parse(Console.ReadLine());
switch(i)
{
case 0:
Console.WriteLine(0);
goto case 1;
case 1:
Console.WriteLine(1);
break;
}
This works. If you don't do the goto case 1 you will receive an error, and if you don't do the break at case 1 you will also receive an error.
I think the problem with your code is the lack of some "goto case x" or some "break".
--- I don't like such gotos, but if that the best way, try finding the lacking gotos / breaks.
modified on Wednesday, May 13, 2009 10:06 AM
|
|
|
|
|
But remember that the goto in this case is fixed. If you need something like:
switch(x)
{
case 0:
if (someCondition)
x += 5;
else
x += 6
do other processing, and then continue with the next case for X (that can be 5, 6 or some other value) the best solution will be to use a while.
For example:
bool continueRunning = true;
while (continueRunning)
{
switch(x)
{
case 0:
// do something;
if (someCondition)
x += 5;
else
x += 6;
break;
... other cases ...
default:
continueRunning = false;
break;
}
}
So, this will:
Execute the switch with x being zero.
You can then recalculate x, and it will execute the switch again.
You can do that how many times you want. If you want to stop it, you call continueRunning = false;
And, if the value does not fall in any case, it will enter the default, with will set continueRunning to false and stops the block.
modified on Wednesday, May 13, 2009 10:07 AM
|
|
|
|
|
Paulo Zemek wrote: This works.
Yes. So does this:
Console.WriteLine(0);
Console.WriteLine(1);
Same outcome, more readable though.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
This does not generate the same result.
I set the i = 0. But consider that i is entered by the user.
Console.WriteLine(0);
Console.WriteLine(1); will not be the same, as in some cases only the case 1 must be executed. As I understand, the idea is to have the option to "start at any point", but continue from it. And I am not saying the solution used is a good one, but it is the easiest to convert a C++ code without really refactoring it.
|
|
|
|
|
Yeah. And then some poor sod (probably me with any luck) gets to maintain the ramshackle heap of spagetti.
Do the job right. If you are converting C++ to C# then convert it to good C#. It may be you who has to "upgrade" it in six months time...
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
|
Hi All,
How can we make the dynamic property in windows application in C#,that can pick the list from the database?
Please help,its urgent....
Thanks,
Lalit Narayan
|
|
|
|
|
lnmca wrote: its urgent
Checked the urgent section on my ToDo list and this wasn't on there so I'm afraid you're wrong.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Make sure you call update() on your todo list before checking new items
|
|
|
|
|
I find this works best
public Message UpdateToDoList()
{
toDoList.Nuke();
return new Message("Nothing to do today, lounge around on CP instead");
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
In C# one can use the Process.Start() method to open a file using the default program set for the filetype in the registry. I would like to use the "edit" property in the registry (possibly with fallback on "open"). This can be achieved by setting the Verb-property in the ProcessStartInfo object.
When the user has not set the application to use when editing a certain file I would like to give him/her the opportunity to choose. I have found that this is done by setting the Verb property to "openas". This will give the user the standard windows dialog to pick an application from a list or browse for it in the filesystem. This leads us to my question.
How can I do the same thing for the edit property in the registry? ("editas" doesn't seem to cut it at all)
|
|
|
|
|
hello
i want to know how to backup a database from a distant server using c# and SqlServer2000.
NB:i just succeeded in doing this with a local server.
and here is the code:
[using System;
namespace test_backup
{ public partial class Form1 : Form
{ private static Server srvSql;
public Form1()
{ InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Create datatable where we enumerate the available servers
DataTable dtServers = SmoApplication.EnumAvailableSqlServers(true);
// If there are any servers at all
if (dtServers.Rows.Count > 0)
{
// Loop through each server in the DataTable
foreach (DataRow drServer in dtServers.Rows)
{
// Add the name to the combobox
cmbServer.Items.Add(drServer["Name"]);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
// If a server was selected at all from the combobox
if (cmbServer.SelectedItem != null && cmbServer.SelectedItem.ToString() != "")
{
// Create a new connection to the selected server name
ServerConnection srvConn = new ServerConnection(cmbServer.SelectedItem.ToString());
// Log in using SQL authentication instead of Windows authentication
// srvConn.LoginSecure = false;
// Give the login username
//srvConn.Login ="."; //txtUsername.Text;
// Give the login password
// srvConn.Password =""; //txtPassword.Text;
// Create a new SQL Server object using the connection we created
srvSql = new Server(srvConn);
// Loop through the databases list
foreach (Database dbServer in srvSql.Databases)
{
// Add database to combobox
cmbDatabase.Items.Add(dbServer.Name);
}
}
else
{
// A server was not selected, show an error message
MessageBox.Show("Please select a server first", "Server Not Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void button2_Click(object sender, EventArgs e)
{
// If there was a SQL connection created
if (srvSql != null)
{
// If the user has chosen a path where to save the backup file
// if (saveBackupDialog.ShowDialog() == DialogResult.OK)
// {
// Create a new backup operation
Backup bkpDatabase = new Backup();
// Set the backup type to a database backup
bkpDatabase.Action = BackupActionType.Database;
// Set the database that we want to perform a backup on
bkpDatabase.Database = cmbDatabase.SelectedItem.ToString();
// Set the backup device to a file
BackupDeviceItem bkpDevice = new BackupDeviceItem("d:\\sana22", DeviceType.File);
// Add the backup device to the backup
bkpDatabase.Devices.Add(bkpDevice);
// Perform the backup
bkpDatabase.SqlBackup(srvSql);
//}
}
else
{
// There was no connection established; probably the Connect button was not clicked
MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void button3_Click(object sender, EventArgs e)
{
// If there was a SQL connection created
if (srvSql != null)
{
// If the user has chosen the file from which he wants the database to be restored
// if (openBackupDialog.ShowDialog() == DialogResult.OK)
// {
// Create a new database restore operation
Restore rstDatabase = new Restore();
// Set the restore type to a database restore
rstDatabase.Action = RestoreActionType.Database;
// Set the database that we want to perform the restore on
rstDatabase.Database = cmbDatabase.SelectedItem.ToString();
// Set the backup device from which we want to restore, to a file
BackupDeviceItem bkpDevice = new BackupDeviceItem("d:\\sana22", DeviceType.File);
// Add the backup device to the restore type
rstDatabase.Devices.Add(bkpDevice);
// If the database already exists, replace it
rstDatabase.ReplaceDatabase = true;
// Perform the restore
rstDatabase.SqlRestore(srvSql);
// }
}
else
{
// There was no connection established; probably the Connect button was not clicked
MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}]
|
|
|
|
|
Hi All,
Is it possible to make the dynamically enum,means it should be created based on the values picked from DB?
Thanks,
Lalit
|
|
|
|
|
Enums are compile time constants and can't be modified/created at run time.
|
|
|
|
|
Appartently they can be created at run time, but they'd be fairly useless as far as I can tell.
|
|
|
|
|
Try using T4
Look here[^] to get you started.
|
|
|
|
|
Not in a way that it would be any useful.
I think that you should look into how you could use a Dictionary instead.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
You can always compile an unit using CodeDOM, but I doubt it will be any useful, as it will not have any hard-coded references to it.
I also suggest using dictionaries to do the job.
|
|
|
|
|
Hi friends,
I have a small project, of which, I do not want to create any setup. Now, I have copied the project .exe file (standalone application) from Debug-Bin-project.exe and am trying to execute it on another machine which has .NET framework 2.0 and 3.5 installed. But surprisingly, I get an error message something like this: The application failed to initialize properly (000000000000..//error code..) Click Ok to terminate the application. . Can anyone explain me the reason for this. Does this mean, that what I did ain't the appropriate way of creating or running a standalone executable application? If yes, then please explain me a better way.
Your help will be truly appreciated,
Rajdeep.NET
|
|
|
|
|
Rajdeep.NET wrote: Now, I have copied the project .exe file (standalone application) from Debug-Bin-project.exe and am trying to execute it on another machine
Make sure you copy all dependent files as well, like configuration files and other assembly(not the .NET framework assemblies) references.
Rajdeep.NET wrote: The application failed to initialize properly (000000000000..//error code..) Click Ok to terminate the application.
Look in the event viewer for a detailed error message.
|
|
|
|
|
N a v a n e e t h wrote: Make sure you copy all dependent files as well, like configuration files and other assembly(not the .NET framework assemblies) references.
Hi Navaneeth,
Thanks for your help yaar, but I want to extract only the .exe file. And moreover, my clients wont be having Visual Studio.NET installed on their system. So, copying the configuration files and assemblies wont help. I've seen many applications which are standalone with just an .exe and no setup is required. Just downloading'em and running the .exe does the work. Ain't it possible in this case?
Help needed,
Rajdeep.NET
|
|
|
|