Scope Resolution Operator in c# to access global variable? - c#

I have a Program in C++
int x=100; //Global declaration
main()
{
int x=200;
{
int y;
y=x;
cout<<"Inner Block"<<endl;
cout<<x<<endl;
cout<<y<<endl
cout<<::x<<endl;
}
cout<<"Outer Block"<<"\n";
cout<<x<<"\n";
cout<<::x;
}
Output of this program is:
Inner Block
200
200
100
Outer Block
200
100
I want to try similar thing in c# but when I type ::x,i gives me error...
Please help
What I have tried is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CAScopeResolution_Operator
{
class Program
{
static int x = 100;
static void Main(string[] args)
{
int x = 200;
{
int y;
y = x;
Console.WriteLine("Inner Block");
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(Program.x);
}
Console.WriteLine("Outer Block");
Console.WriteLine(x);
Console.WriteLine(Program.x);
Console.ReadLine();
}
}
}
I have declared static x,but I dont think this is the solution to have similar code in c#... Please help

As C# does not deal with global variables as C++ does, the :: has a different meaning. It is about namespaces here, as you can identify each member by the class it belongs to.
So if you have namespaces and/or types that share an identifier but in different namespace, you can identify them using ::-operator.
using colAlias = System.Collections;
namespace System
{
class TestClass
{
static void Main()
{
// Searching the alias:
colAlias::Hashtable test = new colAlias::Hashtable();
// Add items to the table.
test.Add("A", "1");
test.Add("B", "2");
test.Add("C", "3");
foreach (string name in test.Keys)
{
// Searching the global namespace:
global::System.Console.WriteLine(name + " " + test[name]);
}
}
}
}
generates this
A 1
B 2
C 3
See here for MSDN reference.

Related

C# calls the function of Cpython

This is the code I copy, and I want to learn to use Python.Runtime, but when it runs, it throws an exception.
"DLL" python2.7m "cannot be loaded: no specified module can be found. (the exception comes from HRESULT:0X8007007E). "
As a problem with a rookie. Thank you.
using Python.Runtime;
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
using (Py.GIL())
{
dynamic np = Py.Import("numpy");
dynamic sin = np.sin;
Console.WriteLine(np.cos(np.pi * 2));
Console.WriteLine(sin(5));
double c = np.cos(5) + sin(5);
Console.WriteLine(c);
Console.ReadKey();
}
}
}
}

What's the difference of using and not using Invoke?

I'm confused with using and not using Invoke.
In the following example, I see no difference with
int answer = b(10, 10);
and
int answer = b.Invoke(10, 10);
so can anyone help me with this?
thx!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace TEST
{
public delegate int BinaryOp(int x, int y);
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main innvoked on thread {0}.", Thread.CurrentThread.ManagedThreadId);
BinaryOp b = new BinaryOp(add);
//int answer = b(10, 10);
int answer = b.Invoke(10, 10);
Console.WriteLine("Doing more work in Main");
Console.WriteLine("10 + 10 is {0}", answer);
Console.Read();
}
static int add(int x, int y)
{
Console.WriteLine("add innvoked on thread {0}.", Thread.CurrentThread.ManagedThreadId);
return x + y;
}
}
}
Basically there is no difference.
Specifically since a Delegate is not really a function rather a holder to a function, that class that holds the function has to have some way to Invoke it. Hence the method Invoke. On the other hand the compiler is smart enough to automatically call Invoke when you append parenthesis to a Delegate. See https://jacksondunstan.com/articles/3283.

when i create and construct 2 objects of the same type, they copy each other to the last constructed object

im a noob in c#. My problem comes when i want to create 2 objects
two_digt d1,d2;
of course i got to call for two_digt(int n) to create an object. But when i do it for d1 and d2, the last one called overwrites the first. Let me introduce to you my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class two_digt {
private static int dgt1, dgt2;
public two_digt(int n) {//constructor
dgt1 = n % 10;
dgt2 = (n % 100)/10;
}
public void print() {//funct to show what they've got
Console.WriteLine("dgt1:"+ dgt1 );
Console.WriteLine("dgt2:"+ dgt2 );
}
}
class Program
{
static void Main(string[] args)
{
two_digt d1 = new two_digt(Convert.ToInt32(Console.ReadLine()));
d1.print();
two_digt d2 = new two_digt(Convert.ToInt32(Console.ReadLine()));
//printing each objects
d2.print();
d1.print();
Console.ReadLine();//just to keep console from closing
}
}
}
Now i will show you the program i/o:
input:
1234
8765
//as you can suggest from my code "two_digt" will took for the
//first 2 digits of both numbers
output:
//d1.print();
dgt1:4
dgt2:3
//d2.print();
dgt1:6
dgt2:5
//d1.print();
dgt1:6
dgt2:5
//What...?
that is actually my issue and i dont understand what .net framework does with data structures, please leave an answer to this ,or, if you can, leave a pro explanation.
The problem is that dgt1, dgt2 are static. Remove the static modifier and you should get separate values.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class two_digt
{
private int dgt1, dgt2;
public two_digt(int n)
{//constructor
dgt1 = n % 10;
dgt2 = (n % 100) / 10;
}
public void print()
{//funct to show what they've got
Console.WriteLine("dgt1:" + dgt1);
Console.WriteLine("dgt2:" + dgt2);
}
}
class Program
{
static void Main(string[] args)
{
two_digt d1 = new two_digt(Convert.ToInt32(Console.ReadLine()));
d1.print();
two_digt d2 = new two_digt(Convert.ToInt32(Console.ReadLine()));
//printing each objects
d2.print();
d1.print();
Console.ReadLine();//just to keep console from closing
}
}
}

Is C# 7 working in VS 15 Preview 4?

I tried a simple test but it didn't like out variables
As a simple test, I wrote this (perhaps there is something simple wrong with it, but I also had trouble with patterns and with tuples)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class Program
{
static void Main(string[] args)
{
Runner runner = new ConsoleApplication2.Runner();
Point p = new ConsoleApplication2.Point();
runner.PrintCoordinates(p);
}
}
public class Point
{
int x = 20;
int y = 50;
public void GetCoordinates(out int a, out int b)
{
a = x;
b = y;
}
}
public class Runner
{
public void PrintCoordinates(Point p)
{
p.GetCoordinates(out int x, out int y);
Console.WriteLine($"({x}, {y})"); // x does not exist in current context
}
}
}
According to this post, where the PrintCoordinates example method comes from:
Note: In Preview 4, the scope rules are more restrictive: Out variables are scoped to the statement they are declared in. Thus, the above example will not work until a later release.
The new tuples suffer from a similar problem, though it seems you can partially work around that with a NuGet download:
Note: Tuples rely on a set of underlying types, that aren’t included in Preview 4. To make the feature work, you can easily get them via NuGet:
Right-click the project in the Solution Explorer and select “Manage NuGet Packages…”
Select the “Browse” tab, check “Include prerelease” and select “nuget.org” as the “Package source”
Search for “System.ValueTuple” and install it.

Error 1 The name 'WriteAt' does not exist in the current context

I'm trying to write a game in C# that runs on my cmd on Windows and I need to be able to write to any part of the box to do that. I found WriteAt used extensively for this purpose, however it doesn't seem to work in VS 2010. I get the error: "The name WriteAt does not exist in the current context"
I have the default:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
at the top of my code. So why can't I use WriteAt?
Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GamePCL
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
for (int x = 0; x < 24; x += 2)
{
WriteAt("█", x, 0);
WriteAt("█", x, 30);
}
}
}
}
When you call a method without an object or type prefix, as in this case WriteAt() (as opposed to for example Console.WriteLine(), which is called on the Console type), the method must exist in the current context, i.e. in the current class.
You copied that code from MSDN without copying the relevant method:
protected static void WriteAt(string s, int x, int y)
{
try
{
Console.SetCursorPosition(origCol+x, origRow+y);
Console.Write(s);
}
catch (ArgumentOutOfRangeException e)
{
Console.Clear();
Console.WriteLine(e.Message);
}
}

Categories