.NET Framework - Declare variable. Is this possible?

Asked By shapper
22-Nov-08 09:33 AM
Hello,

I am defining a class as follows:

stat = new Stat {
Count = Roles.GetUsersInRole(RoleType.Administrator.ToString
()).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.ToString()).Count
() / Membership.GetAllUsers().Count   * 100

Thanks,
Miguel
Roles.GetUsersInRole
(1)
Membership.GetAllUsers
(1)
GetUsersInRole
(1)
GetAllUsers
(1)
Stat
(1)
Stat.UserCount
(1)
UserCount
(1)
Stat.Count
(1)
  Jeroen Mostert replied...
19-Nov-08 01:56 PM
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.
Create New Account
help
But i m stopped here $c = Get-Credential Connect-VIServer -server nameserver -Credential $c Get-Stat -Entity (Get-vmhost -Name "servername*") -stat mem.usage.average -maxsamples 10 -Start (get-date).adddays(-7) -Finish (get-date) | ft -auto entity , value | Out-File c: \ francesco \ stat.txt $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = $SqlCmd = New-Object System.Data SqlClient.SqlCommand $SqlCmd.CommandText = get-content c: \ francesco \ stat.txt $sqlDataAdapter = new-object System.Data.SqlClient.SQLDataAdapter($sqlcommand) $sqlDataSet = new-object System.Data.dataset SqlBulkCopy (1) If all you want to do is take the output of your Get-Stat command you could convert the output to a DataTable and use SqlBulkCopy to import the blogs / posh / archive / 2007 / 01 / 21 / powershell-gui-scripblock-monitor-script.aspx Get your Get-Stat output and convert to DataTable using the Out-DataTable function $dataTable = Get-Stat . . rest of options. . | Select entity , value | Out-DataTable Run this SQL statement from SQL Server Management Studio to create the stat table "Create table stat (entity varchar(255), value decimal(18, 2))" Assuming the stat table
into C Sharp. Dim buf As New VB6.FixedLengthString(16384) Dim buf1 As String Dim stat As Integer Dim blen As Integer blen = Len(buf.Value) stat = GetRace(buf.Value, blen, CDBDE_REFCODE_TABLIMITED, Combo3.Text, Combo4.Text, getstate(), getpool(), Val(Text1.Text), Check1 buf as new StringBuilder(16384) dim buf1 as string dim blen as integer = 16384 dim stat as integer = GetRace(buf, blen, CDBDE_REFCODE_TABLIMITED, Combo3.Text, _ Combo4.Text, getstate(), getpool(), Val(Text1
Long If Source Is Nothing Then Debug.Print "Storage is not open" Else Call Source.Stat(dStat, comDefaultStats) With dStat sName = SysAllocString(.NamePtr) Call CoTaskMemFree(.NamePtr) End With Debug.Print "Contents long OffsetLSB, [in] long OffsetMSB, [in] long CountLSB, [in] long CountMSB, [in] ELockType LockType); HRESULT Stat( [out] TStatSTG *StatSTG, [in] EStatFlags Flags); HRESULT Clone( [out] IStream * *NewStream); }; / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / [ odl, uuid(0000000d-0000 ModificationTime); HRESULT SetClass( [in] TGUID* ClassID); HRESULT SetStateBits( [in] long StateBits, [in] long Mask); HRESULT Stat( [out] TStatSTG *StatSTG, [in] EStatFlags Flags); }; / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / [ dllname("ole32.dll"), uuid(EB41CAC0-DD1B-11d4-8CF1-080039010D55
like. . C: \ Documents and Settings \ acooper \ Local Settings \ Apps \ 2.0 \ C631H4LC.O7N \ BAL7BNDZ.NJZ \ stat. . tion_4a4e257f00226231_0001.0000_ba863c5628f914a9 While the path to the resource is. . . C: \ Documents and Settings \ acooper \ Local Settings \ Apps \ 2.0 \ Data \ 031PZ688.D4K \ 7MZND6VV.KW7 \ stat. . tion_4a4e257f00226231_0001.0000_ba863c5628f914a9 \ Data If there is someway to force the installer to put the resource
1) function Ping-Host{ begin{ $ping = new-object System.Net.NetworkInformation.Ping } process{ trap{ continue } $stat = $ping.Send($_).status if($stat -ne "Success") { write-warning "$_ is not pingable" } else { write-host "$_ is pingable" } } } get