Converting a list<int> into a list<float>? c# [closed] - 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 6 years ago.
Improve this question
I'm getting the error:
Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)
on multiple lines(20+), I know what this error means so I changed all of my int declarations to float but this didn't change where the error was appearing so I had a look at the lines and noticed it was to do with a list declaration I had in a different class.
the error appears on lines like this one:
graph = new Node[widthX, heightY];
It says the error is on widthX and heightY but both are declared as floats, graph however is declared as:
Node[,] graph;
Which is a different class and inside it is a list which I think, is where the root of this error is actually coming from. The code of that class looks like this:
public List<Node> edges;
public Node(){
edges = new List<Node> ();
}
After reading up on C# I saw a lot of converting from string to int or int to string or double to int etc, but nothing on floats.
Or I'm reading this error incorrectly in which case I have no idea where its coming from.

You can't use widthX and heightY in your array declarations, if they are not integers. If that makes sense, you have to cast them to int.
var graph = new Node[(int)widthX, (int)heightY];

Related

Clearing a static list in 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 3 years ago.
Improve this question
I'm trying to clear a static list from my "playerStats" script which I use to access static variables globally. Adding elements to the list works just fine (with playerStats.myList.Add(levelNumber)), but when I try to use myList.Clear I get this error:
"Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement".
Here is the relevant code: First the list definition inside the playerStats class:
{
public static List<int> myList = new List<int>();
}
And this is then on another script referencing the playerStats:
public void goBack()
{
playerStats.myList.Clear;
SceneManager.LoadScene(0);
}
Why can't I clear the list like this?
This error essentially says that you are taking a reference to a method, but you are not assigning it to anything that can reference it. The compiler doesn't know you are actually trying to call this method.
You need to add parentheses, as in
playerStats.myList.Clear();
to call the method.

Why does my code not realise there is a } to close the constructor off [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 4 years ago.
Improve this question
I have been using Visual Studio Code for my C# code; however, it does not seem to realise that I have got a curly bracket '}' at the end of the constructor for my 'opponent' class. Please help me find a way to make it realise that I have done it correctly as far as I can tell!
Here is the class:
public class opponent
{
public int hp = 20;
public int PDmg = 3;
public int KDmg = 3;
public void Opponent(int lv = 1)
{ # red line error marker here
public int level = lv;
}
}
And here is the error message:
} expected [miscellaneousFiles.csproj]
Please help!
I have tried changing the name and capitalisation of the method name but it has not worked so the case-sensitivity of C# is not the problem. I have also tried removing the word 'void' and it did not work either.
double click on the error and it will take you to the place you have missed the brace maybe its not this code where you have got the error
From this line
public int level = lv;
remove public and its gonna work
C# is case-sensitive language, so Opponent and opponent are not the same ;)

C# 2D Generic List [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 7 years ago.
Improve this question
I'm working on a talents/skills tree system for my game made with Unity and I've got a class for my 45 talents buttons that looks like this:
public class SaveTalentClass {
public int talId;
public int talCurRank;
public int talMaxRank;
public SaveTalentClass(int id, int cRank, int mRank)
{
talId = id;
talCurRank = cRank;
talMaxRank = mRank;
}
}
I created 10 "Talent Lists" so the player can save different talents and I stored these 10 Lists in another List for easier access. So I've created a 2D list like that:
public List<List<SaveTalentClass>> containerList = new List<List<SaveTalentClass>>();
And added the 10 "talent Lists" into it but now I'm stuck trying to access/write in this 2D List.
I've tried a test like:
containerList[0][0].Add (new SaveTalentClass(0,1,2));
but got an error: SaveTalentClass' does not contain a definition forAdd' and no extension method Add' of typeSaveTalentClass' could be found (are you missing a using directive or an assembly reference?)
I'm pretty sure there's an easy fix for that but I couldn't figure out how to do it !
Thanks for helping me :)
Giving two indexes, you're already inside the second list. Either, if you want to add a new element to one of the lists inside the outer list, use
containerList[0].Add (new SaveTalentClass(0,1,2));
Or if you want to modify an existing one:
containerList[0][0] = new SaveTalentClass(0,1,2);
Since you have the containerList, you can add an object of the type on it, in that case is a List of Lists. So, add a new list of SaveTalentClass and after you can access it by 0 index to add an object of SaveTalentClass object, for sample:
// add the first list in the containerList
containerList.Add(new List<SaveTalentClass>());
// add an item in the first list of containerList
containerList[0].Add(new SaveTalentClass(0,1,2));

How can I change the tick frequency in trackbar to a decimal? [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 8 years ago.
Improve this question
I am trying to set the trackbar's tick frequency to 0.015625.
private void trackBar1_Scroll(object sender, EventArgs e) {
ScrollBar.TickFrequency = 0.015625;
}
I am getting an error that says:
Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (Are you missing a cast?)
And:
Cannot convert method group 'ToDouble' to non-delegate type 'int'. Did you intend to invoke the method?
Can someone explain that means or what I'm doing wrong?
Edit: I found the answer to my question.
Demo.Property = (ScrollBar.Value * (Rate));
In my case I used:
Demo.Inches = (ScrollBar.Value * 0.015625);
It was designed as an int, just like the Min, Max and Value properties.
You will have to calculate your own scaling here. By setting Max to a multiple of 64 for instance.

Why is semicolon expected and what does it mean that implicitly-typed local variables must be initialized? [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 8 years ago.
Improve this question
I get two errors and I have no clue where the error is. Can anyone please help me resolve this issue?
; expected
Error Implicitly-typed local variables must be
initialized ComputerPlayer.cs 103
Tried searching for ";", but I don't see nothing in the code that is missing.
public override bool CheckForHit(Cell guessedCell) // Line 101
{ // Line 102
var wasHit base.CheckForHit(guessedCell); // Line 103
} // Line 104
Both errors are related. Watch closely the line 103:
var wasHit base.CheckForHit(guessedCell);
What was your intention? I imagine you were assigning the result of CheckForHit to wasHit, but there is no = sign anywhere.
Also, as noted by Mathew, there is no return in this method, so it won't compile even with = added to it.
You may rewrite it as:
public override bool CheckForHit(Cell guessedCell)
{
var wasHit = base.CheckForHit(guessedCell);
// Additional code goes here.
return wasHit;
}
If there is no additional code, consider simply removing the method, since all it does is to call the base method it overrides.

Categories