Reputation: 1529
I am new to c# and just switched from c++ to c#. I was doing something like this in c++:
Class A
{
public : A(char *argv);//declaration of constructor
}
then in main i was doing like this:
int main(int argc, char **argv)
{
A Obj(argv[1]);
}
then definition of constructor i do like this :
A::A(char * argv)
{
//Here i use this command line argument argv which contains a file.
}
I tried to write equivalent code in c# which is as follows:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace shekhar_final
{
class Huffman
{
public int data_size,length,i,is_there, total_nodes;
string code;
Huffman(char *args);
}
public Huffman(char *args) //called from MyClass Line:16
{
using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) //Line : 18
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
}
}
}
public class MyClass
{
public static void Main(string[] args)
{
Huffman ObjSym =new Huffman(args);//object creation
}
}
}// Line:34
The couple of errors i got are ://I have indicated the line corresponding to the errors in my code
shekhar_c#.cs(16,25): error CS1525: Unexpected symbol `Huffman', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
shekhar_c#.cs(18,33): error CS1530: Keyword `new' is not allowed on namespace elements
shekhar_c#.cs(18,36): error CS1525: Unexpected symbol `BinaryReader', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
shekhar_c#.cs(18,79): warning CS0658: `value' is invalid attribute target. All attributes in this attribute section will be ignored
shekhar_c#.cs(34,1): error CS8025: Parsing error
Compilation failed: 4 error(s), 1 warnings
Could you please help me in writing c# equivalent of this c++ (removing these errors). Extra guidance are also welcome because i am beginner to c#.
Upvotes: 1
Views: 1921
Reputation: 12857
C# requires constructors to be defined within the class:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace shekhar_final
{
public class Huffman{
public int data_size,length,i,is_there, total_nodes;
string code;
public Huffman(string[] args) //called from MyClass Line:16
{
using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) //Line : 18
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
}
}
}
}
public class MyClass
{
public static void Main(string[] args)
{
Huffman ObjSym =new Huffman(args);//object creation
}
}
}// Line:34
Upvotes: 1
Reputation: 4860
In C#, declarations and implementations go together:
namespace shekhar_final
{
class Huffman
{
public int DataSize {get; set;}
public int Length {get; set;}
public int I {get;set;}
public int IsThere {get;set;}
public int TotalNodes {get;set;}
private string code;
public Huffman(string[] args) //called from MyClass Line:16
{
using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) //Line : 18
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
}
}
}
}
public class MyClass
{
public static void Main(string[] args)
{
Huffman objSym = new Huffman(args);//object creation
}
}
}// Line:34
Upvotes: 3
Reputation: 47
The main phlosophy between C# and C++ are different. In C++ you have a header file and an implementation file. In C#, everthing needs to be within a class. So, you declare the constructor to the class and put the implementation within it.
class funny {
public funny() {
... add your constructor stuff here
}
... other stuff ...
}
Upvotes: 2
Reputation: 25505
In c# you do not separate declaration and definition. There is no such concept as declaration in c# since all the types exist together in an assembly. If you wish to use multiple files in c3 for classes you can use the concept of partial classes.
Upvotes: 1
Reputation: 726669
Unlike C++ where you have a choice of combining the declaration and the definition of a member function in the header, or placing the declaration in the header and the implementation in the cpp file, in C# there is no such choice: if a function has a body (i.e. it is not abstract), the body needs to be part of the declaration:
class Huffman
{
public int data_size,length,i,is_there, total_nodes;
string code;
Huffman(string args) {
using (var stream = new BinaryReader(System.IO.File.OpenRead(args)))
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
}
}
}
}
Upvotes: 3
Reputation: 28316
You don't define methods ahead of time in C# - they're defined within the class itself. Try this instead:
class Huffman
{
public int data_size,length,i,is_there, total_nodes;
string code;
public Huffman(char *args) //called from MyClass Line:16
{
using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) //Line : 18
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
}
}
}
}
public class MyClass
{
public static void Main(string[] args)
{
Huffman ObjSym =new Huffman(args); //Here is the error
}
}
Upvotes: 2