The online racing simulator
C# Variable Help
(22 posts, started )
C# Variable Help
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
Quote from dougie-lampkin :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...

uh just put the preassignment right in front of the loop ?

some code sniplet to explain your problem would help
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.

Just set the variable outside of the loop

int myVar = 0;

while(...)
{
switch(myVar)
{
case 0:
...
break;

case 1:
...
break;
}
}

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...

Hope that helps a bit...
please post come code snipet, it's really hard to say what's wrong without it
#7 - Woz
Quote from dougie-lampkin :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...

Hope that helps a bit...

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
Quote from dougie-lampkin :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...

Hope that helps a bit...

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...



[SIZE=2][COLOR=#0000ff]switch[/COLOR][/SIZE][SIZE=2] (property)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff] case[/COLOR][/SIZE][SIZE=2] 1:[/SIZE]
[SIZE=2] InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#800000]"Called 1"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
property += 1;
[SIZE=2][COLOR=#0000ff] break[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff] case[/COLOR][/SIZE][SIZE=2] 2:[/SIZE]
[SIZE=2] InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#800000]"Called 2"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
property += 1;
[SIZE=2][COLOR=#0000ff] break[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff] case[/COLOR][/SIZE][SIZE=2] 3:[/SIZE]
[SIZE=2] InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#800000]"Called 3"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
property += 1;
[SIZE=2][COLOR=#0000ff] break[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff] default[/COLOR][/SIZE][SIZE=2]:[/SIZE]
[SIZE=2] InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#800000]"I dont know what to call"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#0000ff] break[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]}[/SIZE]

If thats what you mean..then after you have done what you need (as its in the connections list) you can go and reset it!


P.S if thats wrong im noob
Quote from Woz :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

Agreed. Sounds a bit like a scope issue though
Quote from mcgas001 :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:

[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] ChaseCondition=0;
[/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] EngageCommand, SuspectUName, SuspectName;
[/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (MSO.Msg.Contains([/SIZE][SIZE=2][COLOR=#a31515]"!resetchase"[/COLOR][/SIZE][SIZE=2]))
{
ChaseCondition = 0;
}
[/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (MSO.Msg.Contains([/SIZE][SIZE=2][COLOR=#a31515]"!engage"[/COLOR][/SIZE][SIZE=2]))
{
[/SIZE][SIZE=2][COLOR=#0000ff]switch[/COLOR][/SIZE][SIZE=2] (ChaseCondition)
{
[/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 0:
EngageCommand = [/SIZE][SIZE=2][COLOR=#2b91af]Convert[/COLOR][/SIZE][SIZE=2].ToString(MSO.Msg);
SuspectUName = EngageCommand.Substring(35);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg "[/COLOR][/SIZE][SIZE=2] + (Connections[GetConnIdx(MSO.UCID)].PlayerName) + [/SIZE][SIZE=2][COLOR=#a31515]"^7 engages "[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]"!"[/COLOR][/SIZE][SIZE=2]);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^1 CONDITION 1"[/COLOR][/SIZE][SIZE=2]);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^1 PULL OVER NOW!"[/COLOR][/SIZE][SIZE=2]);
ChaseCondition = (1); [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 1:
EngageCommand = [/SIZE][SIZE=2][COLOR=#2b91af]Convert[/COLOR][/SIZE][SIZE=2].ToString(MSO.Msg);
SuspectUName = EngageCommand.Substring(35);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^1 CONDITION 2"[/COLOR][/SIZE][SIZE=2]);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^1 LIGHT FORCE AUTHORISED!"[/COLOR][/SIZE][SIZE=2]);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^1 THIS IS YOUR LAST WARNING!"[/COLOR][/SIZE][SIZE=2]);
ChaseCondition = (2); [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 2:
EngageCommand = [/SIZE][SIZE=2][COLOR=#2b91af]Convert[/COLOR][/SIZE][SIZE=2].ToString(MSO.Msg);
SuspectUName = EngageCommand.Substring(35);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^1 CONDITION 3"[/COLOR][/SIZE][SIZE=2]);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^1 HEAVY FORCE AUTHORISED!"[/COLOR][/SIZE][SIZE=2]);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^1 ALL AVAILABLE UNITS JOIN CHASE!"[/COLOR][/SIZE][SIZE=2]);
ChaseCondition = (3); [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 3:
EngageCommand = [/SIZE][SIZE=2][COLOR=#2b91af]Convert[/COLOR][/SIZE][SIZE=2].ToString(MSO.Msg);
SuspectUName = EngageCommand.Substring(35);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^1 CONDITION 4"[/COLOR][/SIZE][SIZE=2]);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^1 ALL FORCE AUTHORISED!"[/COLOR][/SIZE][SIZE=2]);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^1 USE ANY MEANS NECESSARY TO TAKE DOWN SUSPECT!"[/COLOR][/SIZE][SIZE=2]);
ChaseCondition = (4); [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 4:
EngageCommand = [/SIZE][SIZE=2][COLOR=#2b91af]Convert[/COLOR][/SIZE][SIZE=2].ToString(MSO.Msg);
SuspectUName = EngageCommand.Substring(35);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^1 CONDITION 5"[/COLOR][/SIZE][SIZE=2]);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^1 SUSPECT MUST BE NEUTRALISED^6 IMMEDIATLY!!!"[/COLOR][/SIZE][SIZE=2]);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^1 CIVILIANS WATCH OUT!"[/COLOR][/SIZE][SIZE=2]);
ChaseCondition = (5); [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 5:
EngageCommand = [/SIZE][SIZE=2][COLOR=#2b91af]Convert[/COLOR][/SIZE][SIZE=2].ToString(MSO.Msg);
SuspectUName = EngageCommand.Substring(35);
InSim.Send_MST_Message([/SIZE][SIZE=2][COLOR=#a31515]"/msg ^7"[/COLOR][/SIZE][SIZE=2] + SuspectUName + [/SIZE][SIZE=2][COLOR=#a31515]" ^6-->^7 SUSPECT LOST"[/COLOR][/SIZE][SIZE=2]);
ChaseCondition = 0; [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000ff]default[/COLOR][/SIZE][SIZE=2]:
ChaseCondition = 0; [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];


}
}

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?

Any ideas on this?





[/SIZE]
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...


int ChaseCondition = 0;

void YourMethod()
{
string EngageCommand, SuspectUName, SuspectName;

if (MSO.Msg.Contains("!resetchase"))
{
ChaseCondition = 0;
}

if (MSO.Msg.Contains("!engage"))
{
switch (ChaseCondition)
{
case 0:
EngageCommand = Convert.ToString(MSO.Msg);
SuspectUName = EngageCommand.Substring(35);
InSim.Send_MST_Message("/msg " + (Connections[GetConnIdx(MSO.UCID)].PlayerName) + "^7 engages " + SuspectUName + "!");
InSim.Send_MST_Message("/msg ^7" + SuspectUName + " ^6-->^1 CONDITION 1");
InSim.Send_MST_Message("/msg ^7" + SuspectUName + " ^6-->^1 PULL OVER NOW!");
ChaseCondition = (1);
break;

// Snip....

default:
ChaseCondition = 0;
break;
}
}
}

That should do it...
Quote from DarkTimes :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.

They are "private void" by default, will it still work as void? (Can't check right now, will have time in about an hour...)
Quote from dougie-lampkin :They are "private void" by default, will it still work as void? (Can't check right now, will have time in about an hour...)

Yes, it will still work, 'void' is the return type for the method, so it does not effect what variables the method has access to.
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.
Quote from AndroidXP :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.

Yea class is probably the wrong term

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
Quote from dougie-lampkin :Thank you all, I've tried it without the "private" and it works!

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.

Quote :
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...

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.

http://www.google.co.uk/search ... amp;btnG=Search&meta=
Quote from DarkTimes :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.

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.

Quote from DarkTimes :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.

Yes I am confused at them, I'm relatively new to c#
Thanks for the link!
A variable can only be defined inside of its scope. Like when you make a "private void MSO..." you do it like this



private void Dosomething()
{
Something can be definied here...
}

Basically something can be used inside of Dosomething but cant be used out of it..


private void Dosomething()
{
int MyInt = 0;
MessageBox.Show(MyInt.ToString());
}

Would work..



int MyInt = 0;
private void Dosomething()
{
MessageBox.Show(MyInt.ToString());
}

would work..


private void Dosomething()
{
int MyInt = 0;
}

MessageBox.Show(MyInt.ToString());

Would fail...as its outside of the scope it was definied in!

i think thats right anyway
Ah! Well, I'm confused now as to why it didn't work before with "private void"...

Maybe I had something else wrong, I don't know. But hey, it works now! Thank you all for you help
Quote from dougie-lampkin :Ah! Well, I'm confused now as to why it didn't work before with "private void"...

you should _really_ look up what void means
#22 - Woz
Quote from dougie-lampkin :Ah! Well, I'm confused now as to why it didn't work before with "private void"...

Maybe I had something else wrong, I don't know. But hey, it works now! Thank you all for you help

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
{
}
}

Hope that helps

C# Variable Help
(22 posts, started )
FGED GREDG RDFGDR GSFDG