How to set a required reasource location as a variable? - vb.net

I'm currently making a game for an Advanced Higher computing course. After having my previous question answered perfectly and helping me a great bunch. I was wondering if you could share you wisdom upon me once again...
I've currently declared the variables, got the selection process narrowed down. All I'm sturggling to do is for it to recognise it's a variable and not the location itself.
UsedTile = My.Resources.Token(Turn)
CurrentTile = My.Resources.Colour(Turn)
I should also mention that UsedTile and CurrentTile are declared as Image so that it works with the rest of my progam.
Token is an array that swaps between 1 & 2 which is defined by Turn.
Sorry if i'm explaining this baddly, but basically I would want it to look something like
UsedTile = My.Resources.(Colour(Turn))
So that it is interchangable. All of the reasources are there, so for instance it would be a red tile it should show as
UsedTile = My.Resources.ColourRed.png
Thanks, I hope I've made it understandable. I'd happily upload more code if needed :)
-Lewis

It's a little unclear what it is that you are asking, but here are two suggestions that may help you. First, you may want to consider reading the resources once and storing them in some other data structure that makes them more convenient to access when you need them later. For instance, if you created a class like this:
Public Class ColourResources
Public Property TurnImages As Image(2)
End Class
Then you could create an array of them and fill them like this:
Dim colors(2) As ColourResources
colors(0).TurnImages(0) = My.Resources.ColourRed
colors(0).TurnImages(1) = My.Resources.ColourRedUsed
colors(1).TurnImages(0) = My.Resources.ColourBlack
colors(1).TurnImages(1) = My.Resources.ColourBlackUsed
Then when you need a particular image, you could just access in some way similar to this:
Dim tile As Image = colors(currentColor).TurnImages(currentTurn)
If your colors and turns are kept track of with some data type other than an Integer, you could use dictionaries instead of arrays.
My second suggestion is that it is possible to get the resource by string name rather than via resource-designer-auto-generated property. For instance, instead of this:
UsedTile = My.Resources.ColourRed
You could also access the same image like this:
UsedTile = DirectCast(My.Resources.ResourceManager.GetObject("ColourRed"), Image)
Depending on your needs, that may be useful to you.

Related

Need Help Creating Custom Sorter On ObjectListView

Ok, I'm totally at a loss with this....
I think I have looked at every example and every code snippet around and still can't work out how to implement a custom sorter on my ObjectListView.
My primary column (column 0) contains numbers only (as a string) but is sorting all items by alphanumeric instead of numeric, meaning that it is doing something like this:
1
11
111
2
22
etc.
I am trying to find a relatively simple example of how to modify my ObjectListView to sort column 0 when it first loads, but I'm struggling.
I have converted over a custom class called ColumnSorter (from CodeProject) into VB and I'm calling the following delegate:
lvwColumnSorter = New CustomLVSorter.CustomLVSorter()
lsv_OpenTickets.CustomSorter = Sub(column As OLVColumn, order As SortOrder)
lvwColumnSorter.ColumnToSort = Ticket_Status.Index
lvwColumnSorter._SortModifier = CustomLVSorter.CustomLVSorter.SortModifiers.SortByText
lvwColumnSorter.OrderOfSort = SortOrder.Ascending
lsv_OpenTickets.ListViewItemSorter = lvwColumnSorter
End Sub
I get no errors, but I also get no change.
Any help would be greatly appreciated.
Well, are you sure you have looked at every example? I think there are a lot of resources on this one.
When you're using a list, datagridview, or any main form, you can adjust it to use a custom sorter. You create a custom IComparer, i.e. the definition of how you sort something. It can be as simple as converting the string (like yours) to an int with CInt() and returning -1 or +1 if it is greater or less than the last value. This is very common.
If you need help on the basics of how to do it, of course there are always the microsoft links that give you the basics such as Custom Sort I Comparer. But there is a stack flow that also follow your problem here: Custom sort C#
It's in C#, but there are many converters on that around here.
But the easiest way to get around it? Convert your string list into a integer list. Then it will sort perfectly.

How to use _root and this[] in same line?

Here's a chunk of code that WORKS when it's on the main timeline:
var DysonTarget:String = "S"+(random(40)+1);
this[DysonTarget].MyType = "Dyson Sphere";
this[DysonTarget].gotoAndStop(this[DysonTarget].MyType);
It's choosing a number between 1 and 40, adding an S before it, and going into one of forty movie clips on the main stage with instance name S1, S2. . . S40 etc. Then it will display an image in the chosen clip. But to make this truly work the way I want to, I have to put the above code inside a movie clip. So I tried this, after declaring my variable on the main stage:
_root.this[DysonTarget].MyType = "Dyson Sphere";
_root.this[DysonTarget].gotoAndStop(this[DysonTarget].MyType);
It didn't compile, the error message said "Expected a field name after the '.' operator. Trying it with _parent returned the same message. With _level0 didn't work at all, and placing the _root and _parent inside the bracket didn't work either. I haven't been able to find any answer online because trying to type "this" into a search is too vague to return an answer about the actual command.
. . .help me :(
A friend of mine who is a software developer helped me on this one. Here's what we figured out:
First you declare variable DysonTarget on the main timeline:
var DysonTarget:String = "S" + (random(40)+1);
Then inside the movie clip, use this:
_level0[DysonTarget].MyType = "Dyson Sphere';
_level0[_level0.DysonTarget].gotoAndStop(_level0[_level0.DysonTarget].MyType);
I've tried this a few other ways, and the above method is the only one that works the way it's supposed to. But it works! My impression is the brackets tell it to look for an object named what the variable is set to, rather than an object with the same name as the variable.

Changing the color of a leader in AutoCad

I'm currently working on converting a VBA AutoCAD-application over to VB.NET, and the current command I'm working on is creating a simple leader with code like this:
Set leaderObj = ThisDrawing.ModelSpace.AddLeader(points, blockRefObj, leaderType)
leaderObj.ArrowheadType = acArrowDotSmall
leaderObj.ArrowheadSize = 2.5 * varDimscale
leaderObj.DimensionLineColor = acWhite
I've been able to create the Leader-line in .NET using
Dim l = New Leader()
For Each point In jig.LeaderPoints
l.AppendVertex(point)
Next
l.Dimldrblk = arrId
The arrId I got from using the function found here, but I've been unable to figure out how to set the color of the leader to white (it shows up as red by default), and also how to set the size of the arrowhead. If anyone could help me out with this I would be most grateful.
Ok, after a lot of trial and error, I figured out that the solution was rather simple. I didn't have to override any dimension styles (which I honestly don't even know what is, I had a short beginners course in AutoCAD before getting handed this project), I simply had to set an obscure property on the Leader-object. For future references, and for anyone else trying to do the same, here's the properties I ended up using:
leader.Dimclrd
The color of the leader-line. Stands for something like "dimension line color".
leader.Dimasz
The scale of the leader-head.
As type BlockReference, it should have a color property and the property should be an Autodesk.Autocad.Colors.Color or an Integer. Also the reason you are getting the object for read is, in your transaction you are opening the database with
OpenMode.ForRead
And that is correct. But to edit the object in the database, you must retrieve the object like below
var obj = Thetransaction.GetObject(theobjectid,OpenMode.ForWrite) as BlockReferance;
This is done inside of the
using(var trans = TransactionManager.StartTransaction()){}
I'm doing this on a cell, so check the camel case and syntax because I write in c#, but it should be pretty close.
You may want to see if there is a scale property, as to change the size.
Hopefully this will move you in the right direction.
Let me know if you have any problems. :)

How to comment on MATLAB variables

When I´m using MATLAB, sometimes I feel the need to make comments on some variables. I would like to save these comments inside these variables. So when I have to work with many variables in the workspace, and I forget the context of some of these variables I could read the comments I put in every one of them. So I would like to comment variables and keep the comments inside of them.
While I'm of the opinion that the best (and easiest) approach would be to make your variables self-documenting by giving them descriptive names, there is actually a way for you to do what you want using the object-oriented aspects of MATLAB. Specifically, you can create a new class which subclasses a built-in class so that it has an additional property describing the variable.
In fact, there is an example in the documentation that does exactly what you want. It creates a new class ExtendDouble that behaves just like a double except that it has a DataString property attached to it which describes the data in the variable. Using this subclass, you can do things like the following:
N = ExtendDouble(10,'The number of data points')
N =
The number of data points
10
and N could be used in expressions just as any double value would. Using this example subclass as a template, you could create "commented" versions of other built-in numeric classes, with the exception of those you are not allowed to subclass (char, cell, struct, and function_handle).
Of course, it should be noted that instead of using the ExtendDouble class like I did in the above example, I could instead define my variable like so:
nDataPoints = 10;
which makes the variable self-documenting, albeit with a little more typing needed. ;)
How about declaring another variable for your comments?
example:
\>> num = 5;
\>> numc = 'This is a number that contains 5';
\>> whos
...
This is my first post in StackOverflow. Thanks.
A convenient way to solve this is to have a function that does the storing and displaying of comments for you, i.e. something like the function below that will pop open a dialog box if you call it with comments('myVar') to allow you to enter new (or read/update previous) comments to variable (or function, or co-worker) labeled myVar.
Note that the comments will not be available in your next Matlab session. To make this happen, you have to add save/load functionality to comments (i.e. every time you change anything, you write to a file, and any time you start the function and database is empty, you load the file if possible).
function comments(name)
%COMMENTS stores comments for a matlab session
%
% comments(name) adds or updates a comment stored with the label "name"
%
% comments prints all the current comments
%# database is a n-by-2 cell array with {label, comment}
persistent database
%# check input and decide what to do
if nargin < 1 || isempty(name)
printDatabase;
else
updateDatabase;
end
function printDatabase
%# prints the database
if isempty(database)
fprintf('no comments stored yet\n')
else
for i=1:size(database,1)
fprintf('%20s : %s\n',database{i,1},database{i,2});
end
end
end
function updateDatabase
%# updates the database
%# check whether there is already a comment
if size(database,1) > 0 && any(strcmp(name,database(:,1)))
idx = strcmp(name,database(:,1));
comment = database(idx,2);
else
idx = size(database,1)+1;
comment = {''};
end
%# ask for new/updated comment
comment = inputdlg(sprintf('please enter comment for %s',name),'add comment',...
5,comment);
if ~isempty(comment)
database{idx,1} = name;
database(idx,2) = comment;
end
end
end
Always always always keep the Matlab editor open with a script documenting what you do. That is, variable assignments and calculations.
Only exceptions are very short sessions where you want to experiment. Once you have something -- add it to the file (It's also easier to cut and paste when you can see your entire history).
This way you can always start over. Just clear all and rerun the script. You never have random temporaries floating around in your workspace.
Eventually, when you are finished, you will also have something that is close to 'deliverable'.
Have you thought of using structures (or cells, although structures would require extra memory use)?
'>> dataset1.numerical=5;
'>> dataset1.comment='This is the dataset that contains 5';
dataset1 =
numerical: 5
comment: 'This is the dataset that contains 5'

QTP, access to QC field by label

I want to update a custom user field in QC using the Label of field instead of the name
At the moment we are doing it this way
Set currentRun = QCUtil.CurrentRun
currentRun.Field("RN_USER_03") = 1
currentRun.Post
But I would like to do it this way
Set currentRun = QCUtil.CurrentRun
currentRun.Field("Data Rows Passed") = 4
currentRun.Post
But I can't find the method to do it with.
Any Ideas?
Implying all labels are unique (which I doubt..):
You could create a function which accepts a label, searches in QC's tables that define customized fields for the correct field definition, and returns the field name. Then use the function's result value as the indexed property's index.
Suppose that function would be called "GetNameOfLabel", then the Caller's code would look like:
Set currentRun = QCUtil.CurrentRun
currentRun.Field(GetNameOfLabel ("Data Rows Passed")) = 1
currentRun.Post
Of course, the function would not really be trivial, but easy enough after some digging in the QC data model and finding an efficient way to fetch the name from the DB via SQL.
Or, the function could look up the name in an array, or a dictionary, then you would have to maintain that dictionary, but you would not have to go to the database for each lookup.
Disadventages:
Scripts with the wrong label might be harder to be debug
If labels are not unique, it might be real "fun" to debug
If looking up on the DB:
All scripts slow down if you don't cache, or pre-load, SQL query results for those lookups;
complexity, as you have to do the right SQL query, and you depend on QC's data model in a quite peculiar way (usually a horror when you're upgrading)
If looking up in an array, or dictionary:
You either must maintain its initialization (bet other admin guys adding a cust field will forget that easily), or must "load" it from QC's table (which is a bit like the SQL solution above, and has the same downsides).
I'd go with the array/dictionary-initialized-from-db-idea. Or, if you can live with the constant idea already presented, that one is a good bet. Considering that there is no session-independent scope in QCs customizing scripts, the SQL access idea might really kill performance because it would have to be executed for every new user session. Which is why I, too, +1'd the constant idea.
Look at this:
Dim gFieldLabelToNameDICT: Set gFieldLabelToNameDICT = CreateObject("Scripting.Dictionary")
gFieldLabelToNameDICT.CompareMode = vbTextCompare
Function GetNameOfLabel (strFieldLabel)
' If it doesn't exist yet in fieldLabelToName dict -> search it using TDC and add it to the list to improve performance
If Not gFieldLabelToNameDICT.Exists(strFieldLabel) Then
Dim testSetFields As List
Dim testSetFields: Set testSetFields = QCUtil.QCConnection.Customization.Fields.Fields("RUN")
For Each aField in testSetFields
If aField.UserLabel = strFieldLabel Then
gFieldLabelToNameDICT.Item(strFieldLabel) = aField.ColumnName
End If
Next aField
End If
GetNameOfLabel = gFieldLabelToNameDICT.Item(strFieldLabel)
End Function
Maybe you shall want to add some more error handling, such us considering the case that the label is not found.