I don't know why I am receiving error CS1061 [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am working on an id tester. I get error CS1061 every second.
This is my code:
Process[]roblox=Process.GetProcessesByName("RobloxPlayerBeta.exe");
Console.WriteLine("Id of RobloxPlayerBeta.exe is:", roblox.Id);
int id = roblox.Id;

Compiler Error CS1061 will give you more details on why you get this error. But in your question you haven't include most important part of error details.
roblox is array, if you need to get id of process then you need to get item in the array. items of array can be access via index
Process[]roblox=Process.GetProcessesByName("RobloxPlayerBeta.exe");
if(roblox!=null && roblox.Length >0)
{
Console.WriteLine("Id of RobloxPlayerBeta.exe is:", roblox[0].Id);
int id = roblox[0].Id;
}

You have an array of processes roblox, but try to access the Id property of the entire thing as opposed to well... a process. In order to fix this you must actually select the index you wish to use the Id of
Process[] roblox = Process.GetProcessesByName("RobloxPlayerBeta.exe");
if(roblox.GetLength(0) > 0) //Check that any processes exist with that name
{
int id = roblox[0].Id; //set ID first as to avoid accessing the array twice
Console.WriteLine("Id of RobloxPlayerBeta.exe is:" + id); //Write the line using the newly found id variable
}
else //If the process doesn't exist
{
Console.WriteLine("RobloxPlayerBeta.exe is not running"); //Output error of sorts
}
Also fixed here is your Console.WriteLine(), the way you're using it would require a parameter like this Console.WriteLine("Id of RobloxPlayerBeta.exe is:{0}", id); or use concatenation like I do in the example. You seem to have attempted a mixture of the two, which doesn't work
If you are having any issues with getting to grips with arrays, have a read of this article
To paraphrase it, regarding the issue you had, once you have an array, for example
string[] stringArray = new string[] {"hello", "hi"};
You can access its contained objects like so
string firstIndex = stringArray[0]; //for the first index
string secondIndex = stringArray[1]; //for the second index
If we were then to write this
Console.WriteLine(firstIndex + secondIndex);
It would output hellohi
As a side note, you are receiving error CS1061 specifically because you are trying to access a property of the array which does not exist
NB: An interesting one-liner, using -1 to indicate the process is not running
int id = Process.GetProcessesByName("RobloxPlayerBeta.exe")?[0].Id ?? -1;

Related

Parameters in parentheses after a method name: what are they and what do they do? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Newb to C# and OOP. My journey thus far has been to take code bases that I've inherited from former developers and either address issues, or add enhancements, whilst trying to understand said code bases' structures from front-to-back.
I'm having trouble fully grasping the concept around the parameters which follow the initial declaration of a method. Here's an example of a method I'm working with:
public List<Entity> ParseCsvFile(List<string> entries, string urlFile)
{
entries.RemoveAt(entries.Count - 1);
entries.RemoveAt(0);
List<Entity> entities = new List<Entity>();
foreach (string line in entries)
{
Entity entityManagement = new Entity();
string[] lineParts = line.Split('|');
entityManagement.Identifier = lineParts[0];
entityManagement.ProductId = 1234;
entityManagement.Category = "ABCDE";
entities.Add(entityManagement);
}
return entities;
}
The part after ParseCsvFile in parentheses: (List<string> entries, string urlFile)
Could someone explain what these are and what they do, perhaps with metaphors/analogies/real-world examples?
It might be easier to see their purpose if you look at a simpler function for example:
public int Add(int number1, int number2)
{
return number1 + number 2;
}
Above there is a function that adds two numbers together and returns the result. It is a set of instructions to follow. How can it follow the instructions if it doesn't know what numbers to use.
That's where calling the function comes in.
for example:
var result = Add(2, 5);
In this scenario result = 7.
2 is replacing number1 in the function and 5 is replacing number2.

CS1061: Type `string' does not contain a definition for `classChoice' and no extension method `classChoice' [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 am trying to return the value of the string classChoice to a separate Class so that I can return the person's selection, I am still learning C# and have tried to understand this better i read that it needs an instance to work so i created 1, either way, i have tried about 8 different ways of doing this and all keep returning errors, what am I doing wrong?
made it a void and failed, took out the argument and tried to call only the property, tried it outside the cs file in the main cs still no luck.
public class Selection
{
public string CharSel(string classChoice = "")
{
Console.WriteLine("Welcome to the world of Text Games!");
Console.WriteLine("To begin you must select a class!");
Console.WriteLine("Lucky for you there is only 1 class to choose from at this time :-) ");
Console.WriteLine("Select a Class:");
Console.WriteLine("1. Wizard");
Console.WriteLine("2. Nothing");
Console.WriteLine("3. Nothing");
Console.WriteLine("4. Nothing");
Console.WriteLine("5. Nothing");
Console.Write("Make your selection: ");
int choice = Convert.ToInt32(Console.ReadLine());
if (choice == 1)
{
classChoice = "Wizard";
Console.WriteLine("Congrats on selecting {0} now onto your adventure!", classChoice);
}
return classChoice;
}
}
public class Character
{
public static string Wizard(string name)
{
Selection s = new Selection();
string classChosen = s.CharSel().classChoice;
Console.WriteLine("Test, You are a {0}", classChosen);
name = "none yet";
return name;
}
}
Console should spiit out
Test, You are a Wizard
You have a syntax error in your program on the line:
string classChosen = s.CharSel().classChoice;
It should be:
string classChosen = s.CharSel();
CharSel() is a method which returns a string containing the value selected by the user.
You returned that string at the end of the method so when you call that method it effectively is the returned string (the value contained in the classChoice variable). That is why is is giving you that error: 'CharSel()' is a string and what you have written (s.CharSel().classChoice) is attempting to find a classChoice method on the String class (or an extension method). Just remove .classChoice from the assignment to classChosen and it will work as you expect.
Another important point is that classChoice is a private variable of the CharSel() method and isn't visible outside of the method.

Is there a programmatic way to identify .Net reserved words? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am looking for reading .Net, C# reserved key words programmatically in VS 2015.
I got the answer to read C# reserved words in the [link][1].
CSharpCodeProvider cs = new CSharpCodeProvider();
var test = cs.IsValidIdentifier("new"); // returns false
var test2 = cs.IsValidIdentifier("new1"); // returns true
But for var, dynamic, List, Dictionary etc the above code is returning wrong result.
Is there any way to identify .net keywords in run time instead of listing key words in a list?
string[] _keywords = new[] { "List", "Dictionary" };
This is a perfectly fine C# program:
using System;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int var = 7;
string dynamic = "test";
double List = 1.23;
Console.WriteLine(var);
Console.WriteLine(dynamic);
Console.WriteLine(List);
}
}
}
So your premise is wrong. You can find the keywords by looking them up in the short list. Just because something has a meaning does not mean it's in any way "reserved".
Do not let the online syntax highlighting confuse you. Copy and paste it into Visual Studio if you want to see proper highlighting.
As explained by nvoigt, your method of programmatically determining if a string is a keyword is effectively correct. To be complete, (after checking Reflector) it should be:
bool IsKeyword(string s)
{
var cscp = new CSharpCodeProvider();
return s != null
&& CodeGenerator.IsValidLanguageIndependentIdentifier(s)
&& s.Length <= 512
&& !cscp.IsValidIdentifier(s);
}
(The VB.NET version needs 1023 and a check for "_".)

How can I make each array, within an array perform a task? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am getting data with one of the following names, Adam, Bob, Cam, Dan, Earl, or Fred.
I only want certain pairs to operate on each other. Right now I have the string:
string list="Adam-Bob;Cam-Dan;Earl-Fred";
Then I split them via the semicolon
string[] splitList=list.Split(';');
Now I have an array of pairs as so
Adam-Bob Cam-Dan Earl-Fred
[0] [1] [2]
Ideally, I would like to perform an operation on each of them, but instead I find that I can only do the following:
Split via ','
foreach (string s in splitList)
{
string firstPerson=splitList[0];
string secondPerson=splitLilst[1];
if (UDPoutputData.Contains(firstPerson)==true)
{
//record data into string for firstPerson
}
if (UDPoutputData.Contains(seoncdPerson)==true)
{
//record data into string for secondPerson
}
//if I have data for firstPerson AND secondPerson, perform operation and give me the output
}
Unfortunately, if I get the name Adam, followed by Cam, my operations are disorganized. Perhaps I need to automatically create a string for each name? Or is there an eloquent way of operating the data on the first array...
You could get an array of arrays (of string), like this:
string[][] splitList = list.Split(';').Select(pair => pair.Split('-')).ToArray();
Then you can access splitList[0][0] to get Adam, splitList[0][1] would be Bob, splitList[1][0] would be Cam, etc.
So your loop becomes:
foreach (string[] pair in splitList)
{
string firstPerson=pair[0];
string secondPerson=pair[1];
// ...

C# operands error [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to calculate the total for a list in my C# program, I have gotten help from several people in my class and we can't seem to find the problem, my code is,
int totalB = 0;
Cards.ForEach(delegate(ConsoleApplication1.Program.CreditCard Balance)
{
totalB= totalB + Balance;
});
The error is this Error 1 Operator '+' cannot be applied to operands of type 'int' and 'ConsoleApplication1.Program.CreditCard'
Any help for this would be much appreciated as I have no idea and neither do the people that tried to help me with this issue
I'm guessing you have a class like:
partial class CreditCard
{
public int Balance {get; set;}
}
So following what you have, explicitly, you most likely intended:
int totalB = 0;
Cards.ForEach(delegate(ConsoleApplication1.Program.CreditCard card)
{
totalB = totalB + card.Balance;
});
This iterates over each item in your Cards collection, and adds the value of the Balance property to totalB. Note I have called the variable card in the delegate to further illustrate what is going on - the delegate will be called once for each item in the collection. Inside, you can pick out the Balance property and add it to totalB.
Note you can also do this in a number of other ways:
Using LINQ:
int totalB = Cards.Sum(card => card.Balance);
Using a lambda expression instead of an explicit delegate:
int totalB = 0;
Cards.Foreach(card => {totalB += card.Balance;});
Using a foreach loop:
int totalB = 0;
foreach(CreditCard card in Cards)
totalB += card.Balance;
(If you are not familiar with it, the x += y is the same as x = x + y.)
As far as getting sum of list is concerned. it is as simple as (assuming Cards is a list)
Cards.Sum(x=>x.YourPropertyToSum);
Your Error:
The error is this Error 1 Operator '+' cannot be applied to operands of type 'int' and 'ConsoleApplication1.Program.CreditCard'
you are trying to add an int with ConsoleApplication1.Program.CreditCard (what is this) which is obviously not a type that can be added to int. Hence the error you are getting.

Categories