Using static type member to ensure type-safety in a non-static class










0















I have a static class defined like this:



public static class JobStatus

public const string Completed = "Completed";
public const string Failed = "Failed";
public const string Stopped = "Stopped";



(this is actually an external library, so can't change this)



In my non-static class I want a member of that class to ensure that you can only declare it of that "type"



public class JobOutput

public string Output get; set;

public string OutputError get; set;

public JobStatus JobStatus get; set;



Error: 'JobStatus': static types cannot be used as return types / 'JobStatus': static types cannot be used as parameters



Yeye I know your eyes are bleeding, but I hope you get the point - how can I ensure and achieve a form of type-safety for my JobStatus property?










share|improve this question
























  • Do you want to ensure that your JobStatus property can only have those string values defined in the static JobStatus class?

    – dymanoid
    Nov 14 '18 at 10:21












  • Why JobStatus has to be static?

    – Rango
    Nov 14 '18 at 10:24











  • @dymanoid yep exactly

    – Morten_564834
    Nov 14 '18 at 10:25











  • @TimSchmelter can't change it, using the class from another library

    – Morten_564834
    Nov 14 '18 at 10:26











  • I also tried defining JobOutput class and its members as static, same result.

    – Morten_564834
    Nov 14 '18 at 10:27















0















I have a static class defined like this:



public static class JobStatus

public const string Completed = "Completed";
public const string Failed = "Failed";
public const string Stopped = "Stopped";



(this is actually an external library, so can't change this)



In my non-static class I want a member of that class to ensure that you can only declare it of that "type"



public class JobOutput

public string Output get; set;

public string OutputError get; set;

public JobStatus JobStatus get; set;



Error: 'JobStatus': static types cannot be used as return types / 'JobStatus': static types cannot be used as parameters



Yeye I know your eyes are bleeding, but I hope you get the point - how can I ensure and achieve a form of type-safety for my JobStatus property?










share|improve this question
























  • Do you want to ensure that your JobStatus property can only have those string values defined in the static JobStatus class?

    – dymanoid
    Nov 14 '18 at 10:21












  • Why JobStatus has to be static?

    – Rango
    Nov 14 '18 at 10:24











  • @dymanoid yep exactly

    – Morten_564834
    Nov 14 '18 at 10:25











  • @TimSchmelter can't change it, using the class from another library

    – Morten_564834
    Nov 14 '18 at 10:26











  • I also tried defining JobOutput class and its members as static, same result.

    – Morten_564834
    Nov 14 '18 at 10:27













0












0








0








I have a static class defined like this:



public static class JobStatus

public const string Completed = "Completed";
public const string Failed = "Failed";
public const string Stopped = "Stopped";



(this is actually an external library, so can't change this)



In my non-static class I want a member of that class to ensure that you can only declare it of that "type"



public class JobOutput

public string Output get; set;

public string OutputError get; set;

public JobStatus JobStatus get; set;



Error: 'JobStatus': static types cannot be used as return types / 'JobStatus': static types cannot be used as parameters



Yeye I know your eyes are bleeding, but I hope you get the point - how can I ensure and achieve a form of type-safety for my JobStatus property?










share|improve this question
















I have a static class defined like this:



public static class JobStatus

public const string Completed = "Completed";
public const string Failed = "Failed";
public const string Stopped = "Stopped";



(this is actually an external library, so can't change this)



In my non-static class I want a member of that class to ensure that you can only declare it of that "type"



public class JobOutput

public string Output get; set;

public string OutputError get; set;

public JobStatus JobStatus get; set;



Error: 'JobStatus': static types cannot be used as return types / 'JobStatus': static types cannot be used as parameters



Yeye I know your eyes are bleeding, but I hope you get the point - how can I ensure and achieve a form of type-safety for my JobStatus property?







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 10:21









Wai Ha Lee

6,016123965




6,016123965










asked Nov 14 '18 at 10:18









Morten_564834Morten_564834

3831314




3831314












  • Do you want to ensure that your JobStatus property can only have those string values defined in the static JobStatus class?

    – dymanoid
    Nov 14 '18 at 10:21












  • Why JobStatus has to be static?

    – Rango
    Nov 14 '18 at 10:24











  • @dymanoid yep exactly

    – Morten_564834
    Nov 14 '18 at 10:25











  • @TimSchmelter can't change it, using the class from another library

    – Morten_564834
    Nov 14 '18 at 10:26











  • I also tried defining JobOutput class and its members as static, same result.

    – Morten_564834
    Nov 14 '18 at 10:27

















  • Do you want to ensure that your JobStatus property can only have those string values defined in the static JobStatus class?

    – dymanoid
    Nov 14 '18 at 10:21












  • Why JobStatus has to be static?

    – Rango
    Nov 14 '18 at 10:24











  • @dymanoid yep exactly

    – Morten_564834
    Nov 14 '18 at 10:25











  • @TimSchmelter can't change it, using the class from another library

    – Morten_564834
    Nov 14 '18 at 10:26











  • I also tried defining JobOutput class and its members as static, same result.

    – Morten_564834
    Nov 14 '18 at 10:27
















Do you want to ensure that your JobStatus property can only have those string values defined in the static JobStatus class?

– dymanoid
Nov 14 '18 at 10:21






Do you want to ensure that your JobStatus property can only have those string values defined in the static JobStatus class?

– dymanoid
Nov 14 '18 at 10:21














Why JobStatus has to be static?

– Rango
Nov 14 '18 at 10:24





Why JobStatus has to be static?

– Rango
Nov 14 '18 at 10:24













@dymanoid yep exactly

– Morten_564834
Nov 14 '18 at 10:25





@dymanoid yep exactly

– Morten_564834
Nov 14 '18 at 10:25













@TimSchmelter can't change it, using the class from another library

– Morten_564834
Nov 14 '18 at 10:26





@TimSchmelter can't change it, using the class from another library

– Morten_564834
Nov 14 '18 at 10:26













I also tried defining JobOutput class and its members as static, same result.

– Morten_564834
Nov 14 '18 at 10:27





I also tried defining JobOutput class and its members as static, same result.

– Morten_564834
Nov 14 '18 at 10:27












2 Answers
2






active

oldest

votes


















1














You could wrap JobStatus to make it "type safe" but it looks like a bit of an overkill:



public sealed class JobStatusWrapper

public static readonly JobStatusWrapper Completed
= new JobStatusWrapper(JobStatus.Completed);

public static readonly JobStatusWrapper Failed
= new JobStatusWrapper(JobStatus.Failed);

public static readonly JobStatusWrapper Stopped
= new JobStatusWrapper(JobStatus.Stopped);

private readonly string description;
private JobStatusWrapper(string description)
Debug.Assert(!string.IsNullOrEmpty(description));
this.description = description;

public static implicit operator string(JobStatusWrapper status)
=> status.description;



And now you'd use it:



public class JobOutput

//...
public JobStatusWrapper JobStatus get; set;



And there is no way to pass in or get a JobStatusWrapper that doesn't have one of the underlying values defined in JobStatus (except null). Also, the implicit operator makes the wrapper usable anywhere the JobStatus options are.






share|improve this answer

























  • Thanks for the input! learned some new stuff there. But yes as @codecaster said the library could change, so i'd be even greater if it were dynamic. I don't think it will change though, so might be wasted effort. In anycase thanks!

    – Morten_564834
    Nov 14 '18 at 11:07



















1














You can't, because all JobStatus does is contain some members who hold strings. So you'll have to define your JobStatus property as a string as well.



There's no compile-time safety for strings, it could've been an enum instead.



You could add a method SetJobStatus(string status) to your JobOutput class and make JobStatus's setter private. Then in that method, you check (using reflection) whether the status string is present in one of the static class JobStatus's public const fields. Or you could implement the same in the setter.



See How can I get all constants of a type by reflection? for information on how to do that. But that's not compile-time safety, but runtime.






share|improve this answer























  • yes that was also the approach I would go for if I can't get compile-time safety - was hoping though :)

    – Morten_564834
    Nov 14 '18 at 10:30











  • @Tim yeah and then the library gets updated and adds another field...

    – CodeCaster
    Nov 14 '18 at 10:38










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297815%2fusing-static-type-member-to-ensure-type-safety-in-a-non-static-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You could wrap JobStatus to make it "type safe" but it looks like a bit of an overkill:



public sealed class JobStatusWrapper

public static readonly JobStatusWrapper Completed
= new JobStatusWrapper(JobStatus.Completed);

public static readonly JobStatusWrapper Failed
= new JobStatusWrapper(JobStatus.Failed);

public static readonly JobStatusWrapper Stopped
= new JobStatusWrapper(JobStatus.Stopped);

private readonly string description;
private JobStatusWrapper(string description)
Debug.Assert(!string.IsNullOrEmpty(description));
this.description = description;

public static implicit operator string(JobStatusWrapper status)
=> status.description;



And now you'd use it:



public class JobOutput

//...
public JobStatusWrapper JobStatus get; set;



And there is no way to pass in or get a JobStatusWrapper that doesn't have one of the underlying values defined in JobStatus (except null). Also, the implicit operator makes the wrapper usable anywhere the JobStatus options are.






share|improve this answer

























  • Thanks for the input! learned some new stuff there. But yes as @codecaster said the library could change, so i'd be even greater if it were dynamic. I don't think it will change though, so might be wasted effort. In anycase thanks!

    – Morten_564834
    Nov 14 '18 at 11:07
















1














You could wrap JobStatus to make it "type safe" but it looks like a bit of an overkill:



public sealed class JobStatusWrapper

public static readonly JobStatusWrapper Completed
= new JobStatusWrapper(JobStatus.Completed);

public static readonly JobStatusWrapper Failed
= new JobStatusWrapper(JobStatus.Failed);

public static readonly JobStatusWrapper Stopped
= new JobStatusWrapper(JobStatus.Stopped);

private readonly string description;
private JobStatusWrapper(string description)
Debug.Assert(!string.IsNullOrEmpty(description));
this.description = description;

public static implicit operator string(JobStatusWrapper status)
=> status.description;



And now you'd use it:



public class JobOutput

//...
public JobStatusWrapper JobStatus get; set;



And there is no way to pass in or get a JobStatusWrapper that doesn't have one of the underlying values defined in JobStatus (except null). Also, the implicit operator makes the wrapper usable anywhere the JobStatus options are.






share|improve this answer

























  • Thanks for the input! learned some new stuff there. But yes as @codecaster said the library could change, so i'd be even greater if it were dynamic. I don't think it will change though, so might be wasted effort. In anycase thanks!

    – Morten_564834
    Nov 14 '18 at 11:07














1












1








1







You could wrap JobStatus to make it "type safe" but it looks like a bit of an overkill:



public sealed class JobStatusWrapper

public static readonly JobStatusWrapper Completed
= new JobStatusWrapper(JobStatus.Completed);

public static readonly JobStatusWrapper Failed
= new JobStatusWrapper(JobStatus.Failed);

public static readonly JobStatusWrapper Stopped
= new JobStatusWrapper(JobStatus.Stopped);

private readonly string description;
private JobStatusWrapper(string description)
Debug.Assert(!string.IsNullOrEmpty(description));
this.description = description;

public static implicit operator string(JobStatusWrapper status)
=> status.description;



And now you'd use it:



public class JobOutput

//...
public JobStatusWrapper JobStatus get; set;



And there is no way to pass in or get a JobStatusWrapper that doesn't have one of the underlying values defined in JobStatus (except null). Also, the implicit operator makes the wrapper usable anywhere the JobStatus options are.






share|improve this answer















You could wrap JobStatus to make it "type safe" but it looks like a bit of an overkill:



public sealed class JobStatusWrapper

public static readonly JobStatusWrapper Completed
= new JobStatusWrapper(JobStatus.Completed);

public static readonly JobStatusWrapper Failed
= new JobStatusWrapper(JobStatus.Failed);

public static readonly JobStatusWrapper Stopped
= new JobStatusWrapper(JobStatus.Stopped);

private readonly string description;
private JobStatusWrapper(string description)
Debug.Assert(!string.IsNullOrEmpty(description));
this.description = description;

public static implicit operator string(JobStatusWrapper status)
=> status.description;



And now you'd use it:



public class JobOutput

//...
public JobStatusWrapper JobStatus get; set;



And there is no way to pass in or get a JobStatusWrapper that doesn't have one of the underlying values defined in JobStatus (except null). Also, the implicit operator makes the wrapper usable anywhere the JobStatus options are.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 10:55

























answered Nov 14 '18 at 10:40









InBetweenInBetween

25.4k34068




25.4k34068












  • Thanks for the input! learned some new stuff there. But yes as @codecaster said the library could change, so i'd be even greater if it were dynamic. I don't think it will change though, so might be wasted effort. In anycase thanks!

    – Morten_564834
    Nov 14 '18 at 11:07


















  • Thanks for the input! learned some new stuff there. But yes as @codecaster said the library could change, so i'd be even greater if it were dynamic. I don't think it will change though, so might be wasted effort. In anycase thanks!

    – Morten_564834
    Nov 14 '18 at 11:07

















Thanks for the input! learned some new stuff there. But yes as @codecaster said the library could change, so i'd be even greater if it were dynamic. I don't think it will change though, so might be wasted effort. In anycase thanks!

– Morten_564834
Nov 14 '18 at 11:07






Thanks for the input! learned some new stuff there. But yes as @codecaster said the library could change, so i'd be even greater if it were dynamic. I don't think it will change though, so might be wasted effort. In anycase thanks!

– Morten_564834
Nov 14 '18 at 11:07














1














You can't, because all JobStatus does is contain some members who hold strings. So you'll have to define your JobStatus property as a string as well.



There's no compile-time safety for strings, it could've been an enum instead.



You could add a method SetJobStatus(string status) to your JobOutput class and make JobStatus's setter private. Then in that method, you check (using reflection) whether the status string is present in one of the static class JobStatus's public const fields. Or you could implement the same in the setter.



See How can I get all constants of a type by reflection? for information on how to do that. But that's not compile-time safety, but runtime.






share|improve this answer























  • yes that was also the approach I would go for if I can't get compile-time safety - was hoping though :)

    – Morten_564834
    Nov 14 '18 at 10:30











  • @Tim yeah and then the library gets updated and adds another field...

    – CodeCaster
    Nov 14 '18 at 10:38















1














You can't, because all JobStatus does is contain some members who hold strings. So you'll have to define your JobStatus property as a string as well.



There's no compile-time safety for strings, it could've been an enum instead.



You could add a method SetJobStatus(string status) to your JobOutput class and make JobStatus's setter private. Then in that method, you check (using reflection) whether the status string is present in one of the static class JobStatus's public const fields. Or you could implement the same in the setter.



See How can I get all constants of a type by reflection? for information on how to do that. But that's not compile-time safety, but runtime.






share|improve this answer























  • yes that was also the approach I would go for if I can't get compile-time safety - was hoping though :)

    – Morten_564834
    Nov 14 '18 at 10:30











  • @Tim yeah and then the library gets updated and adds another field...

    – CodeCaster
    Nov 14 '18 at 10:38













1












1








1







You can't, because all JobStatus does is contain some members who hold strings. So you'll have to define your JobStatus property as a string as well.



There's no compile-time safety for strings, it could've been an enum instead.



You could add a method SetJobStatus(string status) to your JobOutput class and make JobStatus's setter private. Then in that method, you check (using reflection) whether the status string is present in one of the static class JobStatus's public const fields. Or you could implement the same in the setter.



See How can I get all constants of a type by reflection? for information on how to do that. But that's not compile-time safety, but runtime.






share|improve this answer













You can't, because all JobStatus does is contain some members who hold strings. So you'll have to define your JobStatus property as a string as well.



There's no compile-time safety for strings, it could've been an enum instead.



You could add a method SetJobStatus(string status) to your JobOutput class and make JobStatus's setter private. Then in that method, you check (using reflection) whether the status string is present in one of the static class JobStatus's public const fields. Or you could implement the same in the setter.



See How can I get all constants of a type by reflection? for information on how to do that. But that's not compile-time safety, but runtime.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 10:21









CodeCasterCodeCaster

109k17145197




109k17145197












  • yes that was also the approach I would go for if I can't get compile-time safety - was hoping though :)

    – Morten_564834
    Nov 14 '18 at 10:30











  • @Tim yeah and then the library gets updated and adds another field...

    – CodeCaster
    Nov 14 '18 at 10:38

















  • yes that was also the approach I would go for if I can't get compile-time safety - was hoping though :)

    – Morten_564834
    Nov 14 '18 at 10:30











  • @Tim yeah and then the library gets updated and adds another field...

    – CodeCaster
    Nov 14 '18 at 10:38
















yes that was also the approach I would go for if I can't get compile-time safety - was hoping though :)

– Morten_564834
Nov 14 '18 at 10:30





yes that was also the approach I would go for if I can't get compile-time safety - was hoping though :)

– Morten_564834
Nov 14 '18 at 10:30













@Tim yeah and then the library gets updated and adds another field...

– CodeCaster
Nov 14 '18 at 10:38





@Tim yeah and then the library gets updated and adds another field...

– CodeCaster
Nov 14 '18 at 10:38

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297815%2fusing-static-type-member-to-ensure-type-safety-in-a-non-static-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20