no System.IO.File.ReadLines(file).Count() in .Net 4? - c#

I am trying to read the number of lines of a file
I found here (stackoverflow) that the best way to read the number of lines in a large file is by using the following code:
int count = System.IO.File.ReadLines(file).Count();
However, I can't make it compile. Does any know what is the problem?
Error 5 'System.Collections.Generic.IEnumerable<string>' does not
contain a definition for 'Count' and no extension method 'Count'
accepting a first argument of type
'System.Collections.Generic.IEnumerable<string>' could be found (are
you missing a using directive or an assembly reference?)
Thanks,
Eyal

Count<T>() is an extension method for objects of IEnumerable<T>. Try adding a using statement for the namespace System.Linq.

Could you do:
int count = File.ReadAllLines(#"C:\filepath\file.txt").Length;
EDIT: As pointed out in the comments, this could (will) perform badly for large files. For a similar question with more detailed explanation why, view Determine the number of lines within a text file

This is count for line from window application in read from text file:
int linecount = System.IO.File.ReadAllLines(#"D:\yfile.txt").Length;
MessageBox.Show(linecount != null && (!string.IsNullOrEmpty(linecount.ToString())) ? linecount.ToString() : "Unable To Count");

Related

UiPath: How to set default values to a String[] variable

I have an exception when I'm trying to launch my workflow. It cames from my array declaration where I apparently don't have the right syntax.
My syntax really looks like the one used in the UiPath tuto I follow :
The tuto syntax:
My syntax:
I don't understand how I'm supposed to write the default values for a simple array of strings...
I'm only trying to pass the values in a Write Line activity, but this shows up when compiling:
If anyone can help, I'm stuck... Thanks
--- EDIT ---
The exception message is the following one:
Message: Compilation failure:
; expected Unvalid expression terms ',' ... many times...
The full results are stored in the Data property of this exception. Correct the errors of the source, then retry the load.
I found the problem: I did not instantiate the array with new String[] at the beginning. I suppose that the tutos are written using VB and not C#, which can explain the difference.
your declaration is correct maybe you have error in the loop here is a working example

setgraphicsAPIs is Overloaded Method match (C# Unity)

Can someone help me
PlayerSettings.SetGraphicsAPIs (BuildTarget.Android, TargetGlesGraphics.OpenGLES_2_0);
gives me this compile error :
Assets/_Developer/JEONG/Editor/BuildEditor.cs(776,18): error CS1502:
The best overloaded method match for
`UnityEditor.PlayerSettings.SetGraphicsAPIs(UnityEditor.Buil‌​dTarget,
UnityEngine.Rendering.GraphicsDeviceType[])' has some invalid
arguments
I don't know why but i follow the documentation in Unity
You should call it like that :
PlayerSettings.SetGraphicsAPIs (BuildTarget.Android, new [] { GraphicsDeviceType.OpenGLES3 });
As per the error message, the function is expecting an array of GraphicsDeviceType. Since you want only one device type, you still have to provide an array where there is one element.
Also, the value is GraphicsDeviceType.OpenGLES3 (full name with namespace : UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3 ), and not TargetGlesGraphics.Whatever .
You can see in error message that it expects a value as an array of GraphicsDeviceType, so I searched the unity doc to find this :
https://docs.unity3d.com/ScriptReference/Rendering.GraphicsDeviceType.html
Thank you sir Pac but i tried this one
PlayerSettings.SetGraphicsAPIs (BuildTarget.Android, new [] {UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2});
and it works . Thank you also to everyone that help me. Sorry for my bad english

Additional information: Index and length must refer to a location within the string

I have a substring which is defined as:
<p>#Html.Raw(item.Post.Substring(0, 300))...</p>
The text I'm entering far exceeds 300 characters, but I'm still getting the error above. I thought the second param on the substring method was supposed to be the number of characters you want to cut to?
Unless I'm wrong and I'm missing something?
Try following
item.Post.Substring(0, Math.Min(300, item.Post.Length));

How to find Distinct and Max value from ArrayList in vb.net

I need to get distinct and max value from arraylist in vb.net. I did this in c# and it works like
suppose arraylist has values (1,1,2,4,5,5,60)
C#:-
_objarr.ToArray().Distinct().Max(); Result: 60
this works fine in c# but when i convert into vb
Vb:-
_objarr.ToArray().Distinct().Max()
then i got error 'Distinct' is not a member of 'System.Array'
So please tell me how i can overcome this error in vb.net.
Thanks
Add using to your code
System.Linq;
Distinct is an extension method defined in System.Linq.Enumerable
In vb.net you need to create an instance of your array

C# How can I get from CompilerError instance exact text which caused the error

Could you please tell me how can I get from CompilerError instance exact text which caused the error.
Edited:
What about using
compilerError.FileName
and reading the file with text reader? I am trying to do so but it seems that Compiler doesn't create cs file that doesn't pass the compilation any suggestions?
This CompilerError? : http://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilererror.aspx
There are FileName and Line properties, that's the best you can get.
What are you compiling - is it entirely in-memory (CodeDOM)?
If so you can add code-line pragmas to your object model: http://msdn.microsoft.com/en-us/library/system.codedom.codelinepragma.aspx then you'll be able to link an error to a DOM element.
Or, you can compile from source, then you'll have the source code itself and can get the text from the line number.
The best your going to get is the line number.

Categories