C# - Error on Skip - File.ReadLines(FileNameFinal).Skip.Take(1).First() - c#

I have this error 'System.Linq.Queryable.Skip(System.Linq.IQueryable, int)' is a 'method', which is not valid in the given context.
It is just going to read a file and then read the 15th line but i get the error as above.
Please Help
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
namespace FileManager
{
public class OpenFile
{
public static string FileNameFinal;
public static string GetFileName(string FileName);
public static string line = File.ReadLines(FileNameFinal).Skip.Take(1).First();
}
}

The problem is in .Skip.
As the error specified Skip is a method, and therefor should be called as one: Skip(3) (the 3 is just en example for an argument)
You need to specify How many items you want to skip.
Try something like:
public static string line = File.ReadLines(FileNameFinal).Skip(3).Take(1).First();
for skipping the first 3 items.
You can take a look at the documentation for more details about the method.

Skip require int parameter.
SKIP : how many value it will skip
provide value to it like Skip(10) which will skip 10 values
public static string line = File.ReadLines(FileNameFinal).Skip(10).Take(1).First();

Related

'WordBundle' does not contain a constructor that takes 2 arguments

I feel like this is probably a really dumb oversight on my part, but I can't see why I'm getting the error that my constructor doesn't take 2 arguments. It looks like it does to me, I think I'm using the right class and namespace names, after reading it repeatedly and searching similar answers on here I can't figure it out. Can anyone identify what I'm doing wrong?
MODEL:
using System;
using System.Collections.Generic;
namespace WordCounter.Models
{
public class WordBundle
{
private string _wordInput;
private string _sentenceInput;
public WordBundle (string wordInput, string sentenceInput)
{
_wordInput = wordInput;
_sentenceInput = sentenceInput;
}
}
}
TEST:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WordCounter.Models;
using System;
using System.Collections.Generic;
namespace WordCounter.Tests
{
[TestClass]
public class WordBundleTest
{
[TestMethod]
public void WordBundleConstructor_CreatesInstanceOfWordBundle_WordBundle()
{
string testWord = "cat";
string testSentence = "Who let the cat out of the cathedral?";
WordBundle newWordBundle = new WordBundle(testWord, testSentence);
Assert.AreEqual(typeof(WordBundle), newWordBundle.GetType());
}
}
}
OK, after getting more eyes on the project, it looks like I have a conflict with a classname in another file and that was hijacking my constructor call. Thanks for taking a look, João!

Why I have to write the namespace to access to this extension method?

I have a project that has class to implement extension methods for some type. For example I have this class for ObservableCollection:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace MyProject.Collections.Utils
{
public static class ObservableCollection
{
public static void RemoveAll<T>(this ObservableCollection<T> collection, Func<T, bool> condition)
{
for (int i = collection.Count - 1; i >= 0; i--)
{
if (condition(collection[i]))
{
collection.RemoveAt(i);
}
}
}//RemoveAll
}
}
With this class, in my main project I can use this library with the using:
using MyProject.Collections.Utils
And when I want to use the extension methods I can do:
ObservableCollection<MyType> myOC = new ObservableCollection<MyType>();
myOC.RemoveAll(x=>x.MyProperty == "123");
So I have access to my extension method.
However, I have another class for Decimal, is this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyProject.Decimal.Utils
{
public static class Decimal
{
public static decimal? Parse(this string paramString)
{
try
{
myCode
}
catch
{
throw;
}
}//Parse
}
}
But in this case, although in my main prject I import the class:
using MyProject.Decimal.Utils;
If I do this:
decimal? myDecimalParsed= Decimal.Utils.Decimal.Parse("123");
Why in this case I can't do this?:
decimal? myDecimalParsed= decimal.Parse("123");
thank so much.
Two problems:
You can't use extension methods as if they were static methods of the extended type
System.Decimal already has a Parse method, and the compiler always looks for "real" methods before extension methods.
In fact, you can write
decimal? miTiempoEstimadoParseado = decimal.Parse("123");
... but that will just call the normal method and then convert the decimal to decimal? implicitly in the normal way.
Note that you're not really using your method as an extension method at the moment anyway - to do so you'd write something like:
decimal? miTiempoEstimadoParseado = "123".Parse();
... but personally I'd view that as pretty ugly, partly as the method name doesn't indicate the target type at all, and partly because by convention Parse methods throw an exception instead of returning a null value on failure. You probably want to come up with a different name.

initializecomponent() don't exist in current context while using generics in c# web application

I am trying to create a generics in c# web application and using silverlight-5. This i have already implemented in c# console application.
I am trying to do same in webdevelopment using asp.net,c# and silverlight (and GUI using xaml) in Vs-2010. Whose GUI is displayed on internet explorer on running the code (by button click events).
In console application i do so by following code : (The code is to read a binary file as sole argument on console application and read the symbol in that file, These symbol could be int32/int16/int64/UInt32 etc.). So have to make this Symbol variable as "generic"(<T>). And in console application this code works fine.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace check
{
LINE:1 public class Huffman < T > where T: struct,IComparable < T >,IEquatable < T >
{
public int data_size, length, i, is_there;
public class Node
{
public Node next;
line:2 public T symbol; // This symbol is of generic type.
public int freq;
}
public Node front, rear;
LINE:3 public Huffman(string[] args, Func < byte[], int, T > converter)
{
front = null;
rear = null;
int size = Marshal.SizeOf(typeof (T));
using(var stream = new BinaryReader(System.IO.File.OpenRead(args[0])))
{
long length = stream.BaseStream.Length;
for (long position = 0; position + size < length; position += size)
{
byte[] bytes = stream.ReadBytes(size);
LINE:4 T processingValue = converter(bytes, 0); //**Here I read that symbol and store in processing value which is of type <T>**
//Then further i use this processingValue and "next" varible(which is on Node type)
}
}
}
}
public class MyClass
{
public static void Main(string[] args)
{
line:5 Huffman < long > ObjSym = new Huffman < long > (args, BitConverter.ToInt64);
// It could be "ToInt32"/"ToInt16"/"UInt16"/"UInt32"/"UInt64" with respective
//change in <int>/<short> etc.
//Then i further use this ObjSym object to call function(Like Print_tree() here and there are many more function calls)
ObjSym.Print_tree(ObjSym.front);
}
}
}
The same thing i have to achieve in C# silverlight(web application) with a difference that i have already uploaded and stored the file by button click (By Browsing it)(whereas i was uploading/reading file as sole argument in console application), This file upload part i have already done.
The problem now is how to make this "symbol" variable generic(<T>) here because i am not able to see any Object creation (In main(string[] args) method) where i could pass parameter BitConverter.ToInt32/64/16 (as i am doing in console application, please see code).
NOTE: please see that i have used in LINE 1,2,3,4,5 in my code (so that the same(or different if you have other approach) has to be achieved in the code below to make "symbol" of type )
Because in c# i get body of code like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace check
{
public partial class MainPage : UserControl
{
public class Node
{
public Node next;
public long symbol; // This symbol is of generic type.
public int freq;
}
public Node front, rear;
public MainPage()
{
InitializeComponent();
}
}
}
Could some one please help me in changing the code of this web application exactly similar to that console application code (I mean making "Symbol variable as generic(<T>)")
EDIT: When i do this:
(1) public partial class MainPage <T> : UserControl, IComparable < T > where T: struct,IEquatable < T >
(2) public T symbol; (In Node class)
(3) And all the buttons and boxes i created are given not existing in current context.
then it gives error
Error :The name 'InitializeComponent' does not exist in the current context
Could some one please help me in achieving the same in c# silverlight web application ? Would be a big help,thanks.
Here is a Example.
namespace check
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
// Use the generic type Test with an int type parameter.
Test<int> Test1 = new Test<int>(5);
// Call the Write method.
Test1.Write();
// Use the generic type Test with a string type parameter.
Test<string> Test2 = new Test<string>("cat");
Test2.Write();
}
}
class Test<T>
{
T _value;
public Test(T t)
{
// The field has the same type as the parameter.
this._value = t;
}
public void Write()
{
MessageBox.Show(this._value);
}
}
}
I think you asking this kind of example.
You can use generic as if you don’t use XAML. But if you want to use XAML to define your control, you can’t use generic. That's why the problem is occurs.
Create a another class and use it. I think It's help you.

C# Beginner problems

I have a "Debug" class, which simply prints information to the console etc. From the rest of the code I want to be able to call the methods within it, but so far it's only partly working.
Calling dc.Print() works fine, but as soon as I call dc.Print(dc.GetEventsLogged()) I get a red line with the message
"The best overloaded method match has some invalid arguments" as well as Argument 1: cannot convert from 'int' to 'string'.
Basically: Why are my arguments to dc.Print wrong? Also, what can I do about "cannot convert from int to string? I tried .ToString but that didn't work either.
This is my "Debug.cs" class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public class Debug
{
private int events_logged;
public Debug()
{
events_logged = 0;
}
public void Print(string Message)
{
Console.WriteLine("[" + DateTime.UtcNow + "] " + Message);
events_logged++;
}
public int GetEventsLogged()
{
return events_logged;
}
}
}
And in my "Program.cs" class I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Debug dc = new Debug();
dc.Print("Test");
}
}
}
The reason you are seeing the error is because GetEventsLogged() returns an int whereas Print() expects you to pass in a string. Therefore you need to the return from int to string and you were on the right track with ToString(). This will do what you want to achieve:
dc.Print(dc.GetEventsLogged().ToString());
dc.Print() wants a string argument and dc.GetEventsLogged() returns an int. You need to ToString() the int so that the types match.
int numberOfEventsLogged = dc.GetEventsLogged();
string numberOfEventsLoggedAsString = numberOfEventsLogged.ToString();
dc.Print(numberOfEventsLoggedAsString)
try dc.Print(dc.GetEventsLogged().toString()) because GetEventsLogged() is of int type and Print(string Message) looking for string input.
Your method Print expects an argument of type String. When you call dc.Print(dc.GetEventsLogged()), your actually give an int because your method GetEventsLogged() returns an int.
public string GetEventsLogged()
{
return events_logged.ToString();
}

C#: property/field namespace ambiguities

I get compile error because the compiler thinks Path.Combine refers to my field, but I want it to refer to class System.IO.Path. Is there a good way to handle this other than always having to write the FQN like System.IO.Path.Combine()?
using System.IO;
class Foo
{
public string Path;
void Bar(){ Path.Combine("",""); } // compile error here
}
You can do this:
using IOPath = System.IO.Path;
Then in your code:
class Foo
{
public string Path;
void Bar(){ IOPath.Combine("",""); } // compile error here
}
Seperate your references:
this.Path = System.IO.Path.PathFunction();
I'd strongly suggest implying the System.IO namespace when using Path anywhere inside that class, because it's very difficult to tell the difference. Using the this. qualifier and the full namespace makes them distinct.

Categories

Resources