Yep, I'm stuck in this godforsaken language again...I hate you Microsoft!
Anyway, I'm trying to make a little app, with a switch statement. I've declared the switch variable, but cannot assign it a integer yet, as the results of the switch statement determine the variable...and then it's all in a loop...
Obviously this brings the error "Use of unassigned local variable". But, if I assign it a integer, when the loop comes around again, the varaible is reset to that integer...and I can't move the start position of the loop, because it's at the very start of the program...
Basically, is there any way of assigning the variable a value that will be used as default and ignored thereafter? I've tryed using try/catch but it's too fiddly for me and I can't seem to use it...
Any help would be brilliant, I can't find help elsewhere...
Thanks for reading anyway
It's hard to understand your question without seeing any code, but it sounds like maybe you could use a do-while loop instead of a normal one...
do { // Your code, presumably the switch statement. } while(<condition>);
With a do-while the loop runs a single iteration before the condition is tested, so your switch statement can set the value of the variable before it gets tested. As I say it's hard to understand your question really, although maybe I'm being slightly dense.
Its the Insim C# Base, which sorta loops by default, with no actual loop (or similar) command (it could be the classes looping, but I'm new to c# :nod...
The app is just something I'm workin on as a test, to see what Insim (and c# for that matter) can and can't do...
I have the switch in the IS_MSO, to catch a command out. If this is the first time the command has been said by a player, this is case 0 (This is why I'm trying to have the variable 0 by default), and then at the end of case 0, the variable is incremented by 1, so that next time the command is said, case 1 is used, and so on...
I've tried int variable=0; but that seems to do the same thing as just declaring it seperatly. After 5 cases, the variable is reset to 0...
You really need to post the code. From experience a written description is normally what the developer "thinks" is happening and not what is actually going on
What i can guess your trying to do is some how store how many times someone has said a command right?. If thats the case make a property in the connections list(so its for each user) then increatment it by 1 on the command. Once you have done that you can get the UCID from the MSO packet then grab there property from the connections list and make a switch with the given answers...
That's sorta what I'm trying to do, yes. The code is to replicate the !engage thingy on cruise servers...silly, i know, but I'm tryin it as a little project, and it's harder than i thought
For those that haven't seen one in action, basically the cop says the command !engage (or similar) and the nearest car is "engaged" (a message is sent saying "TS ROB has been engaged - condition 1!"). I have that bit done, no problems. But, when the !engage command is used again, the message is "TS ROB - Condition 2", so the same car player is "engaged" again. As the cop keeps saying !engage, the conditions keep rising, up until 4, then the condition 4 message is repeated.
What I had intented on doing is having a variable "ChaseCondition" set to 0 by default. Each time the cop types !engage, the varaible is inceremented by 1, thus giving the variable the condition number-1. Here's my code:
I used substring as a temporary fix - I.E., the cop has to type the player's name after the !engage bit. It's temporary until I get the condition bit working...but that's not important...
I've made it so that on the "6th condition", the suspect is lost and the condition reset. Again, that's temporary...
The problem is declaring the variable "ChaseCondition". I need it to be set to 0 by default, and then the decleration ignored from then on, if you follow me
With what I have at the moment, the decleration of 0 is used each time the loop comes around...
The whole source file is looped, with no actual "loop" or similar command. Presumably this is because of the classes?
Initialise the ChaseCondition variable outside the scope of that method, so it is only initialised when the class is instantiated, instead of every time the method is called. Like so...
Thank you all, I've tried it without the "private" and it works!
One more thing; if I changed another class to "void", would that allow both classes to read each other's packets? i.e., could the MSO class read info straight from the MCI class, if I used a variable declared outside the classes? Just something that I thought of...
Void (means "nothing") is a return type of a method. You cannot apply void to a class. What you probably want to modify is the variable scope, which is defined with private, public, internal or protected.
When I say class, I mean the different "classes (as i call them )" in InSim such as IS_NCN and IS_MSO. In the c# base, when you want to use one of these classes, you start with "void" or "private void". So I then decided to call these "areas (don't know the proper term...)" classes...
But that's just me...good thing programming's not oral
If you omit the access modifier (EG private, public, protected etc..) then the C# compiler defaults the access to private, so there is actually no difference between saying 'private void YourMethod()' and just 'void YourMethod()'.The only reason to include private is that it can make code easier to read.
You seem to be a little confused as to what classes and methods are and how they work, so I'd really strongly advise reading a tutorial on the subject.
But then how come omitting the modifier increases the variable scope? When it had "private void", it couldn't get variables from outside it. But at just "void", it worked.
Yes I am confused at them, I'm relatively new to c#
Thanks for the link!
I would say the trouble you have at the moment is you do not understand the scope rules of the language.
Hope this helps...
public MyClass { public int a; // Visible outside MyClass private int b; // Only visible in MyClass
// Only visible in MyClass private void DoAction1() { int aa; // Visible only in this method
// Some work }
// Only visible in MyClass and classes that inherit protected bool DoAction2() { bool results = false;
// Some work that sets results
return results; // Must return as not void }
// Visible outside the object public void DoAction3() { // Keeps value between calls but only visible in function static aa; }
// Not a member function so has no access to the class // variables unless it has an object reference to MyClass // and then only access to public members and attributes. public static voidd DoAction4(MyClass aReference) { aReference.a = 5; // Fine aReference.b = 6; // Error as does not have access }
// Class that is only available inside this class private class MyOtherClass { } }