Hello!
I've got a stupid error, which makes no sense. Have a look at this code from my plugin please:
It seems to be fine, but the compiler throws an error:Code:MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); //... MySqlCommand cmd; cmd = connection.CreateCommand(); cmd.CommandText = command; cmd.ExecuteNonQuery();
This snippet is the only place in code where the 'cmd' name occurs. The code had been tested in a normal Console C# project before, and it worked perfectly. However, it causes an error in Oxide.Code:A local variable `cmd' cannot be used before it is declared
Could someone explain it to me?
Solved A local variable can't be used before it's declared (missing reference)
Discussion in 'Hurtworld Development' started by gogolon, Jul 1, 2018.
-
Wulf Community Admin
You have initialized cmd with anything. The fix would be:
MySqlCommand cmd = connection.CreateCommand(); -
It's already initialized. Have a look on the snippet again please.
-
Wulf Community Admin
Try the code, there's no need to declare it below the uninitialized variable, just combine the lines. -
I tried it, but it doesn't fix the error :/
-
Wulf Community Admin
-
The full error is:
Code:Error while compiling: DataUploader.cs(42,17): error CS0841: A local variable `cmd' cannot be used before it is declared
Code:MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); //... MySqlCommand cmd = connection.CreateCommand(); cmd.CommandText = command; cmd.ExecuteNonQuery();
-
Wulf Community Admin
-
I'm developing using Visual Studio Community 2017. It doesn't seem to be a missing reference, because it would throw a missing reference exception. It's quite annoying since it stops me from developing any plugins. Could you try to compile the plugin to recreate the error? Or at least try to find out what the problem is by having a look at the whole code? It's pretty short: ybin | private paste
Thanks in advance. -
Okay, the thread can be closed. I solved it somehow, but it required changing the source of the Oxide.CSharp. Thanks for replies.
-
Wulf Community Admin
-
I added a reference to MySqlData dll in the CSharp source, compiled it, then added using MySqlData in the plugin and it worked. Not sure if it wouldn't work without adding the reference in source though.
-
Wulf Community Admin
At the top of your plugin:
// Reference: DLLName -
Thanks, I didn't know about that. The Oxide's MySQL library has no documentation, has it? I couldn't find it, so I decided to use standard library instead.
-
Wulf Community Admin