473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Declare variable. Is this possible?

Hello,

I am defining a class as follows:

stat = new Stat {
Count = Roles.GetUsersInRole(RoleType.Administrator.ToStri ng
()).Count(),
Share = Count / UserCount * 100,
UserCount = Membership.GetAllUsers().Count
};

Is there a way to make:
Share = Count / UserCount * 100,

To work?

I mean, at the moment, Count and UserCount are not being recognized.
I understand why ... I am just trying to figure if there is a way to
declare this instance in one step without repeating the code like:

Share = Roles.GetUsersInRole(RoleType.Administrator.ToStri ng()).Count
() / Membership.GetAllUsers().Count * 100

Thanks,
Miguel
Nov 19 '08 #1
1 1299
shapper wrote:
I am defining a class as follows:

stat = new Stat {
Count = Roles.GetUsersInRole(RoleType.Administrator.ToStri ng
()).Count(),
Share = Count / UserCount * 100,
UserCount = Membership.GetAllUsers().Count
};

Is there a way to make:
Share = Count / UserCount * 100,

To work?
Sure. Write it *after* your object initializer:

stat = new Stat { ... };
stat.Share = stat.Count / stat.UserCount * 100;

Note that underneath, this is what the C# compiler is doing anyway: "x = new
X { A = a, B = b }" is implemented as "x = new X(); x.A = a; x.B = b;".
Object initializers are neat, but it's not a disaster if you can't write
everything down neatly.

Also, if "Share" is always completely defined by Count and UserCount,
consider making it a property without storage instead:

class Stat {
...
double Share {
get {
return UserCount == 0 ? 0 : Count * 100.0 / UserCount;
}
}
}

Assigning to Share is now neither necessary nor possible.

--
J.
Nov 19 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
6
by: Jason Shohet | last post by:
2 questions for anyone who can answer: 1. in class declarations, I realize you have to use the NEW keyword if you want to declare & instantiate some class at the same time. Whats an advantage...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
6
by: Jakob Bieling | last post by:
Hi, I recently noticed that my compiler (VC++7.1) allows me to declare variables inside the expression for an if-statement, like so: if (int i = 0) { i = 1; }
4
by: ankurdave | last post by:
Is it possible to declare a class member variable in the constructor? For example, class SomeClass { public: SomeClass() { int SomeArray; } }
1
by: ares.lagae | last post by:
- I have a typelist and I want to declare a member variable for each of the types. How can I do that? E.g. I have the typelist "typedef boost::mpl::vector<int, float> types;" and I want to declare...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
5
by: dancer | last post by:
Using ASP.net 1.1 and VB Is it possible to declare variables in their own subroutine? I had my DIM statements within a sub that worked just fine. I wanted to be able to use them in another...
0
by: FLANDERS | last post by:
Hi all, Is it possible to declare a SQL type of result set or similar? I want to do use the IN predicate like you can in a non-procedural SQL like this: UPDATE TABLE1 SET COL1 = 123 WHERE COL2 IN...
5
by: WanHongbin | last post by:
#include <stdio.h> double square(); /*without declare main() { double s; s = square(2); printf("%g\n", s); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.