Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 01:16

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Problem with custom constructor for a struct

Discussion on Problem with custom constructor for a struct within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Sep 2013
Posts: 63
Received Thanks: 26
Problem with custom constructor for a struct

Hay guys i have the following problem:

I have a struct for saving some faked "weather" data. So i want to store a random number as measure value and the date + time it was measured. My teacher told me to use a custom constructor so that i can give the values when creating a new one. So far so good but when i did it Visual Studio marked me the first value in the master constructor as error.
The error is :
PHP Code:
The this object can only be used when all fields have been assigned
And this is the struct code :
PHP Code:
struct WeatherData
    
{
        private 
int _fakeValue;
        private 
string _fakeDate;
        
// getter und setter for the privat members
        
public int FakeValue
        
{
            
get { return _fakeValue; }
            
set _fakeValue value; }
        }
        public 
string FakeDate
        
{
            
get { return _fakeDate; }
            
set _fakeDate value; }
        }
        
// overrides the inherited ToString method with my own version
        
public override string ToString()
        {
            return 
String.Format("Wert: {0}, gemessen am: {1}"FakeValueFakeDate);
        }

        public 
WeatherData(int value)
        :
this(valueDateTime.Now.ToString()){}

        public 
WeatherData(string date)
        :
this(0date){}

        
// here is the error but i don't know why... 
        
public WeatherData(int valuestring date)
        {
            
this.FakeValue value;
            
this.FakeDate date;
        }
    } 
So would be nice if anyone could tell me what's wrong there ^^
And if you have any ideas of making it better just tell me

desTenshi

ps:

This whole program is a school project i got from my teacher, so don't say something about the topic of it xD ...
desTenshi is offline  
Old 10/06/2013, 20:42   #2
 
elite*gold: 0
Join Date: May 2010
Posts: 88
Received Thanks: 23
You should inside the class:

Code:
public WeatherData(int value, string date)
{
    _fakeValue = value;
    _fakeDate = date;
}
Easy-Emu is offline  
Old 10/06/2013, 21:01   #3
 
elite*gold: 0
Join Date: Sep 2013
Posts: 63
Received Thanks: 26
Quote:
Originally Posted by Easy-Emu View Post
You should inside the class:

Code:
public WeatherData(int value, string date)
{
    _fakeValue = value;
    _fakeDate = date;
}
Well in this case it would be ok if i use it, but if i would use any tests or something in the setter i would have to use this code again here. So in my eyes it's not the best way to solve the problem by just using the private variable :/
desTenshi is offline  
Old 10/07/2013, 03:18   #4
 
elite*gold: 0
Join Date: May 2010
Posts: 793
Received Thanks: 268
another solution is calling the default contructor before wich sets all fields to their default values:
Code:
        public WeatherData(int value, string date)
            :this()
        {
            this.FakeValue = value;
            this.FakeDate = date;
        }
note that you cannot change the compiler generated default constructor. i think this is more a question of taste then of anything else.
nkkk is offline  
Thanks
1 User
Old 10/07/2013, 11:20   #5
 
elite*gold: 0
Join Date: Sep 2013
Posts: 63
Received Thanks: 26
Quote:
Originally Posted by nkkk View Post
another solution is calling the default contructor before wich sets all fields to their default values:
Code:
        public WeatherData(int value, string date)
            :this()
        {
            this.FakeValue = value;
            this.FakeDate = date;
        }
note that you cannot change the compiler generated default constructor. i think this is more a question of taste then of anything else.
Well that works. But i still don't understand why VS say's that's an error.. I can compile it and it works just fine.
I know i cant change the default constructor for a struct, but in which way does this effect the main constructor? (sorry if that's a stupid question :x )
desTenshi is offline  
Old 10/07/2013, 12:15   #6



 
Shawak's Avatar
 
elite*gold: 0
The Black Market: 259/0/0
Join Date: Apr 2010
Posts: 10,289
Received Thanks: 3,613
You don't use :this() {..}

Code:
	public WeatherData(int value)
	{
		WeatherData(value, DateTime.Now.ToString());
	}

	public WeatherData(string date)
	{
		WeatherData(0, date);
	}

        public WeatherData(int value, string date)
        {
		_fakeValue = value;
		_fakeDate = date;
        }
Edit: nvm, mixed something up
Shawak is offline  
Thanks
1 User
Old 10/07/2013, 14:06   #7
 
elite*gold: 0
Join Date: May 2010
Posts: 88
Received Thanks: 23
Quote:
Originally Posted by desTenshi View Post
Well in this case it would be ok if i use it, but if i would use any tests or something in the setter i would have to use this code again here. So in my eyes it's not the best way to solve the problem by just using the private variable :/
it's normal to use private variables inside the class and the public getter/setter only in other classes
Easy-Emu is offline  
Old 10/07/2013, 14:21   #8
 
elite*gold: 0
Join Date: Sep 2013
Posts: 63
Received Thanks: 26
Quote:
Originally Posted by Shawak View Post
You don't use :this() {..}

Code:
	public WeatherData(int value)
	{
		WeatherData(value, DateTime.Now.ToString());
	}

	public WeatherData(string date)
	{
		WeatherData(0, date);
	}

        public WeatherData(int value, string date)
        {
		_fakeValue = value;
		_fakeDate = date;
        }
Why ? It's normal constructor chaining?

Quote:
Originally Posted by Easy-Emu View Post
it's normal to use private variables inside the class and the public getter/setter only in other classes
Well but that would be against the logic to implement the code only once.
Also the book Pro C# 5.0 and the .net 4.5 framework tells to use the c# properties. In my eye's it's smarter than write the code for class / struct data checks two times.
desTenshi is offline  
Reply


Similar Threads Similar Threads
Struct/MAP in struct speichern?
09/19/2013 - C/C++ - 26 Replies
Ich versuche momentan in einer Struct den datentyp map zu speichern, aber sobald ich es in der struct in der headerdatei mit reinpacke stirbt das Programm beim start. sieht ca so aus struct EventGift { DWORD dwID; int nCount; int nPercentage; };
Beta-Max Online ~ 120 CAP ~ D13 ~ Custom Events ~ Custom Quests ~ Custom Areas
03/19/2012 - SRO PServer Advertising - 6 Replies
Fantastic Server. If reliablility is what you want then Beta-Max is for you. Unique PVP Battles - Pets - Items - Quests - Jobs - Wonga and so much more Server Details: Level Cap: 120 Skill Cap: 120 Chinese Mastery Cap: 360 European Mastery Cap: 240 Item Degree: 13th Degree Active Areas: All (Including Jupiter)



All times are GMT +1. The time now is 01:16.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.