Byte array Properties C#
up vote
0
down vote
favorite
I have an object like this:
public class CustomObject{
public byte FieldA {private get; set;}
public IPAddreess FieldB {private get; set;}
}
FieldA is the byte rappresentation of FieldB.
I create this object from two sources of data.
One from a binary file where i need to be fast, then i prefer to set only the FieldA. The other one is in an application where i retrieve the data only in "FieldB format".
I want a function like this:
public IPAddress GetField(){
if (FieldB != null)
return FieldB;
FieldB = new IPAddress(FieldA);
return FieldB;
}
To simplify i used an IPAddress conversion, but usually i have more complex operations to do.
Is this the correct way to do this? Or there is some other method that can simplify this one? I'm using .NET CORE Thank you in advance.
c# properties .net-core byte
add a comment |
up vote
0
down vote
favorite
I have an object like this:
public class CustomObject{
public byte FieldA {private get; set;}
public IPAddreess FieldB {private get; set;}
}
FieldA is the byte rappresentation of FieldB.
I create this object from two sources of data.
One from a binary file where i need to be fast, then i prefer to set only the FieldA. The other one is in an application where i retrieve the data only in "FieldB format".
I want a function like this:
public IPAddress GetField(){
if (FieldB != null)
return FieldB;
FieldB = new IPAddress(FieldA);
return FieldB;
}
To simplify i used an IPAddress conversion, but usually i have more complex operations to do.
Is this the correct way to do this? Or there is some other method that can simplify this one? I'm using .NET CORE Thank you in advance.
c# properties .net-core byte
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 at 8:53
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 at 8:56
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 at 9:03
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 at 9:21
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 at 9:32
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have an object like this:
public class CustomObject{
public byte FieldA {private get; set;}
public IPAddreess FieldB {private get; set;}
}
FieldA is the byte rappresentation of FieldB.
I create this object from two sources of data.
One from a binary file where i need to be fast, then i prefer to set only the FieldA. The other one is in an application where i retrieve the data only in "FieldB format".
I want a function like this:
public IPAddress GetField(){
if (FieldB != null)
return FieldB;
FieldB = new IPAddress(FieldA);
return FieldB;
}
To simplify i used an IPAddress conversion, but usually i have more complex operations to do.
Is this the correct way to do this? Or there is some other method that can simplify this one? I'm using .NET CORE Thank you in advance.
c# properties .net-core byte
I have an object like this:
public class CustomObject{
public byte FieldA {private get; set;}
public IPAddreess FieldB {private get; set;}
}
FieldA is the byte rappresentation of FieldB.
I create this object from two sources of data.
One from a binary file where i need to be fast, then i prefer to set only the FieldA. The other one is in an application where i retrieve the data only in "FieldB format".
I want a function like this:
public IPAddress GetField(){
if (FieldB != null)
return FieldB;
FieldB = new IPAddress(FieldA);
return FieldB;
}
To simplify i used an IPAddress conversion, but usually i have more complex operations to do.
Is this the correct way to do this? Or there is some other method that can simplify this one? I'm using .NET CORE Thank you in advance.
c# properties .net-core byte
c# properties .net-core byte
edited Nov 20 at 8:42
John
10.8k31736
10.8k31736
asked Nov 20 at 8:33
Hyruma92
996
996
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 at 8:53
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 at 8:56
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 at 9:03
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 at 9:21
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 at 9:32
add a comment |
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 at 8:53
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 at 8:56
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 at 9:03
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 at 9:21
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 at 9:32
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 at 8:53
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 at 8:53
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 at 8:56
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 at 8:56
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 at 9:03
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 at 9:03
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 at 9:21
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 at 9:21
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 at 9:32
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 at 9:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
You can do that in FieldB
's getter, without explicitly writing a get-method:
private IPAddreess _fieldB;
public IPAddreess FieldB
{
get
{
if (_fieldB == null)
{
_fieldB = new IPAddress(FieldA);
}
return _fieldB;
}
set
{
_fieldB = value;
}
}
This code uses a private backing field _fieldB
for storing the property's value. Upon retrieving the property, it'll either return the value already stored in the field, or assign it based on FieldA
's contents and then return it.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can do that in FieldB
's getter, without explicitly writing a get-method:
private IPAddreess _fieldB;
public IPAddreess FieldB
{
get
{
if (_fieldB == null)
{
_fieldB = new IPAddress(FieldA);
}
return _fieldB;
}
set
{
_fieldB = value;
}
}
This code uses a private backing field _fieldB
for storing the property's value. Upon retrieving the property, it'll either return the value already stored in the field, or assign it based on FieldA
's contents and then return it.
add a comment |
up vote
2
down vote
You can do that in FieldB
's getter, without explicitly writing a get-method:
private IPAddreess _fieldB;
public IPAddreess FieldB
{
get
{
if (_fieldB == null)
{
_fieldB = new IPAddress(FieldA);
}
return _fieldB;
}
set
{
_fieldB = value;
}
}
This code uses a private backing field _fieldB
for storing the property's value. Upon retrieving the property, it'll either return the value already stored in the field, or assign it based on FieldA
's contents and then return it.
add a comment |
up vote
2
down vote
up vote
2
down vote
You can do that in FieldB
's getter, without explicitly writing a get-method:
private IPAddreess _fieldB;
public IPAddreess FieldB
{
get
{
if (_fieldB == null)
{
_fieldB = new IPAddress(FieldA);
}
return _fieldB;
}
set
{
_fieldB = value;
}
}
This code uses a private backing field _fieldB
for storing the property's value. Upon retrieving the property, it'll either return the value already stored in the field, or assign it based on FieldA
's contents and then return it.
You can do that in FieldB
's getter, without explicitly writing a get-method:
private IPAddreess _fieldB;
public IPAddreess FieldB
{
get
{
if (_fieldB == null)
{
_fieldB = new IPAddress(FieldA);
}
return _fieldB;
}
set
{
_fieldB = value;
}
}
This code uses a private backing field _fieldB
for storing the property's value. Upon retrieving the property, it'll either return the value already stored in the field, or assign it based on FieldA
's contents and then return it.
answered Nov 20 at 8:35
CodeCaster
106k17139190
106k17139190
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388994%2fbyte-array-properties-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 at 8:53
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 at 8:56
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 at 9:03
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 at 9:21
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 at 9:32