How to provide User Data in Scan callback function in Java/C#?

The Aerospike Knowledge Base has moved to https://support.aerospike.com. Content on https://discuss.aerospike.com is being migrated to either https://support.aerospike.com or https://docs.aerospike.com. Maintenance on articles stored in this repository ceased on December 31st 2022 and this article may be stale. If you have any questions, please do not hesitate to raise a case via https://support.aerospike.com.

Summary

How can I provide User Data in Scan callback function in Java/C#

Resolution

Since the scan callback is a delegate, you can instantiate your user data class and provide the scan callback method there. Please see the following example in C#:

public class MyScan
	{
		public void scan(AerospikeClient client)
		{
			MyData data = new MyData("string1", 8);
			client.ScanAll(null, "test", "test", data.ScanCallback);
		}
	}

public class MyData
	{
		private string a;
		private int b;

		public MyData(string a, int b)
		{
			this.a = a;
			this.b = b;
		}

	public void ScanCallback(Key key, Record record)
		{
			// Have access to a,b at this point
		}
	}

The scan callback could also be put directly in the same class as the scan in some cases.
See AerospikeDemo/ScanParallel.cs which uses the member variable recordCount in its scan callback.