I want to write simple macro in c#, to be more clear i just want to save some typing when testing (but want use snippets, cause they don't save space). Want to replace reserved keywords with shorter words
Example in c++ (return sum):
#define r return
int foo(int i, int c){ r (i + c); }
So the point is to save same space and typing, is there something similar in c#?
C# does not support macros like C++. Visual Studio on the other hand has snippets
Check that question:
C# Macro definitions in Preprocessor
Macros in a different sense:
http://alookonthecode.blogspot.com.au/2011/06/macros-in-c.html
A Macro Preprocessor in C#
http://www.codeproject.com/Articles/10049/A-Macro-Preprocessor-in-C
Related
In c++, one can use std::cout << std::hexfloat << someFloatValue to print floating point values in base 16, instead of the usual base 10. For example, 0.09375 (i.e. 3/32) would be printed as "0x1.8p-4"
It should not print "0x3DC00000"! This is simply be type-punning the float to an int and printing that in hexadecimal. That's not what I'm looking for!
I am looking specifically for the C++ "hexfloat" format.
I cannot find anything similar in .NET. I need to print some floating point vaules from a VB.NET program (but that's not required, C# works too) that are later read by a C++ program. Ideally, I would also like to be able to parse those back into my VB.NET program.
How do I correctly print hexfloats from .NET? I would rather avoid rolling my own algorithm, if there is already something in the framework. If not, I would like to avoid platform-dependent code, e.g. plonking the float into a union ([StructLayout(LayoutKind.Explicit)] struct) and inspecting the underlying bits through that.
I am developing a program using C#, but I just figured out that what I am going to program is very very difficult in C# yet easy in Python. So what I want to do is make a .PYD file and use it in C# program, but I don't know how. I've been searching about it but I couldn't find anything.
So these are my questions: How do I use .PYD file in C#? I know that .PYD files are similar to .DLL files, so are .PYD files still usable in computers that have no Python installed?
A .pyd file IS a DLL but with function init*() where * is the name of your .pyd file. For example, spam.pyd has a function named initspam(). They are not just similar, but the same! They are usable on PCs without Python.
Do not forget: builtins will NOT be available. Search for them in C, add them as an extern in your Cython code and compile!
Use pythonnet
Tutorial
You need a GIL state.
using (Py.GIL())
{
// Your code here
}
Inside the GIL block, create a scope.
using (Py.GIL())
{
using (PyScope scope = Py.CreateScope())
{
// Your code here
}
}
To execute code:
scope.Exec(codeStr);
To set a variable:
scope.Set(name, value); // 'name' is a string and 'value' is a 'PyObject' object.
To convert to PyObject:
obj.ToPython()
Convert to PyObject if you need to put your object into Python.
To evaluate[0][1][2][3][4] an expression:
scope.Eval(expr)
To get a variable[5][6] value:
scope.Get(variableName)
C# code that doesn't have an end semicolon before any comment and is one-line represents an expression.
References
0: https://codereview.stackexchange.com/questions/160524/evaluating-arithmetic-expressions1: https://www.programiz.com/python-programming/methods/built-in/eval2: What does Python's eval() do? on SO3: https://docs.python.org/3/reference/expressions.html4: Python - Evaluate math expression within string on SO5: https://realpython.com/python-variables6: https://www.w3schools.com/python/python_variables.asp
I am trying to make a basic translator, which changes values in the code, for example, a . may be ::, e.t.c, and I can do that by using
if(code.Contains("."))
{
code.Replace(".", "::");
}
But my problem is I don't know how to ignore it in the inside of speech, as if the sentence was "Hello.", it could be translated to "Hello::". How would I be able to stop this? (I know you can use Regex "\".+?\"" to find speech in text)
What you're trying to do here is vastly more complicated than you realise.
Programming languages differ in more than just appearance, they have different capabilities and syntax rules, even for two as similar as C# and C++. Aside from the fact that the C# . is equivalent to . -> and :: in C++. There's also different rules regarding pointers, and sometimes you get issues like having a pointer to a pointer, not to mention that the * and & symbols can be binary arithmetic/logic operations or pointer operations depending on their use. There's also issues involving keywords such as const, auto and sizeof.
In short, unless you're prepared to write a proper tokeniser, you aren't going to pull this off properly. To properly translate one programming language to another you would at least have to write a good chunk of a full compiler, which is a specialist subject.
I suggest you do some research into tokenisers and lexical analysis before you go any further.
As a hint though, you'll find it easier to split your code up into an array of characters and handle one character at a time whilst keeping track of the program's state (ie are you currently in the middle of a string, are you in the middle of two brackets, how many levels of nesting have you come across). By doing it that way you can at least manage to change the surface-level differences (as opposed to deeper ones like keywords and typing).
EDIT:
Some useful resources for writing tokenisers and compilers:
http://www.cs.man.ac.uk/~pjj/farrell/comp3.html
http://msdn.microsoft.com/en-us/library/vstudio/3yx2xe3h(v=vs.100).aspx
http://en.wikibooks.org/wiki/Compiler_Construction/Lexical_analysis
I speak from experience, as I did recently attempt to write my own compiler (in C#), but put the project on hold due to more important matters getting in the way.
You could probably use a regex to help you out here, but it would be fairly complex and perform poorly. You could also just do this:
var sb = new StringBuilder();
bool insideSpeech = false;
foreach(char c in code) {
if(c == '"') {
insideSpeech = !insideSpeech;
}
if(c == '.' && !insideSpeech) {
sb.Append("::");
} else {
sb.Append(c);
}
}
code = sb.ToString();
Well in short i am a big fan of writing nice looking code, there is a great article here
the beauty of doom 3 source code I have been using different code editors before visual studio.
And i liked a compact writing style.
I dont like almost empty white lines like
public void my function(string s)
{ //almost empty line
string n = domystuff();
if (n=="blanc")
{ //almost empty
mycode("should start a row earlier on { line");
} //..
else
{
ohno.notagain("another two blanc lines above and below else");
Altough.it.compiles("equaly");
myscreen.used = not.optimal;
}
}
If code is better written, then its nice to read, if something is nice to read with much white lines then the white lines spoil it. So i wonder can this auto-formatting be changed in VS2010 ?
C# as a language has a set of coding conventions which C# programmers expect to be fairly consistent from project to project. If you want to use the One True Brace style, switch to Java.
If you insist on being the odd man out, you can configure the Visual Studio auto-formatter in Tools → Options... → Text Editor → C# → Formatting.
I want to read the constants in C#, these constants are defined in a .h file for C++. Can anyone tell me how to perform this in C# ? The C++ .h file looks like:
#define MYSTRING1 "6.9.24 (32 bit)"
#define MYSTRING2 "6.8.24 (32 bit)"
I want to read this in C# ?
Here is a really simple answer that you can use on each line of your .h file to extract the string value.
string GetConstVal(string line)
{
string[] lineParts = string.Split(line, ' ');
if (lineParts[0] == "#define")
{
return lineParts[2];
}
return null;
}
So any time it returns null, you don't have a constant. Now keep in mind that it is only getting the value of the constant, not the name, but you can easily modify the code to return that as well via out parameter, etc.
If you want to represent other data types, like integers, etc. you will have to think of something clever since macros in C++ don't really count as type safe.
You have two options:
1- Create a C++ wrapper code, which wraps these macros, export this code to a lib or dll and use it from C#.
2- read/parse the .h file from your code, and get the values at run-time.