Adding elements from vector to listBox in c++ - vector

Im new programmer, learning C++ nearly 15 weeks so far and I'm doing a small project about student database with GUI ( Windows-forms ). I created GUI with Drag n Drop thing in Visual Studio 2015.
I have class Student with 13 variables including getters and setters (Index, First Name,Last name, etc...). I've successfully opened connection with MySQL database for Adding, Deleting and Update students.
And I also wrote objects String^ to pick text from my TextBoxes and then parse them into normal string with msclr/marshal.
Now, problem is when i create object of Student and after adding it into vector with code below:
vector<Student> vektor;
Student *s = new Student(iIndeks, cIme, cPrezime, cDatumRodjenja, cFakultet, cSmer, cEmail, iBrTelefona, cDrzava, cMesto, cUlicaIBroj, cOpstina, iPostanskibr);
vektor.push_back(*s);
Im unable to add elements of that vector into listBox with:
listBox1->Items->Add(vektor);
It says this error:
Function cannot be called with given argument list,
argument types are:
(std::vector<Student, std::allocator<Student>>) object type is:
System::Windows::Forms::ListBox::ObjectCollection ^.
I guess its pretty self-explaining but I have no idea (should I or not) create my class like the ref class or something like that?
Any suggestion what this newbie can do?
Thanks for help.

Related

How to format the Level column value in Serilog’s MS Sql Server Sink

I am trying to use Serilog in my .Net Core API to log to SQL Server with the Serilog-Sinks-MSSqlServer sink. The standard Level column either writes out the full value (e.g. Information, Warning, Error) or a TinyInt enum value if you set the StoreAsEnum property to true as shown here. I cannot seem to find an easy way (like there is in Log4net and NLog) to format the output to write only the first character of the Level (e.g. I, W, E). I tried setting the DataLength property to 1 but that causes the log entry to not be written at all.
I have been able to accomplish my desired behavior with a custom enricher that takes the Level value from the standard column and then uses just the first character to write to a custom column while removing the standard Level column but that really seems like overkill when I feel like there may be a formatting mechanism somewhere that I just haven’t seen.
For Serilog.Sinks.MSSqlServer, it does not expose easy way to custom the Level value.
If you check MSSqlServerSink, you will see that it convert IEnumerable<LogEvent> events to _traits.eventTable by a private method, which means you could not override the method void FillDataTable(IEnumerable<LogEvent> events).
In addition, for MSSqlServerSinkTraits, it is internal, you could not inherit from it to implement your own MSSqlServerSinkTrait.
There is no much thing you could not if you want to override something from Serilog.Sinks.MSSqlServer.
For easiest way, you may consider forking the Serilog.Sinks.MSSqlServer, and change GetStandardColumnNameAndValue from below code
case StandardColumn.Level:
return new KeyValuePair<string, object>(columnOptions.Level.ColumnName, columnOptions.Level.StoreAsEnum ? (object)logEvent.Level : logEvent.Level.ToString());
to change logEvent.Level.ToString() to your expected value like logEvent.Level.ToString().Substring(0,1).
Then build the project and reference this project instead of Serilog.Sinks.MSSqlServer

Accessing math functions with sqlite-net-pcl and Xamarin.Forms

When writing a C# console App, and using System.Data.SQLite, I am able to perform SQL commands such as:
string cosfun = string.Format("UPDATE test SET cosColumn = column1*cos(20));
However, when I try using a similar command in Xamarin.Forms, using the sqlite-net-pcl package, I get the followin error: SQLite.SQLiteException: 'no such function: cos'
I have found a similar question on SO (Custom SQLite functions in Xamarin.iOS). However, I didn't fully understand the response. I now have the following questions:
1) Can I make custom SQL functions using sqlite-net-pcl in Xamarin.Forms? If so, could someone please share a simple (but complete) example of how to do this?
2) Is there anyway for me to access the same math functions (pow, cos, sin, etc.) that I can access when writing console Apps in C#?
3) Is there another way to do this? For example, can I read columns from the database into a List, then perform the required math functions, and feed that back into the database? Would this be a terrible idea with a large database?
Thanks in advance,
Dustin
First is OK.
The SQLite-net PCL by Frank Kreuger is the one that Xamarin University uses in their XAM160 - Working with SQLite and Mobile Data class: https://university.xamarin.com/classes/track/cross-platform-design
Second is Ok.
You can find some documentation on how to get started on the Xamarin developer site: http://developer.xamarin.com/recipes/android/data/databases/sqlite/
Third answer is clear.
More Info:
You can refer to official document in here, Another similar discussion may be helpful for you this.
Correct me if I'm wrong, but what you're trying to do is essentially have two columns where one contains a set of data, and the other contains the result of a simple mathematical operation from the first column. From this you have two columns where one is dependent on the other, which means you are occupying double the necessary memory space. For a 100 entries, that's alright. For 1,000,000? Less so.
I personally thing you are better off not having cosColumn, and you should calculate the cosine when you read the data. For example:
// In your C# code...
public class MyData
{
public double Column1 { get; set; } = 0.0;
public double Cosine => Math.Cos(Column1);
}
In the above, the cosine value is never stored or created in neither C# or SQLite, but it is obtained only when needed. This makes it much more memory-friendly to the SQLite table, and it implements a better SQLite structure.
In the code above, the line:
public double Cosine => Math.Cos(Column1);
is exactly equivalent to:
public double Cosine
{
get
{
return Math.Cos(Column1);
}
}
There's no real difference between the two, and you save a lot of line-space. You can find more information on the => notation from this StackOverflow answer by Alex Booker.
Let's go through an example of implementing this structure. Suppose you have a database with 1 column with the name Column1, and you want to apply a Cosine function to this value and display it. Your code might look like:
// Read from database object of type MyData
MyData data = ReadOneValueFromDatabase<MyData>();
// Display values in a label
MyValueLabel.Text = "Database value: " + data.Column1.ToString();
MyCosineLabel.Text = "Cosine value: " + data.Cosine.ToString();
The object data will store the value of Column1 from the database in Column1, but not Cosine. The value of Cosine is only obtained when you call data.Cosine.

Weird problem with a custom bindable class and a vector

I'm having a very weird problem with a vector in my application.
Details...
I have the following classes.
Person,Player,PlayerController.
Player extends Person. Person extends ObjectProxy in order to enable binding.
So the Player class has the [Bindable] tag.
The PlayerController class contains a remote object calling a php method to receive a firstname and a lastname and when the CallResponder gets the result from the call,the result handler creates a Player instance. At that moment I am trying to push the player object into a Vector..
The problem is the following.
Every time the push method is called, the vector is being populated with the last player that was created but not just in the end of the vector. It replaces the other instances as well! So the vector always contains the most recent player instance but in every position of it. :S
I have also tried doing it with an Array and the results are the same.
Any thoughts on what I'm doing wrong? It's driving me crazy. :S
My guess is that you are pushing the same object reference into your vector after setting that reference to a new instance of Player, meaning that all of the items in your vector refer to the same object, which is always the newest object. I say "guess" because I haven't seen your code. What are you pushing into your vector, a local variable? A member variable?
Edit: Based on your comment below, try adding your new Player object to your vector using a local variable rather than from your member variable (player_):
var newPlayer:Player = new Player();
newPlayer.firstName = results[firstName];
newPlayer.lastName = results[lastName];
players_.push(newPlayer);
player_ = newPlayer;
You are doing what I suspected, which is adding multiple references to the same object to your vector. Since all of the references in your object refer to the same object, changing the one object changes ALL of the entries in your vector. Doing the above will create a brand new (and unique) Player object each time you add to your vector.

In Qt, create a table with an blank editable row

This is a Qt-specific question.
It's convenient to be able to add new data to a table by typing content into a blank row at the bottom of a table. When the data is committed, a new blank row is added to the table.
Has anyone found a way of implementing this in a generic way, that fits into Qt's model-view programming architecture? My closest attempt involves creating a proxy model, such that the rowCount() returned from the model is always one greater than the source model.
QAbstractTableModel* sourceModel ; // Data is stored here
QBlankRowModel* model ; // Proxy model that adds one to rowCount()
QTableView* view ; // View
view->setModel( model ) ;
model->setSourceModel( sourceModel ) ;
Any suggestions are welcome. Thanks.
From a design-perspective, this should be part of the view, not the model. Therefore, I suggest implementing a view with the functionality and leave the model unchanged. KOffice Kexi does just this with kexitableview (screenshot, documentation). Maybe you want to use some of their code.
BTW, you might still be able to use your hack and combine it with my suggestion by putting it inside a new table view implementation YourTableView:
QBlankRowModel re-implements the
QAbstractTableModel
interface. It returns sourceModel.rowCount()+1 as the QBlankRowModel::rowCount().
It returns a QVariant() if the n+1th row is requested in QBlankRowModel::data().
All the rest within QBlankRowModel is forwarded to the sourceModel (with editing
the n+1th row in QBlankRowModel buffered and replaced with inserting into the
sourceModel when finished).
The new YourTableView inherits from
QTableView and wraps the sourceModel within
YourTableView::setModel(), calling
QTableView::setModel(QBlankRowModel(sourceModel)).
Thereby, your hack is localized at one point.
Your solutions seems a little hackish. Your problem is not only additions, it's also editions. What happens when your user edits a row, the typed data goes directly to your "data layer" even before the user commits his edition?
A better solution would be to restrict the role of your sourceModel. Rather than being a "direct" representation of your data, it should be a "buffered" representation of it. When the sourceModel is created, you make a copy of your data in some kind of Row() instances. The sourceModel, having its own copy of the data can then freely play around, perform editions and additions, and only commit the data to your model layer when the user commits his edits.
If you want a PyQt example of such a table, you can look at the source of a project of mine:
http://hg.hardcoded.net/moneyguru/
You might have to dig around to actually find the "buffering" logic because it's not in the PyQt code itself, but rather the "cross-platform" part of the code:
http://hg.hardcoded.net/moneyguru/src/tip/core/gui/table.py
This logic is then used in my QAbstractItemModel subclass:
http://hg.hardcoded.net/moneyguru/src/tip/qt/controller/table.py
Sounds like a reasonable solution, as it should work for any model that you might want as the actual table model, ie. SqlTableModel or just a plain one. As long as you add the row when the user is done editing and take care not to add the row when the user did not add any data.

How to get a LPITEMIDLIST pointer according to the path of a folder?

I want to get the system icon of a specified folder, but maybe the only way to retrieve the icon is to use SHGetFileInfo() method. The first parameter of SHGetFileInfo() method is a pointer of LPITEMIDLIST.
If I only have the absolute path of the folder, how can I get the pointer according to the path?
SHParseDisplayName().
Welcome to the wonderful world of PIDLs.
You can read more at Introduction to the Shell Namespace, but basically a PIDL is a Pointer to an item ID List. You can think of it as a linked list in contiguous memory, but instead of each node having a pointer to the next node, you instead have the cb member which is the Count of Bytes that are contained the item, so you can add that to the base address to get the next item. IDLists are terminated with an item with { cb = 0, abID = NULL }.
So, what's in these magic lits? Basically you don't care and can't know. Any IShellFolder implementation can create a new type of ID to represent its type of item in the shell namespace. The basic file system view that the Shell implements just stores the parts of the path in these lists, so you have something like "c:\" in the first one "Users\" in the next one, etc. In reality they are serialized structs (or classes) that may contain more data. But they can also represent printers, network shares, database searches (for search folders, stacks, etc).
All you really need to know is you can ask IShellFolders to give you a PIDL that represents the items they contain, and later on you can give that PIDL back to them, and other various Shell functions and interfaces, and they know how to deal with them. What SHParseDisplayName() basically does (I think) is go through the registry looking for all registered IShellFolder implementations and asks them if they know what to do with the string you pass in, and the first one to handle it makes the PIDL and gives it back.

Resources