GitHub    Download    Forum
Overview
Tutorial
Database setup
Preparing the project
Establish the connection
Shut down the connection
DTDL types
Creating the schema
DADL classes
Entry point object
Write and read attributes
Write and read lists
Extending the schema
Modified entry point object
Write and read objects
Write and read object lists
C++ API
C# API
DTDL
DADL
Setup
C++
C#

Write and read lists

To write to the list, we use the generated "Set" function. This function opens the list for writing and reading. The "Get" function on the other side would open the list for reading only.
The other difference between "Set" and "Get" functions is that a "Set" function would create a new list if there is not already one available, while "Get" would not.
ShopList* pList;
pSupplies->SetShops(&pList);
ShopList pList;
pSupplies.SetShops(out pList);
Adding and getting the list's content is done using the generated item structs. See UListUList for more information about the functions.
ShopListItem Item;
Item.ShopName = L"the name of the shop";
Item.ShopURL = L"http://www.theshop.com";
pList->Insert(NULL, &Item);

const ShopListItem* pItem;
pList->Get(0, &pItem);
ShopListItem Item = new ShopListItem();
Item.ShopName = "the name of the shop";
Item.ShopURL = "http://www.theshop.com";
uint vl;
pList.Insert(out vl, Item);

pList.Get(0, out Item);
It is important to keep in mind that the indexes returned or used by list functions are only valid until the list in the memory gets modified or reloaded, and that they are only valid in your current local runtime copy. They have no meaning on the server or for other clients.
So our 'writeList' and 'readList' functions might look like this:
writeList.cpp
writeList.cs
#include "pch.h"

void writeList(WSupplies* pSupplies)
{
	// Shops
	ShopList* pList;
	pSupplies->SetShops(&pList);

	ShopListItem Item;

	// ShopName
	wprintf(L"Shop name:\n");
	std::wstring input1;
	getline(std::wcin, input1);
	Item.ShopName = input1.c_str();

	// ShopURL
	wprintf(L"Shop URL:\n");
	std::wstring input2;
	getline(std::wcin, input2);
	Item.ShopURL = input2.c_str();

	pList->Insert(NULL, &Item);

	// Store
	pSupplies->Store();	// we added an item to the list

	// Execute
	HRESULT hRes;
	if(FAILED(hRes = pSupplies->GetDomain()->Execute(Transaction::Store)))
	{
		wprintf(L"Domain failed to execute the transaction (0x%x)", hRes);
	}
}
using System;
using DataFoundationAccess;

namespace TutorialCs
{
partial class Tutorial
{
	public static void writeList(WSupplies pSupplies)
	{
		// Shops
		ShopList pList;
		pSupplies.SetShops(out pList);

		ShopListItem Item = new ShopListItem();

		// ShopName
		Console.WriteLine("Shop name:");
		string input1 = Console.ReadLine();
		Item.ShopName = input1;

		// ShopURL
		Console.WriteLine("Shop URL:");
		string input2 = Console.ReadLine();
		Item.ShopName = input2;

		uint _i;
		pList.Insert(out _i, Item);

		// Store
		pSupplies.Store(Transaction.Store); // we added an item to the list

		// Execute
		int hRes;
		if(0 > (hRes = pSupplies.GetDomain().Execute(Transaction.Store)))
		{
			Console.WriteLine("Domain failed to execute the transaction (0x{0:x})", hRes);
		}
	}
}
}

readList.cpp
readList.cs
#include "pch.h"

void readList(WSupplies* pSupplies)
{
	HRESULT hRes;

	const ShopList* pList;
	if(S_OK == (hRes = pSupplies->GetShops(&pList)))
	{
		wprintf(L"All ShopList items (%i)\n\n", pList->GetLength());

		for(UINT32 i = 0; i < pList->GetLength(); i++)
		{
			const ShopListItem* Item;
			pList->Get(i, &Item);
			wprintf(L"Shop %i\n", i);
			wprintf(L"Shop Name: %s\n", Item->ShopName);
			wprintf(L"Shop Url: %s\n\n", Item->ShopURL);
		}
	}
	else
		wprintf(L"Cannot read shop list (0x%x)\n", hRes);
}
using System;
using DataFoundationAccess;

namespace TutorialCs
{
partial class Tutorial
{
	public static void readList(WSupplies pSupplies)
	{
		int hRes;

		ShopList pList;
		if (0 == (hRes = pSupplies.GetShops(out pList)))
		{
			Console.WriteLine("All ShopList items ({0})\n", pList.GetLength());

			for (UInt32 i = 0; i < pList.GetLength(); i++)
			{
				ShopListItem Item;
				pList.Get(i, out Item);
				Console.WriteLine("Shop {0:F}", i);
				Console.WriteLine("Shop Name: {0}", Item.ShopName);
				Console.WriteLine("Shop URL: {0}\n", Item.ShopURL);
			}
		}
		else
			Console.WriteLine("Cannot read shop list (0x{0:x})", hRes);
	}
}
}
© 2022 Mobiland AG