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!
Related
I'm getting a Create Unit Tests is supported only within a public class or a public method when I try to create unit tests for my app. I was trying to test a public method in a very simple app in Visual Studio 2015 Enterprise. Here's a screenshot.
Here's the Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Scratch
{
public class Program //Originally this was just class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world!");
}
public static string TruncateOld(string value, int length)
{
string result = value;
if (value != null) // Skip empty string check for elucidation
{
result = value.Substring(0, Math.Min(value.Length, length));
}
return result;
}
public static string Truncate(string value, int length)
{
return value?.Substring(0, Math.Min(value.Length, length));
}
}
}
I've found this msdn article, but it doesn't really offer any suggestions on how to solve the problem. Also, I've never installed 'ALM Rangers Generate Unit Test.'
I got rid of everything from my Program.cs except for the Main and added a new Public Class1 with the following code (I still get the error and the menu goes away):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Scratch
{
public class Class1
{
public Class1()
{
}
public string TruncateOld(string value, int length)
{
string result = value;
if (value != null) // Skip empty string check for elucidation
{
result = value.Substring(0, Math.Min(value.Length, length));
}
return result;
}
public string Truncate(string value, int length)
{
return value?.Substring(0, Math.Min(value.Length, length));
}
}
}
To anyone that is new to C# having this issue, you need to add the public keyword to your class.
For example,
class myProgram
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
You'd need to change the first line to public class myProgram
Probably coming to the party too late, but since I had the same problem and was able to solve it, you never know who is going to need it. So... as soon as I removed the reference to the Microsoft.VisualStudio.QualityTools.UnitTestFramework
the "Create Unit Tests" option reappeared in the context menu and I was able to create the tests I needed.
So I know this is late, and not really an answer to the above question due to your class definition, but I don't see enough forums on this error, so I decided to add my findings here.
I found that if you have a class with only properties and no true methods, 'Create Unit Tests' will not work. Even if your properties are public and have complex getters or setters. Here was the state of my code when running into this problem, as I just made the class:
namespace some.Namespace
{
using some.Namespace.Interfaces;
using System;
using System.Collections.Generic;
public class SomeData : ISomeData
{
public bool SomeFlag => throw new NotImplementedException();
public Dictionary<string, string> SomeFields => throw new NotImplementedException();
public Dictionary<string, string> SomeOtherFields => throw new NotImplementedException();
}
}
However, you can work around this by either defining at least constructor or by temporarily making some random method. However, note that tests will only be made for true methods in your class so you will still have to manually write the tests for your properties' getters/setters.
Hope someone finds this answer helpful.
Apparently, this is still a rabbit hole to fall into, as I stepped into it today. In my case, running on VS 2019 Enterprise. My error was having NuGet packages installed on the test project which isn't needed. I had added NuGet packages MSTest.TestAdapter, and Microsoft.TestPlatform. Removing both, closing and reopening the IDE restored the wizard for creating a Test Method. For an initial or new test project, the only NuGet package needed for getting started is Microsoft.VisualStudio.TestPlatform.
If you mean the built in Visual Studio 2015 test cases, you need to take out the static from the public members. Like this:
[TestClass]
public class UnitTests
{
[TestMethod]
public void Testing_01()
{
Assert.IsTrue( [some bool condition] );
}
}
Basically, I've been searching the internet for a while to find out why I have this problem but I can't find the answer. Can someone please help me with the solution?
I have heard that it has something to do with the namespaces but I don't exactly know what I am doing wrong.
The error is with me trying to create a new linked list and it comes up with the requires type 1 arguement with "LinkedList" and "List".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
namespace LinkedList2
{
public class Node
{
public int data;
public Node next;
}
public class List
{
private Node head;
public void AddData(int data)
{
Node node = new Node();
while (node != null)
{
System.Console.Write(node.data);
System.Console.Write(" -> ");
node = node.next;
}
System.Console.WriteLine("");
}
}
}
class Program
{
static void Main(string[] args)
{
LinkedList List1 = new List();
}
}
Severity Code Description Project File Line
Error CS0305 Using the generic type 'List' requires 1 type arguments Soft153Assignment C:\Users\Casey\Desktop\University Assignments\Soft153Assignment\Soft153Assignment\Program.cs 53
Severity Code Description Project File Line
Error CS0305 Using the generic type 'List' requires 1 type arguments Soft153Assignment C:\Users\Casey\Desktop\University Assignments\Soft153Assignment\Soft153Assignment\Program.cs 53
List is a class name built-in the .NET framework under the System.Collections.Generic namespace. If you look at the top of your code, you're using System.Collections.Generic which means that "your" public class List conflicts with the one in the .NET framework
What you can do is rename "your" List or use the .NET built-in classes such as LinkedList and List since those can pretty much do what you want a specific collections class to do.
you could make your code look something like this... if you're required to use 'List` as the name of your class.
namespace LinkedList2
{
public class Node
{
//...
}
public class List
{
//...
}
class Program
{
static void Main(string[] args)
{
List List1 = new List();
}
}
}
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();
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.
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.