例如,我有一个名为" Tree"有两个私有变量
public class Tree()
{
private string name;
private int age;
public void Tree(string s, int x)
{
//simply make an object.
}
}
我从另一个类创建了一个对象,使得一个词典
Dictionary<string, Tree> family = new Dictionary<string, Tree>;
字符串将引用昵称,而Tree将包含它所属的人。 (每个昵称一个人)。假设字典中有一个关键字&#34; Vats&#34;和属于&#34; Vats&#34;名字=丹,年龄= 18岁。如果我想得到姓名和年龄,我将如何继续这样做,因为我确定我不能这样做:
&#34;大桶&#34; = nickN
string a = family.TryGetValue[nickN].name;
int x = family.TryGetValue[nickN].age;
有什么想法吗?
答案 0 :(得分:2)
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags-->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<!-- Your app title -->
<title>My App</title>
<!-- Path to Framework7 iOS CSS theme styles-->
<!--<link rel="stylesheet" href="lib/framework7/css/framework7.ios.min.css">-->
<!-- Path to Framework7 iOS related color styles -->
<!--<link rel="stylesheet" href="lib/framework7/css/framework7.ios.colors.min.css">-->
<!-- Path to your custom app styles-->
<!--<link rel="stylesheet" href="css/styles.css">-->
</head>
<body>
<!-- Status bar overlay for full screen mode (PhoneGap) -->
<div class="statusbar-overlay"></div>
<!-- Panels overlay-->
<div class="panel-overlay"></div>
<!-- Left panel with reveal effect-->
<div class="panel panel-left panel-reveal">
<div class="content-block">
<p>Left panel content goes here</p>
</div>
</div>
<!-- Views -->
<div class="views">
<!-- Your main view, should have "view-main" class -->
<div class="view view-main">
<!-- Top Navbar-->
<div class="navbar">
<div class="navbar-inner">
<div class="left">
<a href="#" class="link icon-only open-panel"><i class="icon icon-bars"></i></a>
</div>
<!-- We need cool sliding animation on title element, so we have additional "sliding" class -->
<div class="center sliding">Dashboard</div>
<div class="subnavbar">
<!-- Sub navbar content, for example tabs buttons -->
<div class="buttons-row">
<a href="#tab1" class="button active">Tab 1</a>
<a href="#tab2" class="button">Tab 2</a>
<a href="#tab3" class="button">Tab 3</a>
</div>
</div>
</div>
</div>
<!-- Pages container, because we use fixed-through navbar and toolbar, it has additional appropriate classes-->
<div class="pages navbar-through">
<!-- Pag has additional "with-subnavbar" class -->
<div data-page="home" class="page with-subnavbar">
<div class="page-content hide-bars-on-scroll">
<div class="tabs">
<div id="tab1" class="tab active">
<div class="content-block">
<p>Lorem ipsum dolor ...</p>
<p>In sed augue non ...</p>
</div>
</div>
<div id="tab2" class="tab">
<div class="content-block">
<p>Donec iaculis ...</p>
<p>Curabitur egestas, mi ...</p>
<p>Donec iaculis posuere ...</p>
</div>
</div>
<div id="tab3" class="tab">
<div class="content-block">
<p>Etiam non interdum erat...</p>
<p>Duis ac semper risus. Suspendisse...</p>
<p>Etiam non interdum erat...</p>
<p>Duis ac semper risus. Suspendisse...</p>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Path to Framework7 Library JS-->
<script type="text/javascript" src="lib/framework7/js/framework7.min.js"></script>
<script>
(function () {
if (Framework7.prototype.device.android) {
Dom7('head').append(
//'<link rel="stylesheet" href="lib/framework7/css/framework7.ios.min.css">' +
//'<link rel="stylesheet" href="lib/framework7/css/framework7.ios.colors.min.css">' +
'<link rel="stylesheet" href="lib/framework7/css/framework7.material.min.css">' +
'<link rel="stylesheet" href="lib/framework7/css/framework7.material.colors.min.css">' +
'<link rel="stylesheet" href="css/styles.css">'
);
}
else {
Dom7('head').append(
'<link rel="stylesheet" href="lib/framework7/css/framework7.material.min.css">' +
'<link rel="stylesheet" href="lib/framework7/css/framework7.material.colors.min.css">' +
//'<link rel="stylesheet" href="lib/framework7/css/framework7.ios.min.css">' +
//'<link rel="stylesheet" href="lib/framework7/css/framework7.ios.colors.min.css">' +
'<link rel="stylesheet" href="css/styles.css">'
);
}
})();
</script>
<!-- Path to your app js-->
<script type="text/javascript" src="js/my-app.js"></script>
</body>
</html>
:
COPY TO CLIPBOARD SELECT ALL
答案 1 :(得分:1)
如果您当然知道,您要访问的项目确实存在于词典中,则可以使用带方括号的直接访问语法,而不是使用TryGetValue
。
string a = family[nickN].name;
int x = family[nickN].age;
要从name
类外部访问私有字段age
和Tree
,请将其公开或创建公共属性以便像这样访问它们:
public class Tree()
{
private string name;
private int age;
public void Tree(string s, int x)
{
name = s;
age = x;
}
public string Name{ get{ return name; }}
public int Age{ get{ return age; }}
}
然后你可以使用:
string a = family[nickN].Name;
int x = family[nickN].Age;