C
C#16mo ago
r2d25551

❔ Class with Func<> and class initializers

If I define a class with members to be passed to known create type functions how do I set the Func<> member to the known function? For example,
public class Known
{
public string param1;
public string param2;
public IRet myObj;
public Func<string, string, IRet> Create;
}

Dictionay<string, Known> myData = new Dictionary<string, Known>()
{
{
"one", // dictionary key
{
"abc", // func parameters
"xyz",
null, // func returrn value

CreateFunc, // PROBLEM HERE?
}
}
};


public static IRet CreateFunc(string param1, string param2)
{
IRet iret = new IRet(param1, param2);
return iret;
}
public class Known
{
public string param1;
public string param2;
public IRet myObj;
public Func<string, string, IRet> Create;
}

Dictionay<string, Known> myData = new Dictionary<string, Known>()
{
{
"one", // dictionary key
{
"abc", // func parameters
"xyz",
null, // func returrn value

CreateFunc, // PROBLEM HERE?
}
}
};


public static IRet CreateFunc(string param1, string param2)
{
IRet iret = new IRet(param1, param2);
return iret;
}
Thank you in advance.
50 Replies
Angius
Angius16mo ago
Besides public fields, the code seems fine What is the PROBLEM HERE??
r2d25551
r2d25551OP16mo ago
Thank you for the reponse. Let me look at it. . . Sorry, around this expression the compiler is compainong about :
error CS1525: Invalid expression term '{'
error CS1003: Syntax error, ',' expected
error CS1525: Invalid expression term '{'
error CS1003: Syntax error, ',' expected
Right at the comma at the end of "one",
Angius
Angius16mo ago
Probably because of the missing new Known or at least a new C# is not Javascript
r2d25551
r2d25551OP16mo ago
And I just tried adding that too
Angius
Angius16mo ago
To create a new Known it would either need a) a constructor b) properties instead of fields
r2d25551
r2d25551OP16mo ago
It was complaining so I removed it Adding constructor now . .
Angius
Angius16mo ago
new() {
Param1 = "abc",
Param2 = "xyz",
MyObj = null,
Create = CreateFunc
}
new() {
Param1 = "abc",
Param2 = "xyz",
MyObj = null,
Create = CreateFunc
}
would work, if you used properties instead of fields
new (
"abc",
"xyz",
null,
CreaeFunc
)
new (
"abc",
"xyz",
null,
CreaeFunc
)
would work, if you had a constructor
r2d25551
r2d25551OP16mo ago
tried too earlier, let me go back to it.
Angius
Angius16mo ago
The first method didn't work because you have public fields Instead of public properties
Thinker
Thinker16mo ago
(new() {...})
MODiX
MODiX16mo ago
Angius
REPL Result: Success
class Foo {
public int Bar { get; set; }
}
new { Bar = 69 }
class Foo {
public int Bar { get; set; }
}
new { Bar = 69 }
Result: <>f__AnonymousType0#1<int>
{
"bar": 69
}
{
"bar": 69
}
Compile: 482.180ms | Execution: 115.355ms | React with ❌ to remove this embed.
Angius
Angius16mo ago
No need for ()
Thinker
Thinker16mo ago
That didn't create a Foo tho
Angius
Angius16mo ago
Ah, huh You're right Fixed
r2d25551
r2d25551OP16mo ago
With new { . . . } now the compiler complains about using null for the return type member:
error CS0828: Cannot assign '<null>' to anonymous type property
error CS0828: Cannot assign '<null>' to anonymous type property
Angius
Angius16mo ago
Show code
r2d25551
r2d25551OP16mo ago
Which makes sense given that is literally an c# interface. I need to replace it with the baseclass. . .
Angius
Angius16mo ago
True, null cannot implement an interface But, also, that field is not nullable either
ero
ero16mo ago
should be able to be null just fine what definitely won't work is new IRet
MODiX
MODiX16mo ago
Angius
REPL Result: Success
public interface IFoo {}
public class Bar {
public IFoo Foo { get; set; }
}

new Bar { Foo = null }
public interface IFoo {}
public class Bar {
public IFoo Foo { get; set; }
}

new Bar { Foo = null }
Result: Bar
{
"foo": null
}
{
"foo": null
}
Compile: 481.026ms | Execution: 52.361ms | React with ❌ to remove this embed.
Angius
Angius16mo ago
It seems to work, yeah, you're right In that case something else is wrong But since we don't have the updated code... the answer is ¯\_(ツ)_/¯
ero
ero16mo ago
Cannot assign '<null>' to anonymous type property
seems pretty clear to me
MODiX
MODiX16mo ago
ero
REPL Result: Failure
public interface IRet { }

public record Known(
string Param1,
string Param2,
IRet? MyObj,
Func<string, string, IRet> Create);

Dictionay<string, Known> myData = new()
{
["one"] = new(
"abc",
"xyz",
null,
CreateFunc)
};

static IRet CreateFunc(string param1, string param2)
{
IRet iret = new(param1, param2); // error here
return iret;
}
public interface IRet { }

public record Known(
string Param1,
string Param2,
IRet? MyObj,
Func<string, string, IRet> Create);

Dictionay<string, Known> myData = new()
{
["one"] = new(
"abc",
"xyz",
null,
CreateFunc)
};

static IRet CreateFunc(string param1, string param2)
{
IRet iret = new(param1, param2); // error here
return iret;
}
Exception: CompilationErrorException
- The type or namespace name 'Dictionay<,>' could not be found (are you missing a using directive or an assembly reference?)
- Cannot create an instance of the abstract type or interface 'IRet'
- The type or namespace name 'Dictionay<,>' could not be found (are you missing a using directive or an assembly reference?)
- Cannot create an instance of the abstract type or interface 'IRet'
Compile: 687.224ms | Execution: 0.000ms | React with ❌ to remove this embed.
ero
ero16mo ago
typor ReallyMad
Angius
Angius16mo ago
Cannot create an instance of the abstract type or interface 'IRet' That's a different message
ero
ero16mo ago
i mean yeah i'm referring to this
Angius
Angius16mo ago
Which... is a different error
r2d25551
r2d25551OP16mo ago
An abstract class didn't help. But if I understood correctly the interface should work too?
Angius
Angius16mo ago
Just show us your updated code already
r2d25551
r2d25551OP16mo ago
There is a lot of it. And, more importantly, sensitive. It is painstaking to recreate in Discord,
Angius
Angius16mo ago
Then we're wading in the dark "Doctor, it hurts" "Show me where?" "Me hurty" "Okay, but what hurts?" "Tea didn't help" "???" If there's nothing to work with, well, let's hope Ero and Thinker can help you, because I'm out
ero
ero16mo ago
it's almost difficult to believe there can possibly be any sensitive data where there isn't even any working code
r2d25551
r2d25551OP16mo ago
Does this work?
Dictionay<string, Known> myData = new()
{
["one"] = new(
"abc",
"xyz",
null,
CreateFunc)
};

static IRet CreateFunc(string param1, string param2)
{
IRet iret = new(param1, param2); // error here
return iret;
}
Dictionay<string, Known> myData = new()
{
["one"] = new(
"abc",
"xyz",
null,
CreateFunc)
};

static IRet CreateFunc(string param1, string param2)
{
IRet iret = new(param1, param2); // error here
return iret;
}
ero
ero16mo ago
i don't know, does it?
Angius
Angius16mo ago
You can't instantiate an interface
r2d25551
r2d25551OP16mo ago
Added the ?
public IRet? myObj;
public IRet? myObj;
But that didn't help. ? means nullable?
Angius
Angius16mo ago
It does, yes IRet iret = new(param1, param2); You can't instantiate an interface
r2d25551
r2d25551OP16mo ago
Would the construtor help with (trying now):
error CS0746: Anonymous type members must be declared with a member assignment, simple name or member access.
error CS0746: Anonymous type members must be declared with a member assignment, simple name or member access.
Angius
Angius16mo ago
No Instantiating a concrete class would You cannot instantiate an interface You cannot instantiate an abstract class You can only ever instantiate a concrete class
r2d25551
r2d25551OP16mo ago
As in new Known(. . .)?
Angius
Angius16mo ago
As in
IRet iret = new ClassThatImplementsIRet(param1, param2)
IRet iret = new ClassThatImplementsIRet(param1, param2)
r2d25551
r2d25551OP16mo ago
I am getting two. . .
error CS1922: Cannot initialize type 'Known' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
error CS1922: Cannot initialize type 'Known' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
Changine frm { to )
Angius
Angius16mo ago
Are you initializing a Known with
{
"foo",
"bar",
null
}
{
"foo",
"bar",
null
}
again...?
r2d25551
r2d25551OP16mo ago
That was my initial setup . . . since added new Known { . . . } now trying new Know( . . . ) and adding the constructor
Angius
Angius16mo ago
new Known {
Foo = "bar",
Baz = 421
}
new Known {
Foo = "bar",
Baz = 421
}
will work ONLY if you write your code properly and turn those public fields into public properties
r2d25551
r2d25551OP16mo ago
Can do that. Let me try now. . .
Angius
Angius16mo ago
For reference, $structure
MODiX
MODiX16mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
For C# versions older than 10, see $StructureOld
r2d25551
r2d25551OP16mo ago
Making changes (a few) be right back . . . thank you all for the help. Thank you. The compile and build worked. I need to test it now. Using as properties did the trick. And I am still using an interface as the base. Continued use and improvements of what I worked on is successful. Thank you all.
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server