Autodesk guide project two "a namespace cannot directly contain members from" [duplicate] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to use this code for NET.reflector using Reflexil. I am trying to replace code with this:
if(Input.GetKeyDown(KeyCode.Keypad5)) {
int i = 0;
Character localPlayer = PlayerClient.GetLocalPlayer().controllable.GetComponent<Character>();
foreach (UnityEngine.Object obj2 in UnityEngine.Object.FindObjectsOfType(typeof(LootableObject)))
{
if (obj2 != null)
{
i++;
LootableObject loot = (LootableObject) obj2;
Debug.Log("Loot "+i+": "+loot.transform.position.ToString());
CCMotor ccmotor = localPlayer.ccmotor;
if(ccmotor != null && tpPos1 != Vector3.zero) {
ccmotor.Teleport(loot.transform.position);
Notice.Popup("", "Teleported to "+loot.name, 1.5f);
}
break;
}
}
}
But it gives me an error when I try to compile:
Line: 1 Column: 1 Error Number: CS0116 Error Message: "A namespace does not directly contain members such as fields or methods"
This is Unity code I think. I am not that experienced. Could anyone fix this for me? Or tell me what to do? Thanks!

The snippet you're showing doesn't seem to be directly responsible for the error.
This is how you can CAUSE the error:
namespace MyNameSpace
{
int i; <-- THIS NEEDS TO BE INSIDE THE CLASS
class MyClass
{
...
}
}
If you don't immediately see what is "outside" the class, this may be due to misplaced or extra closing bracket(s) }.

Related

C# SQL "Index was outside the bounds of the array." [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I've been learning how to create a SQL base with C# and I'm stuck here with a question.
Here I am testing how to return the value of a text row to a string
Why does the compiler make an error here? I read the method list and my database has 3 more values for the 'FirstName' column. Why can I only see an index?
I appreciate your help in advance
{
static void Main(string[] args)
{
string dbName = "URI=file:testeDB.db";
using (var connection = new SqliteConnection(dbName))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "SELECT FirstName FROM Persons";
SqliteDataReader reader = command.ExecuteReader();
Console.WriteLine(reader);
if ( reader.Read() )
{
Console.WriteLine("Reading...");
Thread.Sleep(1000);
string myVar = reader.GetString(2); //Index was outside the bounds of the array."
Console.WriteLine(myVar);
}
else
{
Console.WriteLine("Dont Read");
}
}
}
}
}
This happens because the method GetString(int) as the documentation says "Gets the value of the specified column as a String." and according to the code you showed us you are just retrieving one column from the table Persons, that's why you are getting the exception, only GetString(0) will work in this case.

Using ternary operations to make my code cleaner [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have been doing some C# practice problems for a while and I want to start playing with ternary operations to make my code cleaner.
Here is my code:
public static string Bomb(string txt)
{
txt.ToLower().Contains("bomb") == true ? "Duck!!!" : "There is no bomb, relax.";
}
So basically if Bomb("xxxxxx") contains the string "bomb" it will return "Duck!!!" if not it will return "There is no bomb, relax."
But for some reason, this doesn't work and I can't figure out why.
You just need to add return
public static string Bomb(string txt)
{
return txt.ToLower().Contains("bomb") == true ? "Duck!!!" : "There is no bomb, relax.";
}
Your ternary operator looks OK, but you're missing a return statement from the function. Also note that Contains returns a boolean, so the == true is redundant:
So I have been doing some c# practice problems for a while and I want to start playing with ternary operations to make my code cleaner.
Here is my code:
public static string Bomb(string txt)
{
return txt.ToLower().Contains("bomb") ? "Duck!!!" : "There is no bomb, relax.";
}
you missed the return part in the function
public static string Bomb(string txt)
{
return txt.ToLower().Contains("bomb") ? "Duck!!!" : "There is no bomb, relax.";
}

How do I solve this problem with my code? C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've tried adding a ) where it says to, but that adds more problems. The error I get states:
Assets/LevelComplete.cs(12,64): error CS1026: ) expected.
What I'm trying to do here is use the if statement to find out if the Active Scene is in the specific range. Also, for a little more context, All I need is a range, and not the code to pick a number in that range if that helps.
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LEvelComplete : MonoBehavior
{
public void LoadNextLevel()
{
int Active12 = Range(1, 3);
int Load34 = UnityEngine.Random.Range(3, 5);
if (SceneManager.GetActiveScene().buildIndex = Active12
{
SceneManager.LoadScene(Load34);
}
}
}
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using Random = UnityEngine.Random;
public class question_63947858_script_error : MonoBehaviour
{
public void LoadNextLevel()
{
int Active12 = Random.Range(1, 3);
int Load34 = Random.Range(3, 5);
if (SceneManager.GetActiveScene().buildIndex == Active12)
{
SceneManager.LoadScene(Load34);
}
}
}
Things wrong:
MonoBehaviour was spelled wrong
System was clashing with UnityEngine namespace. So disambiguated with Random = UnityEngine.Random
No parentheses after the if statement
= changed to == in the if statement
"find out if the Active Scene is in the specific range."
...
var scene = SceneManager.GetActiveScene();
if (scene.buildIndex >= 1 && scene.buildIndex <= 3)
... or if you prefer Linq:
if (Active12.Contains(SceneManager.GetActiveScene().buildIndex))
See also How to elegantly check if a number is within a range?

using strings inside if statement throw error "Operator '||' cannot be applied to operands string and string" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi my c# is not what it used to be and have just come back after using java script for a while.
Essentially I am just trying to do a simple if statement using index of arrays but I receive the error message.
"Operator '||' cannot be applied to operands string and string"
How come this is not allowed as it essentially becoming a bool.
string[] userCustomAnswerArray = {"It needs to be reaplaced", "This could be improved", "I struggle to see this"};
int customResponseindex = rand.Next(0, 3);
string[] questionResponseArray = { "Yes", "No but not a problem", userCustomAnswerArray[customResponseindex] };
int questionResponseIndex = rand.Next(0, 3);
string userAnswer = questionResponseArray[questionResponseIndex];
if (userAnswer = questionResponseArray[0] || userAnswer = questionResponseArray[1])
{
}
Thanks for your help !!!!
userAnswer = questionResponseArray[0] is incorrect.
= is the assignment operator while == is the equality operator

Code unreachable when comparing types [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Question: My else-statement is unreachable, what am i doing wrong?
very VERY new at programming and i'm trying to compare the type so for example people can't enter strings when i'm asking for integers.
My code is probably pretty bad, if i could get a header what to do and why the if-argument skips the else-part i'd be really happy!
Thanks!
class Program
{
static void Main(string[] args)
{
int integer = 0;
start:
Console.WriteLine("How old are you?: ");
int svar = int.Parse(Console.ReadLine());
Utility.CompareTypes(svar, integer);
if (true)
{
Console.WriteLine("Thanks");
}
else
{
Console.WriteLine("You have to enter a number!");
goto start;
}
}
}
class Utility
{
public static bool CompareTypes<T01, T02>(T01 type01, T02 type02)
{
return typeof(T01).Equals (typeof(T02));
}
}
:c
It's not really a question of code, but of logic...
if (true) // <--- this will ALWAYS be true
{
Console.WriteLine("Thanks");
}
else // <--- therefore this will NEVER happen
{
Console.WriteLine("You have to enter a number!");
goto start;
}
Since your else block can never possibly execute under any logical circumstance, the entire block of code can be simplified to:
Console.WriteLine("Thanks");
In order for the else block to execute, the condition checked in the if statement needs to be false. You're currently not checking any actual condition, just a hard-coded true value.
Perhaps you meant to use the result of the previous line of code? Something like this:
var typesAreSame = Utility.CompareTypes(svar, integer);
if (typesAreSame)
{
//...

Categories