Solved

using Classes

Posted on 2009-05-11
13
129 Views
Last Modified: 2012-05-06
Is it possible to use the public string in a class in aspx pages without declaring a class in the aspx page?

I am trying to have less lines of code

        AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();

How can I write the above in one line only?
0
Comment
Question by:goodk
  • 7
  • 6
13 Comments
 
LVL 12

Expert Comment

by:rgn2121
ID: 24361074
You could create your AceClasses to take a string parameter when you declare and instantiate it by overloading your New()..
I don't use a lot of C#, but the New() could look something lke what is below.

Public Sub New( byval s as string)
 
verify 's' and set it equal to the string in your class...
 
End Sub
 
'''Then you could delare it by doing this...If GetConString returns a string...
dim aClass as New AceClasses(GetConnString())

Open in new window

0
 

Author Comment

by:goodk
ID: 24364553
so how it is one line?
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24370983
In VB, your one line is the :
Dim aClass as New AceClasses(GetConnString())

The only way I know to make it one line is if your NEW() constructor takes a parameter.  If it doesn't then I don't think there is anything you can do.  That's why I said to modify your class so that you can declare and instantiate your class in one line as well as provide the connection string.
 
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:goodk
ID: 24381647
Sorry I am not familiar with vb, I use c#.

        AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();

How would I combine the above into one line
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24381945
Following what I have stated about overloading your New() constructor, you would be able to use the following code:
AceClasses aClass = new AceClasses(GetConnString());  
 If not...since GetConnString() is a method in your AceClasses class then you have to declare a variable as AceClasses to be able to use the GetConnString().   Which would mean your code is as good as it gets...although not ideal.
Unless, you have created the GetConnString as shared (I think this is static in C#)...then you could use code similar to the line below.
AceClasses aClass = AceClasses.GetConnString();  
This is assuming your GetConnString() is similar to what is below:
VB:
Public Shared Function GetConnString() as String
'Code to get the connection string
End Function
 C# by way of a converter.   http://www.developerfusion.com/tools/convert/vb-to-csharp/

Public Shared Function GetConnString() as String
'Code to get the connection string
End Function
 
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24381953
Sorry...That last section of code should have been in C#..
public static string GetConnString()
 {    
//Code to get the connection string
}
 
0
 

Author Comment

by:goodk
ID: 24384492
AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();

so you are saying I could write the above as

AceClasses connString = new AceClasses(GetConnString());

??
 
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24391860
Unless I am completely misunderstanding, you are trying to delare a class of type AceClasses correct?
I assumed that you were trying to get a connection string to use with the class you just created.  If that is not the case, then I am confused by what you are doing.  
The top section of code in the window below is declaring aClass as an AceClasses Type.  Then you are declaring a string and setting it equal to the string returned by the AceClasses function GetConnString()..
The bottom section of code is totally different...  In it you are declaring connString as type AceClasses and setting it equal to the string returned by GetConnString();  I doubt that you want to or you are able to set your AceClasses object equal to a string.

AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();
 
 
 
AceClasses connString = new AceClasses(GetConnString());

Open in new window

0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24391877
I know nothing about AceClasses, but I would assume you have created a class called AceClasses.  In this class there is a function that returns a string called GetConnString().  Is this correct?
If the above is true, then my next question is do you need to declare an object of type AceClasses or do you just want to get the string returned by GetConnString() into a different string variable?
0
 

Author Comment

by:goodk
ID: 24392062
first paragrph -- YES

second paragraph -- True, all I want is move the return string into a local variable.  Is it possible to do it in one line. If possible, how?

0
 
LVL 12

Accepted Solution

by:
rgn2121 earned 500 total points
ID: 24392392
Yes, but your GetConnString() will have to be declared as Static so that you don't have to create an instance of AceClasses to use it.
Like I stated above your code for GetConnString() would have to look something like what I have below:

public static string GetConnString()
{    
//Code to get the connection string
}
If it does, then you should be able to do the following:
 string connString = AceClasses.GetConnString();

Like I stated early...I really don't use C#, but I think this code works.  I use this all the time in VB.  I have numerous classes that have "Shared" members.  This way I don't have to create an instance of the class to use the functions, properties, methods, etc that it contains.  The "Static" keyword is used in C#...
If you use private or class level variables inside of GetConnString, then those will need to be declared Static as well or you will get an error about access to a Static member...
0
 

Author Comment

by:goodk
ID: 24395713
works, thanks - I think the trick was to declare the string as static.  I do not have a need to manupulate the string externally anyway.

thanks again - bye
0
 

Author Closing Comment

by:goodk
ID: 31580395
thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to gather data from news site 6 46
Adobe Certified Expert? 1 110
Summernote required 3 183
Detailed steps to upload 6 52
Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question