Notifications
Clear all

[Closed] dotnet question – sending null parameter to functions

hi to all whom have experience with dotnet,

I have a simple question which I hope have a simple answer, I wrote a dll for 3dsMax, and want to have the ability to send undefined value (null) to my dotnet functions from maxscript

the return value of dotnet functions converts automatically to undefined but when I send undefined to dotnet function I get the error:


-- Runtime error: No method found which matched argument list

I searched the MSDN but didn’t find answer, can I declare null object/class the same way as I declare any other dotnet value and send it as parameter:

 
stringObj = dotNetObject "System.String" (txt as string)
Int32class = dotNetClass "System.Int32"
DBNullclass = (dotNetClass "System.DBNull")

is it possible to do that without using the SDK?

6 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

i can’t answer your question 100% correct without seeing your c# function … but probably the solution is:
dotNetobject “System.DBNull” undefined
or
dotnet.ValueToDotNetObject undefined (dotNetclass “System.DBNull”)

oh ohoo, if denis can’t pull out the answer immediately, then I’m doomed…

I’ll try to give more information maybe it’ll help find other solution,
say I have a function in C#

 
public void updateUser(string UserName, string fullName, int? someOptionalParameter, bool? active)
{
	 ......
	 .......
}

and I want to be able to call it from maxscript:

 
dotnet.loadAssembly "SomeDBConnection.dll"
 
DB = dotNetObject "SomeNamespace.SomeClass" 
DB.updateUser "blah blah" undefined undefined (dotNetObject "System.Boolean" false) 

I want to be able to send null to the function, that is different from DBNull, DBNull says to the function to delete the information in the database field, while null says to the function leave this field untouched

I tried to send


DB.updateUser "blah blah" (dotNetObject "System.String" undefined) (dotNetObject "System.Int32" undefined) (dotNetObject "System.Boolean" false) 

but that seems to send DBNull to the function, any suggestions?

the better way to handle your functionality is to make several declarations to the function. As I understood if the function gets some undefined params that means these params have to be default.
so, make two or three declarations:
public void updateUser(string UserName, bool active) {}
public void updateUser(string UserName, bool active, string fullName) {}
public void updateUser(string UserName, bool active, string fullName, int Options) {}

now you can call this function with 2 (or 3, or 4) params if it’s necessary.

Thanks denis, The problem is I have lots of functions like that, with sometimes lots of parameters, so I’ll have to write 10 versions for each function to cover all the possibilities, not to mention 10 versions for calling the function from maxscript…ouch…

unfortunatly optional parameters will only be available in the next version of Visual Studio that should be released in 2010, until then we have to be creative…

according to documentation max “undefined” value converts to dotnet Object class.
so in you c# functions you can declare input parameters as Object and check for null and the type of passed parameter.

sample:


 fn getMaxValue = 
(
	source = ""
	source += "using System;
"
	source += "public class MaxValue
"
	source += "{
"
	source += "	public string GetValue(Object input) { return ((input == null) ? \"undefined\" : input.ToString()); }
"
	source += "	public string GetType(Object input) { return ((input == null) ? \"null\" : (input.GetType()).Name); }
"
	source += "}
"

	-- Compile on fly
	csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
	compilerParams.ReferencedAssemblies.Add "System.dll"

	compilerParams.GenerateInMemory = on
	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
	
	compilerResults.CompiledAssembly.CreateInstance "MaxValue"
)
maxValue = getMaxValue()

maxValue.GetValue undefined
maxValue.GetType undefined

maxValue.GetValue ""
maxValue.GetType ""

maxValue.GetValue (dotnetclass "System.Drawing.Color").Red
maxValue.GetType (dotnetclass "System.Drawing.Color").Red

It Works!! you’re a genius!! Thank you! :applause: