VB.NET Webforms application: Sharing global variables between code and SQL - sql

I'm working on some old webforms applications. We have some clients and we currently store some of the info in a global variables class like the next:
Public Class globals
Enum Enterprise
Enterprise1 = 10
Enterprise2 = 11
...
End Enum
Enum Brand
Brand1 = 5
Brand2 = 6
Brand3 = 7
...
End Enum
...
End Class
The fact is that, even we use parameters in all SQL SPs, we have some common SPs where we have exceptions for certain clients, so you will find SPs with code like:
if (id_enterprise = 10)
begin
... do whatever
end
And as you see this "10" it is hard-coded so, it's not maintainable and thus, global variables are not totally global.
I'm not so sure about the most efficient way to deal with this, ending with completely global variables, but one possible solution that have come to my mind is to have a table with global variables stored in it, and in any -not defined yet- way using that values as global sql variables that can be also shared with code.
Regarding the code, on app startup I would read that table (or SP or whatever it ends being) and store read variables in app global variables (only once, of course).
What do you think about it? Does it make any sense? Any other better implementation for completely global variables that can be shared between code and SQL? FYI, we work with Azure, maybe an Azure alternative?

Interesting issue here. I would recommend just creating a session table in your Azure SQL database as you have suggested, then keep just a single integer/bigint variable as a session variable (ie. Session("user_session_id")). This will minimize the footprint of the application and avoid the overflow issue that you are wary of. Load the table entries at the start of the session. In each context, look up only the parameters required to complete any particular task.

Related

Creating a local variable instead of calling a method to get data

My question is about the efficient code. Please let me know which is the efficient approach among the given below.
There's a method call to get an object. For eg.,
relationship.getCommerceItem()
But, we need to call this method multiple times in a single line itself. So, I'm planning to create a local variable to replace the method call and store the return value. Like given below.
commerceItem = relationship.getCommerceItem()
Now, which approach is more efficient and why?
Considering that this code will be executed in an environment where thousands and thousands of requests will be received.
It depends on whether or not the logic executed in the called function needs to be ran every time. In other words, does the return value change?
If not, saving it in a variable saves you the resources needed for the function call (which is the most sane thing to do IMO).

Retrieving records and manipulating them as Ruby objects

New to Sequel and SQL in general, so bear with me. I'm using Sequel's many_through_many plugin and I retrieve resources that are indirectly associated with particular tasks through groups, via a groups_tasks join table and a groups_resources join table. Then when I query task.resource on a Task dataset I get resource objects in Ruby, like so:
>>[#<Resource #values={:id=>2, :group_id=>nil, :display_name=>"some_name"}>, #<Resource #values={:id=>3, :group_id=>nil, :display_name=>"some_other_name"}>]
Now, I want to be able to add a new instance variable, schedule to these resource objects and do work on it in Ruby. However, every time I query task.resources for each task, Sequel is bringing resources objects in to ruby as different resource objects each time (which makes sense), despite being the same record in the database:
>>
"T3"
#<Resource:0x007fd4ca0c6fd8>
#<Resource:0x007fd4ca0c6920>
#<Resource:0x007fd4ca0c60d8>
#<Resource:0x007fd4ca0c57a0>
"T1"
#<Resource:0x007fd4ca0a4c08>
#<Resource:0x007fd4ca097f58>
#<Resource:0x007fd4ca097b48>
"T2"
#<Resource:0x007fd4ca085ba0>
#<Resource:0x007fd4ca0850d8>
I had wanted to just put a setter in class Resource and do resource.schedule = Schedule.new, but since they're all different objects, each resource is going to have a ton of different schedules. What's the most straightforward way to manipulate these resource objects client side, but maintain their task associations that I query from the server?
If I am understanding your question correctly, you want to retrieve Resource objects and then manipulate some attribute named schedule. I am not very familiar with Sequel, but looking over the docs it seems to work similarly to ActiveRecord.
Set up your instance variable (I imagine using something like attr_accessor :schedule on the Resource class).
Store the records in a variable, you will be working with same instance each time, rather than the new instance Sequel returns.

How can I speed up my Microsoft Project VBA code?

I have a macro I use for Microsoft Project that loops through each task in the project, and performs several checks to find any problems with the tasks. These checks include several IF and Select Case statements. When dealing with large projects with more tasks, the macro can get lengthy. Is there anything I can do to improve the speed up the macro? I have already turned off screen updating and manual calculation.
Turning off screen updating and setting calculation mode to Manual are the only application settings you can use to improve performance; the rest depends on your algorithm.
Your description of the problem is a bit vague: How large are your projects and how long does the macro take? If your projects are 1,000 tasks and you are making a dozen checks and your code takes more than five minutes, then yes, there is surely room for improvement. But if it's 20,000 tasks and 50 checks and the macro takes two minutes, stop trying to improve it--that's great performance.
Bottom line: it is impossible to tell if there is room for improvement without seeing your code.
If you use the same property (e.g. objTask.Start) in several different comparisons in your code then set the property into a local variable once and then perform your comparisons on the local variable.
For example:
Slow code:
If objTask.start < TestDate1 and objTask.Start > TestDate2 then ...
Fast code:
Define dteStart as Date
dteStart = objTask.Start
if dteStart < TestDate1 and dteStart > testdate2 then ...
Calls to the COM object model are expensive. The second code example will be quite a bit faster (although as noted by Rachel above) it really does depend on the volume of data being processed.
Also, make sure you define your variables with appropriate types as relying on the default Variant data type is very slow.
if you have some variables with lot of data like collections think about setting it to nothing and the end of your function
Set TasksCollection=Nothing

Additional PlanningEntity in CloudBalancing - bounded-space situation

I successfully amended the nice CloudBalancing example to include the fact that I may only have a limited number of computers open at any given time (thanx optaplanner team - easy to do). I believe this is referred to as a bounded-space problem. It works dandy.
The processes come in groupwise, say 20 processes in a given order per group. I would like to amend the example to have optaplanner also change the order of these groups (not the processes within one group). I have therefore added a class ProcessGroup in the domain with a member List<Process>, the instances of ProcessGroup being stored in a List<ProcessGroup>. The desired optimisation would shuffle the members of this List, causing the instances of ProcessGroup to be placed at different indices of the List List<ProcessGroup>. The index of ProcessGroup should be ProcessGroup.index.
The documentation states that "if in doubt, the planning entity is the many side of the many-to-one relationsship." This would mean that ProcessGroup is the planning entity, the member index being a planning variable, getting assigned to (hopefully) different integers. After every new assignment of indices, I would have to resort the list List<ProcessGroup in ascending order of ProcessGroup.index. This seems very odd and cumbersome. Any better ideas?
Thank you in advance!
Philip.
The current design has a few disadvantages:
It requires 2 (genuine) entity classes (each with 1 planning variable): probably increases search space (= longer to solve, more difficult to find a good or even feasible solution) + it increases configuration complexity. Don't use multiple genuine entity classes if you can avoid it reasonably.
That Integer variable of GroupProcess need to be all different and somehow sequential. That smelled like a chained planning variable (see docs about chained variables and Vehicle Routing example), in which case the entire problem could be represented as a simple VRP with just 1 variable, but does that really apply here?
Train of thought: there's something off in this model:
ProcessGroup has in Integer variable: What does that Integer represent? Shouldn't that Integer variable be on Process instead? Are you ordering Processes or ProcessGroups? If it should be on Process instead, then both Process's variables can be replaced by a chained variable (like VRP) which will be far more efficient.
ProcessGroup has a list of Processes, but that a problem property: which means it doesn't change during planning. I suspect that's correct for your use case, but do assert it.
If none of the reasoning above applies (which would surprise me) than the original model might be valid nonetheless :)

How to retrieve address of variable in vb.net?

How can we fetch variable address where it is stored in memory ?
`
Sub Main()
Dim a As Integer
a = 10
System.Console.WriteLine("Value of a is : {0}", a)
System.Console.WriteLine("Address of a is : {0}", AddressOf(a))
System.Console.ReadKey()
End Sub
`
Here AddressOf is require function name but i want to get address of integer variable.
we dont. so we cant
in dotnet and java world, the concept of address of a variable is not the same as in C. the way address-of-a-variable is taught is old. the java/dot-net languages are way too advanced. of course the variable exists in memory but the address may not be what you think. so while working in these languages we do not usually consider the address aspect. that is considered only in context of C/C++.
now for why we do not consider the address:
while programming in these languages we do not need it. we need it when working with C, but not when working with java/dot-net. these languages do not need pointers (as in C). linked-lists, arrays, dynamically MALLOCed memory etc, all can be achieved without any need to know the memory address.
the address is not constant. it can change. it can change without any obvious reason from programmer's perspective. it can change at any time. it can change to some unpredictable location. these languages are designed to fully utilize the available RAM and so they move around the data. hence we cannot definitely say exactly where in memory it exists.
these languages are designed to work in virtual-memory machines. what this means is if you do somehow manage to get the address of a variable, that number may be far from real. the operating system might assign same address number to two variables in two programs running in two instances of visual studio all at the same time. again that address will not be of much use.
the real address of the variable does not contain just the data of the variable. if you do manage to get the real address of a variable and you go there, you will find a lot of data, not just your data but a lot of other data too! this other data consists of book keeping data. unlike C where a 7-character string occupies exactly 7 (or 8) bytes of memory, in java/dot net it might occupy about 30 bytes (or more)!!!.
so these are some of the reasons why we do not usually go after the memory address of a variable when working in java/dot-net.