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();
}
}
}
Related
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!
So here is a snippet I made where one namespace in inside another (B is inside A).
Usually when you use 'using SpaceA;' you can access all elements without typing SpaceA.
This is the case for class A, but I cannot see SpaceB without having to type SpaceA.SpaceB. Why is this?
Here is the code:
using System;
using SpaceA;
namespace SpaceA
{
class A
{
public A()
{
Console.WriteLine("A");
}
}
namespace SpaceB
{
public class B
{
public B()
{
Console.WriteLine("B");
}
}
}
}
namespace TestingCSharp
{
class Program
{
static void Main(string[] args)
{
//This does not work
SpaceB.B x = new SpaceB.B();
//This does work
SpaceA.SpaceB.B y = new SpaceA.SpaceB.B();
}
}
}
Usually when you use 'using SpaceA;' you can access all elements without typing SpaceA.
Only the direct types which are members of SpaceA. A namespace-or-type-name is never resolved using a namespace in a normal using directive and then another "subnamespace" in the name. Note that this has nothing to do with how the types are declared (in terms of having a nested namespace declaration or just namespace SpaceA.SpaceB) - another example would be:
using System;
...
Xml.Linq.XElement x = null; // Invalid
See section 3.8 of the C# 5 specification for the precise details of how names are resolved.
One slight difference to this is if you have a using alias directive for a namespace, at which point that can be used to look up other namespaces
using X = System.Xml;
...
X.Linq.XElement x = null; // Valid
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'm trying to use a precompiled DLL with reflection, to instantiate an interface for my class that is in the DLL. I tried by the book, but it won't work. It throws InvalidCastException when I try to do something like:
ICompute iCompute = (ICompute)Activator.CreateInstance(type);
Where type of course is my class that implements ICompute interface. I'm stuck and don't know what to do. The complete code follows:
This is the DLL content:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication18
{
public class ClassThatImplementsICompute : ICompute
{
public int sumInts(int term1, int term2)
{
return term1 + term2;
}
public int diffInts(int term1, int term2)
{
return term1 - term2;
}
}
}
The actual program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication18
{
public interface ICompute
{
int sumInts(int term1, int term2);
int diffInts(int term1, int term2);
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Loading dll...");
Assembly assembly = Assembly.LoadFrom("mylib.dll");
Console.WriteLine("Getting type...");
Type type = assembly.GetType("ConsoleApplication18.ClassThatImplementsICompute");
if (type == null) Console.WriteLine("Could not find class type");
Console.WriteLine("Instantiating with activator...");
//my problem!!!
ICompute iCompute = (ICompute)Activator.CreateInstance(type);
//code that uses those functions...
}
}
}
Can anyone help me? Thanks!
The problem is to do with how you load the assembly with Assembly.LoadFrom().
LoadFrom() load the assembly into different context compared to context of the ICompute interface you are trying to cast to. Try to use Assembly.Load() instead if possible. i.e. put the assembly into the bin / probing path folder and load by the full strong name.
Some references:
http://msdn.microsoft.com/en-us/library/dd153782.aspx
http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx (see the disadvantage bit for LoadFrom)
I'm a brand-newbie to C#, albeit not programming, so please forgive me if I mix things up a bit -- it's entirely unintentional. I've written a fairly simple class called "API" that has several public properties (accessors/mutators). I've also written a testing console application that uses reflection to get an alphabetically list of names & types of each property in the class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using MyNamespace; // Contains the API class
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hi");
API api = new API(1234567890, "ABCDEFGHI");
Type type = api.GetType();
PropertyInfo[] props = type.GetProperties(BindingFlags.Public);
// Sort properties alphabetically by name.
Array.Sort(props, delegate(PropertyInfo p1, PropertyInfo p2) {
return p1.Name.CompareTo(p2.Name);
});
// Display a list of property names and types.
foreach (PropertyInfo propertyInfo in type.GetProperties())
{
Console.WriteLine("{0} [type = {1}]", propertyInfo.Name, propertyInfo.PropertyType);
}
}
}
}
Now what I need is a method that loops through the properties and concats all the values together into a querystring. The problem is that I'd like to make this a function of the API class itself (if possible). I'm wondering if static constructors have something to do with solving this problem, but I've only been working with C# for a few days, and haven't been able to figure it out.
Any suggestions, ideas and/or code samples would be greatly appreciated!
This is unrelated to static constructors. You can do it with static methods:
class API {
public static void PrintAPI() {
Type type = typeof(API); // You don't need to create any instances.
// rest of the code goes here.
}
}
You can call it with:
API.PrintAPI();
You don't use any instances when you call static methods.
Update: To cache the result, you can either do it on first call or in an static initializer:
class API {
private static List<string> apiCache;
static API() {
// fill `apiCache` with reflection stuff.
}
public static void PrintAPI() {
// just print stuff from `apiCache`.
}
}