Assign random value to a variable at compile time - vb.net

My code has a Public variable whose value I want to change every time I compile the Solution. How can I do?
This is actual declaration:
Public AppCode as String = "AABBCCDDEEFFGGHHII"
What I am looking for is something like this, but at compile time:
AppCode = Guid.NewGuid().ToString
Thank you
Danny

Related

Intellij live template to convert variable to constant

I'm trying to make a live template in intellij to convert the following examples into constants, given that I select the input line.
Examples Inputs:
String houseName = "The big house"
int houseNumber = 1
Intended results: (and ideally at the top of the class)
public static final String HOUSE_NAME = "The big house"
public static final int HOUSE_NUMBER = 1
Any ideas?
You are probably looking for the 'Extract Constant' refactoring dialog (Ctrl+Alt+C while on variable declaration).
It gives you options to rename your variable and moves the declaration to the top of the class automatically.

VB NET How to execute some code at the start and at the end of any function

I like to try to optimize my code, and I would like to measure the time taken by a function.
I have a class named Chrono. So I just have to add chrono.start at the beginning of the function, and chrono.stop at the end. My class chan also add the times it measure in a list, to then have average time, total time...
It works. Only problem is when there is exit sub or return in the middle of the function. Not really a problem, I just add a Try at the begginf of the function, and put my chrono.stop in the finally portion. I'm not sure it's really efficient, but it works.
So here is my question : I would like to have a function taking function name as parameter, that will automatically launch and stop my class when this function is called. I have heard of Reflection, but I have no idea how to use it. And it's really hard to search for this question in the internet (because the words are too common : "do something at the end of a function")
To resume, my code works, no problem. It's just constraining to add code to a function for just a short period of time (and sometimes forgot to remove it).
Thx (I'm french and hope I'm understandable)
This is how you can use reflection to call a method by name:
using System.Reflection;
public int InvokeMethod(string name)
{
int time1 = 1; //call your chrono.start here.
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(name);
theMethod.Invoke(this, new object[] { 1 });
int time2 = 10; //call your chrono.end here.
return time2 - time1;
}
However, there is a problem. How will you know what parameters to pass to the function? In the code above, I'm passing the integer 1 (new object[] { 1 }) just for example. So this code cannot be automated, but if you run it manually against each function one by one, then you can change that line to pass the correct arguments and make it work without having to modify your function.
This is to answer your question as how to call a function by name using reflection. However, it is much easier to call it using a delegate (or Fuc<T, tRsult> in .Net v3.5 and higher).

Obiective-C, find global variable at Runtime?

I am curious to know if there is any way to find a global variable at runtime, much like NSClassFromString. The variable, a BOOL, is defined in a static library and I found the name by using "nm" which gave this output: "0001924d b _gStartSessionCalled". When debugging in XCode I can add an expression "gStartSessionCalled" and see the value change as the app is running.
What I want to do is find the value of gStartSessionCalled and also change the value. I know it's kind of weird to do this but please disregard the reason why.
The lowercase letter "b" in the nm output
0001924d b _gStartSessionCalled
indicates that gStartSessionCalled is a local (non-external) symbol. It could for example be defined as
static BOOL gStartSessionCalled;
in your library. As far as I know, you cannot access local symbols from outside the object file in which it they are defined.
The debugger can use the symbol table to find the address and display the variable, but the linker refuses to link against a local symbol from a different object file.
A global variable is not an Objective-C specific construct. It is plain C and you can access every global variable when knowing its name by declaring it like
extern <type> <name>;
e.g. in your case
extern BOOL gStartSessionCalled;
…
gStartSessionCalled = YES;
Update:
If you do not know the name of the variable at compile time, you still may find the symbols address at runtime using something like dlsym. I don't know if it is the same on MacOS as on Linux, but there will be something similar.

In VS2010, how to get VB.NET enum values to autocomplete without typing the enum type first?

In VS2008, you could write, for instance,
dim enumValue as MyEnum
enumValue =
.. and then as soon as you typed the =, you'd get a list of possible values of MyEnum.
With VS2010, you have to type
dim enumValue as MyEnum
enumValue = MyEnum.
... before getting the list on typing the final .
This makes for a lot more typing and seems to be a retrograde step ... is there an option I'm missing? (I have 'Auto List members checked in the Text Editor options under 'Basic').
Yes, you're not the first to be annoyed by this. This looks to be likely to get fixed in SP1 according to this feedback item.

Is it possible to declare a dynamic constant in VB .NET?

I'm trying to save a timestamp into a constant at the beginning of a program's execution to be used throughout the program. For example:
Const TIME_STAMP = Format(Now(), "hhmm")
However, this code generates a compiler error - "Constant expression is required." Does that mean all constants in VB .NET have to contain flat, static, hard-coded data? I know that it's possible to initialize a constant with a dynamic value in other languages (such as Java) - what makes it a constant is that after the initial assignment you can no longer change it. Is there an equivalent in VB .NET?
You need to make it Shared Readonly instead of Const - the latter only applies to compile-time constants. Shared Readonly will still prevent anyone from changing the value.
Java doesn't actually have a concept like Const - it just spots when static final values are actually compile-time constants.
What you are looking for is the readonly keyword.
A time stamp has to be calculated at run time and cannot be constant.
ReadOnly TIME_STAMP As String = Format(Now(), "hhmm")
By definition, constants are not dynamic. If you want a variable to be set once, and not modified again, I believe you are looking for the ReadOnly keyword...
Public Shared ReadOnly TIME_STAMP = Format(Now(), "hhmm")
Note that 'Shared' is optional.