Is it possible to instantiate a class using initialization list and call a method, all in single command?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I want to instantiate an object using initialization list and call a method in the same command.
string importantData = SearchOptions() {id = "10", className = "Fluffy"}.justAFunction();
The class looks like this:
class SearchOptions : PageBase
{
public SearchOptions()
{
id = string.Empty;
className = string.Empty;
text = string.Empty;
partialText = string.Empty;
XPath = string.Empty;
cssModifier = string.Empty;
}
string void justAFunction()
{
Console.WriteLine(id);
Console.WriteLine(className);
return "ImportantReturn";
}
}
I want to keep the flexibility of defining the necessary fields using initialization list and leave the others empty.
The class object is not needed later. Only the return argument of the method would be needed.
c# object constructor
add a comment |
I want to instantiate an object using initialization list and call a method in the same command.
string importantData = SearchOptions() {id = "10", className = "Fluffy"}.justAFunction();
The class looks like this:
class SearchOptions : PageBase
{
public SearchOptions()
{
id = string.Empty;
className = string.Empty;
text = string.Empty;
partialText = string.Empty;
XPath = string.Empty;
cssModifier = string.Empty;
}
string void justAFunction()
{
Console.WriteLine(id);
Console.WriteLine(className);
return "ImportantReturn";
}
}
I want to keep the flexibility of defining the necessary fields using initialization list and leave the others empty.
The class object is not needed later. Only the return argument of the method would be needed.
c# object constructor
1
Yes, it is, but you might want a static method instead.
– John
Nov 16 '18 at 17:04
add a comment |
I want to instantiate an object using initialization list and call a method in the same command.
string importantData = SearchOptions() {id = "10", className = "Fluffy"}.justAFunction();
The class looks like this:
class SearchOptions : PageBase
{
public SearchOptions()
{
id = string.Empty;
className = string.Empty;
text = string.Empty;
partialText = string.Empty;
XPath = string.Empty;
cssModifier = string.Empty;
}
string void justAFunction()
{
Console.WriteLine(id);
Console.WriteLine(className);
return "ImportantReturn";
}
}
I want to keep the flexibility of defining the necessary fields using initialization list and leave the others empty.
The class object is not needed later. Only the return argument of the method would be needed.
c# object constructor
I want to instantiate an object using initialization list and call a method in the same command.
string importantData = SearchOptions() {id = "10", className = "Fluffy"}.justAFunction();
The class looks like this:
class SearchOptions : PageBase
{
public SearchOptions()
{
id = string.Empty;
className = string.Empty;
text = string.Empty;
partialText = string.Empty;
XPath = string.Empty;
cssModifier = string.Empty;
}
string void justAFunction()
{
Console.WriteLine(id);
Console.WriteLine(className);
return "ImportantReturn";
}
}
I want to keep the flexibility of defining the necessary fields using initialization list and leave the others empty.
The class object is not needed later. Only the return argument of the method would be needed.
c# object constructor
c# object constructor
edited Nov 16 '18 at 17:45
Rufus L
19.4k31732
19.4k31732
asked Nov 16 '18 at 17:01
hasnain rehmanhasnain rehman
31
31
1
Yes, it is, but you might want a static method instead.
– John
Nov 16 '18 at 17:04
add a comment |
1
Yes, it is, but you might want a static method instead.
– John
Nov 16 '18 at 17:04
1
1
Yes, it is, but you might want a static method instead.
– John
Nov 16 '18 at 17:04
Yes, it is, but you might want a static method instead.
– John
Nov 16 '18 at 17:04
add a comment |
2 Answers
2
active
oldest
votes
You almost have it. You just need to add the new
keyword to create a new object before you can call a method on it:
string importantData = new SearchOptions {id = "10", className = "Fluffy"}.justAFunction();
Since you don't set any reference to the newly created object, it goes out of scope as soon as this line executes and will be cleaned up by the garbage collection engine.
Note that there are some other issues with your code that need to be fixed in order to make this work:
justAFunction()
should be publicly accessible (in your example, the access modifier is not specified, and the default isprivate
, so it would not work).- You cannot specify two return types (you have it declared as:
string void justAFunction
. You should remove thevoid
since you're returning a string).
And a few other suggestions:
- This assumes that the base class contains the properties you're setting (or they exist in this class and you just left them out for brevity)
- The properties you set when instantiating the object must be publicly accessible (or at least accessible from wherever you're creating it - usually they are
public
). - Public properties and methods should be
PascalCase
(notcamelCase
).
With these suggestions in mind, your classes would look something like:
class PageBase
{
public string Id { get; set; }
public string ClassName { get; set; }
public string Text { get; set; }
public string PartialText { get; set; }
public string XPath { get; set; }
public string CssModifier { get; set; }
}
class SearchOptions : PageBase
{
public SearchOptions()
{
Id = string.Empty;
ClassName = string.Empty;
Text = string.Empty;
PartialText = string.Empty;
XPath = string.Empty;
CssModifier = string.Empty;
}
public string JustAFunction()
{
Console.WriteLine(Id);
Console.WriteLine(ClassName);
return "ImportantReturn";
}
}
And then the call to get the important data looks like:
string importantData = new SearchOptions {Id = "10", ClassName = "Fluffy"}.JustAFunction();
add a comment |
Replace "string void justAFunction()" => "public string justAFunction()".
While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.
– Rufus L
Nov 16 '18 at 17:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53342342%2fis-it-possible-to-instantiate-a-class-using-initialization-list-and-call-a-metho%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You almost have it. You just need to add the new
keyword to create a new object before you can call a method on it:
string importantData = new SearchOptions {id = "10", className = "Fluffy"}.justAFunction();
Since you don't set any reference to the newly created object, it goes out of scope as soon as this line executes and will be cleaned up by the garbage collection engine.
Note that there are some other issues with your code that need to be fixed in order to make this work:
justAFunction()
should be publicly accessible (in your example, the access modifier is not specified, and the default isprivate
, so it would not work).- You cannot specify two return types (you have it declared as:
string void justAFunction
. You should remove thevoid
since you're returning a string).
And a few other suggestions:
- This assumes that the base class contains the properties you're setting (or they exist in this class and you just left them out for brevity)
- The properties you set when instantiating the object must be publicly accessible (or at least accessible from wherever you're creating it - usually they are
public
). - Public properties and methods should be
PascalCase
(notcamelCase
).
With these suggestions in mind, your classes would look something like:
class PageBase
{
public string Id { get; set; }
public string ClassName { get; set; }
public string Text { get; set; }
public string PartialText { get; set; }
public string XPath { get; set; }
public string CssModifier { get; set; }
}
class SearchOptions : PageBase
{
public SearchOptions()
{
Id = string.Empty;
ClassName = string.Empty;
Text = string.Empty;
PartialText = string.Empty;
XPath = string.Empty;
CssModifier = string.Empty;
}
public string JustAFunction()
{
Console.WriteLine(Id);
Console.WriteLine(ClassName);
return "ImportantReturn";
}
}
And then the call to get the important data looks like:
string importantData = new SearchOptions {Id = "10", ClassName = "Fluffy"}.JustAFunction();
add a comment |
You almost have it. You just need to add the new
keyword to create a new object before you can call a method on it:
string importantData = new SearchOptions {id = "10", className = "Fluffy"}.justAFunction();
Since you don't set any reference to the newly created object, it goes out of scope as soon as this line executes and will be cleaned up by the garbage collection engine.
Note that there are some other issues with your code that need to be fixed in order to make this work:
justAFunction()
should be publicly accessible (in your example, the access modifier is not specified, and the default isprivate
, so it would not work).- You cannot specify two return types (you have it declared as:
string void justAFunction
. You should remove thevoid
since you're returning a string).
And a few other suggestions:
- This assumes that the base class contains the properties you're setting (or they exist in this class and you just left them out for brevity)
- The properties you set when instantiating the object must be publicly accessible (or at least accessible from wherever you're creating it - usually they are
public
). - Public properties and methods should be
PascalCase
(notcamelCase
).
With these suggestions in mind, your classes would look something like:
class PageBase
{
public string Id { get; set; }
public string ClassName { get; set; }
public string Text { get; set; }
public string PartialText { get; set; }
public string XPath { get; set; }
public string CssModifier { get; set; }
}
class SearchOptions : PageBase
{
public SearchOptions()
{
Id = string.Empty;
ClassName = string.Empty;
Text = string.Empty;
PartialText = string.Empty;
XPath = string.Empty;
CssModifier = string.Empty;
}
public string JustAFunction()
{
Console.WriteLine(Id);
Console.WriteLine(ClassName);
return "ImportantReturn";
}
}
And then the call to get the important data looks like:
string importantData = new SearchOptions {Id = "10", ClassName = "Fluffy"}.JustAFunction();
add a comment |
You almost have it. You just need to add the new
keyword to create a new object before you can call a method on it:
string importantData = new SearchOptions {id = "10", className = "Fluffy"}.justAFunction();
Since you don't set any reference to the newly created object, it goes out of scope as soon as this line executes and will be cleaned up by the garbage collection engine.
Note that there are some other issues with your code that need to be fixed in order to make this work:
justAFunction()
should be publicly accessible (in your example, the access modifier is not specified, and the default isprivate
, so it would not work).- You cannot specify two return types (you have it declared as:
string void justAFunction
. You should remove thevoid
since you're returning a string).
And a few other suggestions:
- This assumes that the base class contains the properties you're setting (or they exist in this class and you just left them out for brevity)
- The properties you set when instantiating the object must be publicly accessible (or at least accessible from wherever you're creating it - usually they are
public
). - Public properties and methods should be
PascalCase
(notcamelCase
).
With these suggestions in mind, your classes would look something like:
class PageBase
{
public string Id { get; set; }
public string ClassName { get; set; }
public string Text { get; set; }
public string PartialText { get; set; }
public string XPath { get; set; }
public string CssModifier { get; set; }
}
class SearchOptions : PageBase
{
public SearchOptions()
{
Id = string.Empty;
ClassName = string.Empty;
Text = string.Empty;
PartialText = string.Empty;
XPath = string.Empty;
CssModifier = string.Empty;
}
public string JustAFunction()
{
Console.WriteLine(Id);
Console.WriteLine(ClassName);
return "ImportantReturn";
}
}
And then the call to get the important data looks like:
string importantData = new SearchOptions {Id = "10", ClassName = "Fluffy"}.JustAFunction();
You almost have it. You just need to add the new
keyword to create a new object before you can call a method on it:
string importantData = new SearchOptions {id = "10", className = "Fluffy"}.justAFunction();
Since you don't set any reference to the newly created object, it goes out of scope as soon as this line executes and will be cleaned up by the garbage collection engine.
Note that there are some other issues with your code that need to be fixed in order to make this work:
justAFunction()
should be publicly accessible (in your example, the access modifier is not specified, and the default isprivate
, so it would not work).- You cannot specify two return types (you have it declared as:
string void justAFunction
. You should remove thevoid
since you're returning a string).
And a few other suggestions:
- This assumes that the base class contains the properties you're setting (or they exist in this class and you just left them out for brevity)
- The properties you set when instantiating the object must be publicly accessible (or at least accessible from wherever you're creating it - usually they are
public
). - Public properties and methods should be
PascalCase
(notcamelCase
).
With these suggestions in mind, your classes would look something like:
class PageBase
{
public string Id { get; set; }
public string ClassName { get; set; }
public string Text { get; set; }
public string PartialText { get; set; }
public string XPath { get; set; }
public string CssModifier { get; set; }
}
class SearchOptions : PageBase
{
public SearchOptions()
{
Id = string.Empty;
ClassName = string.Empty;
Text = string.Empty;
PartialText = string.Empty;
XPath = string.Empty;
CssModifier = string.Empty;
}
public string JustAFunction()
{
Console.WriteLine(Id);
Console.WriteLine(ClassName);
return "ImportantReturn";
}
}
And then the call to get the important data looks like:
string importantData = new SearchOptions {Id = "10", ClassName = "Fluffy"}.JustAFunction();
edited Nov 16 '18 at 17:52
answered Nov 16 '18 at 17:26
Rufus LRufus L
19.4k31732
19.4k31732
add a comment |
add a comment |
Replace "string void justAFunction()" => "public string justAFunction()".
While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.
– Rufus L
Nov 16 '18 at 17:44
add a comment |
Replace "string void justAFunction()" => "public string justAFunction()".
While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.
– Rufus L
Nov 16 '18 at 17:44
add a comment |
Replace "string void justAFunction()" => "public string justAFunction()".
Replace "string void justAFunction()" => "public string justAFunction()".
answered Nov 16 '18 at 17:08
EvgeniiEvgenii
91
91
While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.
– Rufus L
Nov 16 '18 at 17:44
add a comment |
While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.
– Rufus L
Nov 16 '18 at 17:44
While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.
– Rufus L
Nov 16 '18 at 17:44
While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.
– Rufus L
Nov 16 '18 at 17:44
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.
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%2f53342342%2fis-it-possible-to-instantiate-a-class-using-initialization-list-and-call-a-metho%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
1
Yes, it is, but you might want a static method instead.
– John
Nov 16 '18 at 17:04